flot how to know the x axis points in dynamic data

2019-08-30 11:01发布

问题:

I am using flot jquery library to draw charts.

when my chart has a specific number of columns, I can set the x values for my points.

but now I have a dynmic data. so how can I know the points of the x axis please?

for example,

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

but lets say that I will have 9 columns. how can I know the x axis for them please?

I am printing the chart in this way

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

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

the input data is like this

label: "label1"

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

but now I don't know how many points in that array. it could be 11 or it could be 2

edit

this the chartoption

  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:

When using $.plot(holder, data, chartOptions);, save the plot with a variable.

Declare var plot; globally,

and then call like this: plot = $.plot(holder, data, chartOptions);.

You can grab the data from the plot like this:

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]
        }
    }
}