为什么在控制台中显示错误:"未指定数据"使用jqPlot之后

时间:2017-07-14 04:56:47

标签: javascript jquery jqplot

假设我有两个分别用于x轴和y轴的数组。我想用jsPlot绘制折线图。我这样做了:

 if (watervalue.length != 0) {

                        for (var a = 0; a < watervalue.length; a++) {

                            wvalues += '[' + months[watermonth[a]] + ',' + watervalue[a] + '],';
                        }
  }

                    wvalues = wvalues.slice(0, -1) + ']';
var plot2 = $.jqplot('container1', wvalues, {
                        // Give the plot a title.
                        title: 'Plot With Options',
                        // You can specify options for all axes on the plot at once with
                        // the axesDefaults object.  Here, we're using a canvas renderer
                        // to draw the axis label which allows rotated text.
                        axesDefaults: {
                            labelRenderer: $.jqplot.CanvasAxisLabelRenderer
                        },
                        // Likewise, seriesDefaults specifies default options for all
                        // series in a plot.  Options specified in seriesDefaults or
                        // axesDefaults can be overridden by individual series or
                        // axes options.
                        // Here we turn on smoothing for the line.
                        seriesDefaults: {
                            rendererOptions: {
                                smooth: true
                            }
                        },
                        // An axes object holds options for all axes.
                        // Allowable axes are xaxis, x2axis, yaxis, y2axis, y3axis, ...
                        // Up to 9 y axes are supported.
                        axes: {
                            // options for each axis are specified in seperate option objects.
                            xaxis: {
                                label: "X Axis",
                                // Turn off "padding".  This will allow data point to lie on the
                                // edges of the grid.  Default padding is 1.2 and will keep all
                                // points inside the bounds of the grid.
                                pad: 0
                            },
                            yaxis: {
                                label: "Y Axis"
                            }
                        }
                    });

显示错误,未指定数据

console.log的{​​{1}}输出为:

  

[[二月,0],[三月,0],[四月,0],[日,0],[六月,0],[七月,7]]

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

你必须pass an Array instead of a string。修改你的for循环,类似于follow,并删除wvalues = wvalues.slice(0, -1) + ']';行。

&#13;
&#13;
var months = ['February', 'March', 'April', 'May', 'June', 'July'];
var watervalue = [0, 0, 0, 0, 0, 7];

var wvalues = [];
var tmp = [];

for(var i=0; i < watervalue.length; i++){

  tmp = [months[i], watervalue[i]];
  // In your case: tmp = [months[watermonth[i]], watervalue[i]]
  
  wvalues.push(tmp);

}

console.log(wvalues); 
// output: [["February",0],["March",0],["April",0],["May",0],["June",0],["July",7]]
&#13;
&#13;
&#13;