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
3
// create column chart
4
var chart = anychart.column();
5
6
// set series data
7
var series = chart.column([
8
["Jan", 10],
9
["Feb", 12],
10
["Mar", 11],
11
["Apr", 15],
12
["May", 20],
13
["Jun", 22],
14
["Jul", 21],
15
["Aug", 28],
16
["Sep", 29],
17
["Oct", 40],
18
["Nov", 41],
19
["Dec", 45]
20
]);
21
22
series.selected().fill("#444");
23
series.stroke(null);
24
25
// adding dollar symbols to yAxis labels
26
var yLabels = chart.yAxis().labels();
27
yLabels.format(function() {
28
return "$" + this.value;
29
});
30
31
// create an event listener for the pointsHover event
32
chart.listen("pointsHover", function(event){
33
// getter for hovered point
34
var point = event.point;
35
// index of hovered point
36
var index = point.getIndex();
37
// getter for hovered point's series
38
var currentSeries = point.getSeries();
39
/* if this event is triggered when the point
40
is not in the hover state, nothing will happen */
41
if (!event.currentPoint.hovered) return;
42
// get an array of months, hovered point belong to and hover it.
43
currentSeries.hover(getQuarterMonths(index));
44
});
45
46
// create an event listener for the pointsSelect event
47
chart.listen("pointsSelect", function(event){
48
// getter for selected point
49
var point = event.point;
50
// index of selected point
51
var index = point.getIndex();
52
// getter for selected point's series
53
var currentSeries = point.getSeries();
54
// if the point was deselected
55
if (!event.currentPoint.selected)
56
// deselect every point of the series
57
currentSeries.select(false);
58
// if the point was selected
59
else {
60
// get an array of months, selected point belong to
61
var months = getQuarterMonths(index);
62
// select each point from the array
63
for (var i =0; i < months.length; i ++)
64
currentSeries.getPoint(months[i]).selected(true);
65
}
66
});
67
68
69
var tooltip = series.tooltip();
70
tooltip.titleFormat(function(point){
71
// index of the current point
72
var index = point.index;
73
// adjust title depending on point's index
74
if (index<3) return "First Quarter";
75
else if (index<6) return "Second Quarter";
76
else if (index<9) return "Third Quarter";
77
else return "Fourth Quarter";
78
});
79
80
tooltip.format(function(point){
81
// get an array of months the point belong to
82
var months = getQuarterMonths(point.index);
83
// create variable for counting the income in the months
84
var value = 0;
85
// summarize the income
86
for (var i=0;i<months.length;i++)
87
value+=series.getPoint(months[i]).get("value");
88
// display the income
89
return "Income: $" + value;
90
});
91
92
// set container id for the chart
93
chart.container("container");
94
95
// initiate chart drawing
96
chart.draw();
97
});
98
// find out an array a month belong to
99
function getQuarterMonths(month) {
100
var quarterStartMonth = 3 * Math.floor(month / 3);
101
return [quarterStartMonth, quarterStartMonth + 1, quarterStartMonth + 2];
102
}