having trouble updating my legend with values

90 views
Skip to first unread message

Peter

unread,
Feb 10, 2012, 4:37:15 PM2/10/12
to Flot graphs
I have been reading the example on the FLOT home page and googling for
other examples but I have not yet been able to get my graph to update
the value in the legend with the value of the graph under the mouse.
Here is my code, I am not getting any errors from firebug or the error
console so I am really confused. Could someone please point out my
mistake?

var container = $("#covarianceChartContainer");
var data = [{ data: covariance, label: "T = 0.0" }];
var options = {
crosshair: { mode: "x" },
xaxis:{
mode: "time",
ticks: 6
},
grid: { hoverable: true}
};
var plot = $.plot(container, data, options);
var legends = $("#covarianceChartContainer .legendLabel");


container.bind("plothover", function (event, pos) {
for(i in plot.getData().length){
var series = plot.getData()[i];
var j = 0;
// find the nearest points, x-wise
for (j = 0; j < series.data.length; ++j){
if (series.data[j][0] > pos.x){
break;
}
}
// now interpolate
var y, p1 = series.data[j - 1], p2 = series.data[j];
if (p1 == null)
y = p2[1];
else if (p2 == null)
y = p1[1];
else
y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0]
- p1[0]);

legends.eq(i).text(series.label.replace(/=.*/, "= " +
y.toFixed(2)));
}
});

Emily

unread,
Nov 9, 2012, 9:29:47 AM11/9/12
to flot-...@googlegroups.com, peterds...@gmail.com
Can't be sure because I'm new to think but I think the problem is in this line...

legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2))); 

The part I highlighted works for the example on Flot because of the existing text in the legend. You'll want to put what exists in your version of the legend in that field. It's looking for what part of the legendLabel to replace. 

Here's more on the javascript replace method: http://www.w3schools.com/jsref/jsref_replace.asp
Reply all
Reply to author
Forward
0 new messages