HTMLcopy
1
<div id="tooltip" class="custom-tooltip"></div>
2
<div id="container"></div>
CSScopy
15
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
.custom-tooltip {
8
visibility: hidden;
9
pointer-events: none;
10
position: absolute;
11
width: 100px;
12
padding: 4px;
13
border: solid black 2px;
14
background: #fff;
15
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
var data = anychart.data.set([
5
["Velma", 10],
6
["Fred", 12],
7
["Daphne", 18],
8
["Shaggy", 6],
9
["Scooby", 46]
10
]);
11
12
// map the data
13
var dataMapping = data.mapAs({x: 0, value: 1});
14
15
// create a chart
16
var chart = anychart.column(dataMapping);
17
18
// set the data
19
var series = chart.getSeries(0);
20
21
// disable the built-in tooltips
22
series.tooltip(false);
23
24
// set the selection mode to single
25
chart.interactivity().selectionMode("single-select");
26
27
// set the chart title
28
chart.title("How many pieces of pizza have they eaten?");
29
30
var tooltip = document.getElementById("tooltip");
31
32
/* show a custom tooltip
33
when the mouse is over a column */
34
chart.listen("pointMouseOver", function(e) {
35
36
// show the tooltip
37
tooltip.style.visibility = "visible";
38
39
// set the text of the tooltip
40
tooltip.innerHTML = data.row(e.pointIndex)[0] + " has eaten "
41
+ data.row(e.pointIndex)[1] +
42
" pieces of pizza in total";
43
});
44
45
/* hide the custom tooltip
46
when the mouse is out of a column */
47
chart.listen("pointMouseOut", function() {
48
tooltip.style.visibility = "hidden";
49
});
50
51
// set the position of custom tooltips
52
chart.listen("mouseMove", function(e) {
53
54
var clientX = e["offsetX"];
55
var clientY = e["offsetY"];
56
57
tooltip.style.left = clientX + 20 + "px";
58
tooltip.style.top = clientY + 10 + "px";
59
tooltip.style.zIndex = 10000;
60
tooltip.style.border = "solid black 2px";
61
});
62
63
// set the container id
64
chart.container("container");
65
66
// initiate drawing the chart
67
chart.draw();
68
});