Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

javascript - updating flot data and axis

I have created a highly interactive graph. all of the interactions have been achieved without re-initiating the flot object. re-initiating the flot object causes the interactions to break, i would have liked to have done this as efficiently as possible.

the interactions my graph supports are:

  • changing visibility of individual series
  • multiple y-axes and toggling between them
  • zoom graph(with a reset)
  • click and drag panning

the final two features i have to add are:

  • adding/subtracting individual series
  • updating individual series

both of which require ajax requests, the graph must support 1000's of datapoints so efficiency is key (i will have to reduce the datapoints at some point).

as i mentioned i have managed to do all of this so far without multiple calls to $.plot, i have used plugins in-built functions and setting some options from the getOptions() method. instead of using $.plot i call the plot.draw() method to update the chart. But, i am struggling to find anyway to push data into the provided methods that don't involve calling the entire function again (besides doing so makes it harder to keep track of the variables i am changing in options.)

My question is has anyone had experience with updating, not replacing, flot axes and data using ajax requests and how were you able to achieve efficient results?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The method you are missing is the setupGrid. This will recreate the axises without re-initing the plot.

In general my workflow for modfiying an existing plot is something like this:

var series = plot.getData(); // reference to your series
series[0].data = someNewArray;
series[0].color = 'blue'; // modify existing series
series.push({data: [[0, 5], [1, 1], [2, 7]], color: 'green'}); // add a new one
plot.setData(series); // you need to set the data so that flot will re-process any newly added series
var opts = plot.getOptions() // get a reference to the options
opts.xaxes[0].min = -1; // adjust an axis min, use the xaxes property NOT the xaxis
// there is no need to "setOptions"...
plot.setupGrid() // if you need to redraw the axis/grid
plot.draw()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...