dorien dorien - 3 months ago 5
jQuery Question

Accessing a previously drawn plot in javascript/jquery

I draw a plot like this:

var items = $.get("./moonlight_sonata_diameter.data", function(data) {

items = data.split(/\r?\n/).map( pair => pair.split(/\s+/).map(Number) );

$(function () {

plot = $.plot($("#placeholder"),
[ { data: linePoints} ], {
series: {
lines: { show: true }
},
crosshair: { mode: "x" },
grid: { hoverable: true, autoHighlight: false },
yaxis: { min: 0, max: 5 }

});
});
});


Now at a later moment in time, I want to update the crosshair of the plot. However, because it is embedded in so many functions, I don't know how to access it as I am not familiar with jQuery.

Within the script, I can run:

plot.setCrosshair({x: 100})


However, in another script, at another time, there is no object called plot. Is there a way to access it still?

Answer

Actually you have put your plot creation code in document ready function and your plot.setCrosshair({x:100}) is executed just before your plot creation code. so A simple settimeout will do the trick.

just replace

 plot.setCrosshair({x: 4})

with

 setTimeout(function(){  plot.setCrosshair({x: 41})}, 3000);

and this will work fine. if you call your setCrosshair function after loading the complete dom then you will not need of setTimeout function. I hope this will help and if not then let me know. Check it at http://plnkr.co/edit/3cMHmzWEIk6c39mblb0Z?p=preview