Skip to content

Commit 4bde9d7

Browse files
committed
Updated README
1 parent 31de956 commit 4bde9d7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,51 @@ Chart.js
22
=======
33
*Simple HTML5 Charts using the canvas element* [chartjs.org](http://www.chartjs.org)
44

5+
## Changes
6+
This adds a mouseover and mouseout event to the data points in a line graph (events for all graphs may follow shortly). I implemented because I needed to implement tooltips for the data points, but you can use the event to do whatever you like.
7+
8+
To hook into the events, just specify a mouseover and mouseout event handler for your dataset. This works for browser mouse events as well as mobile touch events.
9+
```javascript
10+
var data = {
11+
labels : labels,
12+
datasets : [
13+
{
14+
fillColor : "rgba(151,187,205,0.5)",
15+
strokeColor : "rgba(151,187,205,1)",
16+
pointColor : "rgba(151,187,205,1)",
17+
pointStrokeColor : "#fff",
18+
data : values,
19+
mouseover: function(data) {
20+
// data returns details about the point hovered, such as x and y position and index, as
21+
// well as details about the hover event in data.event
22+
// You can do whatever you like here, but here is a sample implementation of a tooltip
23+
var active_value = values[data.point.dataPointIndex];
24+
var active_date = labels[data.point.dataPointIndex];
25+
// For details about where this tooltip came from, read below
26+
tooltip.html(active_date + " - " + active_value).css("position", "absolute").css("left", data.point.x-17).css("top", data.point.y-55).css("display", 'block');
27+
},
28+
mouseout: function (data) {
29+
// Hide the tooltip
30+
tooltip.html("").css("display", "none");
31+
}
32+
}
33+
]
34+
};
35+
36+
var ctx = document.getElementById("myCanvasElement").getContext("2d");
37+
var chart = new Chart(ctx).Line(data);
38+
```
39+
40+
As you can see above, we can easily add an HTML tooltip to the data point by using the x and y position of the point hovered. Above, I offset the tooltip by a few pixels, position it absolutely, and format using plain old CSS/HTML. The absolute position works because I wrap my canvas in a relative div and put the tooltip as a sibling to the canvas:
41+
```html
42+
<div style="position: relative;">
43+
<canvas id="myCanvasElement" width="400" height="400"></canvas>
44+
<div id="tooltip"></div>
45+
</div>
46+
```
47+
48+
Make your tooltips as fancy as you like now! My implementation:
49+
![Sample Chart.js Tooltip](http://github.com/jhdavids8/Chart.js/raw/master/sample_tooltip.png)
550

651
Documentation
752
-------

sample_tooltip.png

32.6 KB
Loading

0 commit comments

Comments
 (0)