displaying custom tooltip when hovering over a point in flot

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.

Answers


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

$("#placeholder").bind("plotclick", function (event, pos, item) {/* code */});

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 the item objects when clicked and add additional data to them and use them for the hover 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 called alternateText and stores it in an array called itemsClicked.

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 current item object, which is obtained via item.dataIndex. If there is a matching index in the cache array itemsClicked it will grab the item object from it and use the alternateText property that was added during the click event earlier.

The first point's item object would look something like this:

item : {
    dataIndex: 0,
    datapoint: [
        1290802154,
        0.3
    ],
    pageX: 38,
    pageY: 82,
    series: {/* properties of the series that this point is in */},
    seriesIndex: 0
}

Once clicked it would then look like this and be stored in the itemsClicked array:

item : {
    alternateText: 'hello',
    dataIndex: 0,
    datapoint: [
        1290802154,
        0.3
    ],
    pageX: 38,
    pageY: 82,
    series: {/* properties of the series that this point is in */},
    seriesIndex: 0
}

Please let me know if any of this is helpful or not, if not I'll shut up and stop updating my answer :P


You can add data to the series simply by adding it to the data array.

Instead of

$.plot(element, [[1, 2], [2, 4]] ...)

You can

$.plot(element, [[1, 2, "label"], [2, 4, "another label"]] ...)

And then use that information to show a custom label.

See this fiddle for a full example: http://jsfiddle.net/UtcBK/328/


Need Your Help

AS3 getting vars, but can't access

php actionscript-3

I am trying to get some user data from PHP to AS3.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.