javascript - Flot Re-scale Yaxis minmax on X-axis scale - Stack Overflow

admin2025-04-18  0

I have one long unixtime, value Array which is used to initiate a flot chart, and some buttons to change the scale, what I can't seem to be able to do is get Y-axis to scale with the change in X-scale.

Here is an example chart: /

var datarows = //Data Array Here
var options =  { series: { lines: { show: true }, points: { show: true } },
        grid: { hoverable: true,clickable: false, },
        xaxis: { mode: "time", min: ((new Date().getTime()) - 30*24*60*60*1000), max: new Date().getTime(), }
    };
function castPlot() {
window.PLOT = $.plot("#placeholder", [{ data: dataRows }], options
);
};

In the official example scaling is automatic and unspecified on the Y-axis: .html

The only alternative I can think of is looping through the dataset and calculating new Y min/max on each button press. Unless I am breaking some very obvious default function.

I have one long unixtime, value Array which is used to initiate a flot chart, and some buttons to change the scale, what I can't seem to be able to do is get Y-axis to scale with the change in X-scale.

Here is an example chart: http://jsfiddle/U53vz/

var datarows = //Data Array Here
var options =  { series: { lines: { show: true }, points: { show: true } },
        grid: { hoverable: true,clickable: false, },
        xaxis: { mode: "time", min: ((new Date().getTime()) - 30*24*60*60*1000), max: new Date().getTime(), }
    };
function castPlot() {
window.PLOT = $.plot("#placeholder", [{ data: dataRows }], options
);
};

In the official example scaling is automatic and unspecified on the Y-axis: http://www.flotcharts/flot/examples/axes-time/index.html

The only alternative I can think of is looping through the dataset and calculating new Y min/max on each button press. Unless I am breaking some very obvious default function.

Share asked Jul 15, 2014 at 22:26 user3467349user3467349 3,2015 gold badges40 silver badges64 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

When calculating y-scale, flot does not look at only the "viewable" data but the whole dataset. Since the data points are still present, the y min/max respects them. So your options are:

  1. Subset the series data down to the desired range and let flot scale both x and y.
  2. As you suggested, calculate your own min/max on the y axis.

If you plot get any more plicated than it is now (especially if you start setting up click/hover events on it), I would also remend you switch to redrawing instead of reiniting your plot.

var opts = somePlot.getOptions();
opts.xaxes[0].min = newXMin;
opts.xaxes[0].max = newXMax;
opts.yaxes[0].min = newYMin;
opts.yaxes[0].max = newYMax;
somePlot.setupGrid();
somePlot.draw();

EDITS

Here's one possible solution.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744951258a276349.html

最新回复(0)