Black Lives Matter. Please consider donating to Black Girls Code today.

addTraces error - "gd.data must be an array"

Hi,

I’m relatively new to plotly js and can’t get around this error when using .addTraces().

I have an existing scatter plot in ‘myDiv’, and I want to add a few traces. I’ve defined the traces like this:

                var trace3 = {
                    type: "scatter",
                    mode: "lines",
                    name: "F1",
                    x: dateArray1,
                    y: priceArray1,
                    visible: "legendonly",
                    line: { color: '#FF5050' },
                    connectgaps: true,
                    marker: {
                        size: 5,
                    }
                };

                var trace4 = {
                    type: "scatter",
                    mode: "lines",
                    name: "F2",
                    x: dateArray2,
                    y: priceArray2,
                    visible: "legendonly",
                    line: { color: '#3399FF' },
                    connectgaps: true,
                    marker: {
                        size: 5,
                    }
                };

                var trace5 = {
                    type: "scatter",
                    mode: "lines",
                    name: "F3",
                    x: dateArray3,
                    y: priceArray3,
                    visible: "legendonly",
                    line: { color: '#4dffa6' },
                    connectgaps: true,
                    marker: {
                        size: 5,
                    }
                };

                var trace6 = {
                    type: "scatter",
                    mode: "lines",
                    name: "F4",
                    x: dateArray4,
                    y: priceArray4,
                    visible: "legendonly",
                    line: { color: '#7070db' },
                    connectgaps: true,
                    marker: {
                        size: 5,
                    }
                };

Then I set var data = [trace3, trace4, trace5, trace6]

When I call Plotly.addTraces(‘myDiv’, data); I get the error ‘gd.data must be an array’. However, maybe one out of every five times, the traces are added without an error.

I’ve tried initializing data as [], then pushing the traces and it doesn’t work. I’ve tried just typing the traces directly into an array in the .addTraces call, doesn’t work. I’ve tried setting the array equal to the first element of each trace [trace3[0], trace4[0]] and it still didn’t work. What am I missing?

To add another data point to my question - if I call Array.isArray on my data object, I get True. Then when I pass the object to .addTraces, I get the “gd.data must be an array” message again.

Turns out I was looking in the wrong place. I’m creating my plot in one ajax call, and adding these traces in another. It was a coin flip as to which ajax call would finish first, which meant it was a coin flip as to whether or not there was a data property for addTraces() to access. I just made sure it was explicit that these traces were to be added after the plot was generated and everything’s fine now.

1 Like