From the example here, I kind of know how to create a Flot graph that shows tooltips when hovering. But the examples only show to how to display tooltips containing the x value, y value, label, etc., and I can't figure out how to create more customized tooltips.
Is there someplace I can attach custom data, that I can access when creating the tooltip?
For example, to simplify, let's suppose my code looks like:
var d = [ { label: "Fake!", data: [ [1290802154, 0.3], [1292502155, 0.1] ] } ];
var options = {
xaxis: { mode: "time" },
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true }
};
$.plot($("#placeholder"), d, options);
Now, when clicking on the first datapoint, I want the tooltip to show "Hello", and when clicking on the second datapoint I want to show "Bye". How do I do this? When binding the plothover event
$(".placeholder").bind("plothover", function (event, pos, item) { /* etc. */ };
I'm not sure what "item" refers to, and how to attach data to it.
Here is a rough JSFiddle example that I whipped up. Not sure if it's functioning exactly how you like but might spark an idea...
[update]
you'll want to bind to the
event for clicking events
[update2] Updated Example
I've adjusted the example to use your test data and to work more with what you have described above. As for the
item
object it seems that it is generated on the fly so, from what I can tell, you can not add additional data to it. However, you can create an array to cache theitem
objects when clicked and add additional data to them and use them for thehover
event.This new example does just that, it shows the normal tooltip when nothing is clicked. but once clicked it determines if the point clicked was the first or second and adds an addition property to the
item
object calledalternateText
and stores it in an array calleditemsClicked
.Now when a point is hovered over it checks to see if there is a cached
item
object within the array based on the same index of the currentitem
object, which is obtained viaitem.dataIndex
. If there is a matching index in the cache arrayitemsClicked
it will grab theitem
object from it and use thealternateText
property that was added during theclick
event earlier.The first point's
item
object would look something like this:Once clicked it would then look like this and be stored in the
itemsClicked
array:Please let me know if any of this is helpful or not, if not I'll shut up and stop updating my answer :P
Also you can try following code:
HTML body:
Script:
You can add data to the series simply by adding it to the data array.
Instead of
You can
And then use that information to show a custom label.
See this fiddle for a full example: http://jsfiddle.net/UtcBK/328/