如何知道动态数据中的x轴点

时间:2014-04-14 15:55:52

标签: javascript jquery flot

我正在使用flot jquery库来绘制图表。

当我的图表有特定数量的列时,我可以为我的点设置x值。

但现在我有一个动态数据。那我怎么知道x轴的点呢?

例如,

the x axis of the first point is 1325876000000
the x axis of the second point is 1328194400000
the third is 1330360000000
the fourth is 1332838400000

但是我要说我会有9列。我怎么知道他们的x轴呢?

我正在以这种方式打印图表

var holder = $('#vertical-chart');

   if (holder.length) {
       $.plot(holder, data, chartOptions);
   }

输入数据是这样的

标签:" label1"

data: [[1325876000000,0],[1325876000000,0],[1325876000000,0],[1325876000000,30]]

但现在我不知道该阵列中有多少分。它可能是11或它可能是2

修改

这是图表选项

  chartOptions = {
           xaxis: {
               min: (new Date(2011, 11, 15)).getTime(),
               max: (new Date(2012, 04, 18)).getTime(),
               mode: "time",
               tickSize: [2, "month"],
               monthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
               tickLength: 0
           },
           grid: {
               hoverable: true,
               clickable: false,
               borderWidth: 0
           },
           bars: {
               show: true,
               barWidth: 12 * 24 * 60 * 60 * 300,
               fill: true,
               lineWidth: 1,
               order: true,
               lineWidth: 0,
               fillColor: { colors: [{ opacity: 1 }, { opacity: 1 }] }
           },

           tooltip: true,
           tooltipOpts: {
               content: '%s: %y'
           },
           colors: App.chartColors
       }

1 个答案:

答案 0 :(得分:0)

使用$.plot(holder, data, chartOptions);时,请使用变量保存图表。

全局声明var plot;

然后像这样打电话:plot = $.plot(holder, data, chartOptions);

您可以从图中获取数据:

var datasets = [];
var xData = [];

datasets = plot.getData();    //this returns an array of all datasets

//goes through each series of data
for (var i = 0; i < datasets.length; i++){
    //picks the series with label of "label 1"
    if (datasets[i].label == "label 1"){
        //copies x coordinates from the series into xData
        for (var j = 0; j < datasets[i].data[j].length; j++){
            xData.push(datasets[i].data[j][0]);               //if you want y coords, use datasets[i].data[j][1]
        }
    }
}