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.selectFill("#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
// event on hovering a point
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 on points unhovering, nothing will happen
40
if (!event.currentPoint.hovered) return;
41
// get an array of months, hovered point belong to and hover it.
42
currentSeries.hover(getQuarterMonths(index));
43
});
44
45
// event on point hovering
46
chart.listen("pointsSelect", function(event){
47
// getter for selected point
48
var point = event.point;
49
// index of selected point
50
var index = point.getIndex();
51
// getter for selected point's series
52
var currentSeries = point.getSeries();
53
// if the point was unselected
54
if (!event.currentPoint.selected)
55
// unselect every point of the series
56
currentSeries.select(false);
57
// if the point was selected
58
else {
59
// get an array of months, selected point belong to
60
var months = getQuarterMonths(index);
61
// select each point from the array
62
for (var i =0; i < months.length; i ++)
63
currentSeries.getPoint(months[i]).selected(true);
64
}
65
});
66
67
68
var tooltip = series.tooltip();
69
tooltip.titleFormat(function(point){
70
// index of the current point
71
var index = point.index;
72
// adjust title depending on point's index
73
if (index<3) return "First Quarter";
74
else if (index<6) return "Second Quarter";
75
else if (index<9) return "Third Quarter";
76
else return "Fourth Quarter";
77
});
78
79
tooltip.format(function(point){
80
// get an array of months the point belong to
81
var months = getQuarterMonths(point.index);
82
// create variable for counting the income in the months
83
var value = 0;
84
// summarize the income
85
for (var i=0;i<months.length;i++)
86
value+=series.getPoint(months[i]).get("value");
87
// display the income
88
return "Income: $" + value;
89
});
90
91
// set container id for the chart
92
chart.container("container");
93
94
// initiate chart drawing
95
chart.draw();
96
});
97
// find out an array a month belong to
98
function getQuarterMonths(month) {
99
var quarterStartMonth = 3 * Math.floor(month / 3);
100
return [quarterStartMonth, quarterStartMonth + 1, quarterStartMonth + 2];
101
}