HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
var tooltip = $("<div class='custom-tooltip' id='tooltip'></div>").css({
3
"position": "absolute",
4
"pointerEvents": "none",
5
"display": "none",
6
"border": "1px solid #d8d8d8",
7
"height": "150px",
8
"width": "320px"
9
});
10
11
// data
12
var data = anychart.data.set([
13
["2000", 10048, 2600, 3200, 2159, 2089],
14
["2001", 10938, 2367, 1989, 2383, 4199],
15
["2002", 9989, 2156, 2399, 2867, 2567],
16
["2003", 10226, 2398, 2225, 3010, 2593],
17
["2004", 11004, 2851, 3007, 2805, 2341],
18
["2005", 10934, 3400, 2105, 2563, 2866],
19
["2006", 11275, 2878, 2933, 3561, 1903],
20
["2007", 11945, 2959, 3855, 3109, 2022],
21
["2008", 12245, 4001, 2156, 3928, 2160],
22
["2009", 12056, 1995, 2057, 4902, 3102],
23
["2010", 11734, 2584, 3166, 3101, 2883],
24
["2011", 12337, 3106, 2845, 2933, 3453],
25
["2012", 12761, 3296, 2655, 3144, 3666],
26
["2013", 13390, 2611, 4194, 3206, 3379],
27
["2014", 13818, 3000, 2599, 4107, 4112],
28
["2015", 13188, 3199, 2564, 3118, 4307]
29
]);
30
31
// map data for the each series
32
var dataMapping = data.mapAs({x: [0], value: [1]});
33
34
// chart type
35
var chart = anychart.column();
36
37
// set data
38
var column = chart.column(dataMapping);
39
// disable default tooltip
40
column.tooltip(false);
41
42
// chart title
43
var title = chart.title();
44
title.useHtml(true);
45
title.text("<span style=\"color:#545f69; font-size: 18px;\">" +
46
"Sales volume</span>" +
47
"<br>" +
48
"<span style=\"color:#545f69; font-size: 10px\">ACME Corp.</span>");
49
title.enabled(true);
50
51
// set axes titles
52
var xAxis = chart.xAxis();
53
xAxis.title("Retail Channel");
54
var yAxis = chart.yAxis();
55
yAxis.title("Sales");
56
57
// disable legend
58
chart.legend(false);
59
60
// disable select mode
61
var interactivity = chart.interactivity();
62
interactivity.selectionMode("none");
63
64
var barChart = anychart.bar();
65
var barSeries = barChart.bar([0]);
66
67
// create tooltip chart and adjust some settings
68
function createTooltipChart() {
69
// adjust series
70
barSeries.stroke(null);
71
// configure axes and labels
72
var barXAxis = barChart.xAxis();
73
barXAxis.stroke(null);
74
barXAxis.ticks(false);
75
var xLabels = barXAxis.labels();
76
xLabels.fontSize(10);
77
var yLabels = barChart.yAxis().labels();
78
yLabels.fontSize(10);
79
// adjust title
80
var barTitle = barChart.title();
81
barTitle.enabled(true);
82
barTitle.fontSize(12);
83
// set padding
84
barChart.padding([0, 12, 0, 0]);
85
// set tooltip container
86
barChart.container("tooltip");
87
// draw tooltip chart
88
barChart.draw();
89
}
90
91
// event for mouse over a column
92
chart.listen("pointMouseOver", function (e) {
93
// display hidden tooltip
94
tooltip.css({"display": "block"});
95
var index = e.pointIndex;
96
97
var dataRow = data.row(index);
98
// set new data for tooltip chart
99
barSeries.data([
100
{x: "John", value: dataRow[2]},
101
{x: "James", value: dataRow[3]},
102
{x: "Peter", value: dataRow[4]},
103
{x: "Mattew", value: dataRow[5]}
104
]);
105
var titleText = "Sales Persons Share: ";
106
// adjust title
107
barChart.title(titleText + dataRow[0]);
108
});
109
110
// event for mouse leaving column
111
chart.listen("pointMouseOut", function () {
112
tooltip.css({"display": "none"});
113
});
114
115
// adjust tooltip position on mouse move
116
chart.listen("mouseMove", function (e) {
117
var $container = $(this.container().getStage().container());
118
var offset = $container.offset();
119
var clientX = e['clientX'] - offset['left'];
120
var clientY = e['clientY'] - offset['top'];
121
122
if (!$container.find('.custom-tooltip').length) {
123
$container.append(tooltip);
124
createTooltipChart();
125
}
126
127
// prevent tooltip from leaving the screen
128
var left = (clientX - 10) - tooltip.width() / 2;
129
if (left + tooltip.width() > chart.container().width())
130
left = chart.container().width() - 2 - tooltip.width();
131
if (left < 0)
132
left = 0;
133
// get mouse position and move tooltip
134
tooltip.css({"left": left, "top": (clientY - 10) - tooltip.height()});
135
});
136
137
// draw
138
chart.container("container");
139
chart.draw();
140
});