Changing axis min/max on an existing plot using Flot JQuery Library
Asked Answered
P

2

16

I've created a series of plots using the flot library, which are all displayed on a single page. Is there a way to update the X axis min and max options (options.xaxis.min, options.axis.max) values WITHOUT re-plotting the plots ($.plot('placeholder',data,options))?

I found this solution: http://osdir.com/ml/flot-graphs/2012-02/msg00064.html Which suggests that the following line would do it, but it does not work for me - the plots visible min and max are not modified based on this call.

monitorGraph.getOptions().xaxis[0].max = xaxis.max;

Any tips on updating the graphs xaxis max and min values would be greatly appreciated!

EDIT: Solution Below

The following code will take an existing plot, update the range that is visible, and redraw it in a very light and efficient way.

            plot.getOptions().xaxis[0].min = time.start;
            plot.getOptions().xaxis[0].max = time.end;
            plot.setupGrid();
            plot.draw();
Phenyl answered 18/7, 2013 at 23:3 Comment(1)
To specify futher: I would like the plots to reflect the new x min and max, but I would not like to re-plot them completely, since that is very expensive and seems to be slowing down the process.Phenyl
P
12

After you set the value of the yaxis max height, try

yourPlot.setupGrid();

Not sure if it'll be as smooth as you want but I think it does the trick.

Passementerie answered 18/7, 2013 at 23:11 Comment(6)
This helped a lot! Major improvement over calling .plot each time. Thank you!Phenyl
Oops, at second glance it seems myPlot.setupGrid(); also does not update the plot visually in any way. Any ideas?Phenyl
Hmmm I went on flot's website and just dicked around in the console. Maybe try setupGride then also try draw()?Passementerie
and are you sure that you did "monitorGraph.getOptions().yaxes[0].max = whateverheightyouwant" before you did setupGrid()?Passementerie
I tried both in a row and they seem to not effect the plots - although that is what the flot website says they are supposed to do. There may be an issue with how I am storing my plots. Thank you for your help!Phenyl
Yeah make sure the variable you are using in place of "myPlot" is actually a reference to your plot. And if it works out, accept my answer! CheersPassementerie
D
12

You can also dynamically modify MIN/MAX parameters in axis options:

plot.getAxes().xaxis.options.min = 0;
plot.getAxes().xaxis.options.max = 999; 

plot.setupGrid();
plot.draw();
Dismissal answered 11/8, 2014 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.