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
183
1
anychart.onDocumentReady(function() {
2
3
// set stage
4
var stage = anychart.graphics.create("container");
5
6
// manage data
7
var data2005 = anychart.data.set([
8
["John", 10000, 12000, 20000, 16000],
9
["Jake", 12000, 15000, 19000, 21000],
10
["Peter", 16000, 17000, 21000, 22000]
11
]);
12
var data2006 = anychart.data.set([
13
["John", 13000, 10000, 18000, 21000],
14
["Jake", 11700, 12000, 21000, 20000],
15
["Peter", 18000, 11000, 12000, 22000]
16
]);
17
18
19
// create variables and count results
20
var results2005 = [];
21
for(var series2005=1;series2005<data2005.data()[0].length;series2005++){
22
var total = 0;
23
var values = [];
24
var bestResult = 0;
25
for(var points2005=0;points2005<data2005.mapAs().getRowsCount();points2005++){
26
total+= data2005.data()[points2005][series2005];
27
values.push(data2005.data()[points2005][series2005]);
28
if (Math.max.apply(Math, values) == data2005.data()[points2005][series2005])
29
bestResult = data2005.data()[points2005][0];
30
}
31
results2005.push({total: total, bestResult: bestResult});
32
}
33
34
var results2006 = [];
35
for(var n=1;n<data2006.data()[0].length;n++){
36
var total2006 = 0;
37
var values2006 = [];
38
var bestResult2006 = 0;
39
for(var r=0;r<data2006.mapAs().getRowsCount();r++){
40
total2006+= data2006.data()[r][n];
41
values2006.push(data2006.data()[r][n]);
42
if (Math.max.apply(Math, values2006) == data2006.data()[r][n])
43
bestResult2006 = data2006.data()[r][0];
44
}
45
results2006.push({total: total2006, max: bestResult2006});
46
}
47
48
// set charts
49
var chart2005 = anychart.column();
50
chart2005.title("2005 Sales");
51
52
// set data
53
chart2005.column(data2005.mapAs({x: [0], value: [1]}))
54
// set name
55
.name("Quarter 1")
56
// set data for legend tooltip content
57
.meta({legendText: "<b>Year:</b> 2005"+
58
"<br><b>Quarter</b> first"+
59
"<br><b>Total Sales:</b> "+results2005[0].total+
60
"<br><b>Best Result:</b> "+results2005[0].bestResult
61
});
62
chart2005.column(data2005.mapAs({x: [0], value: [2]}))
63
.name("Quarter 2")
64
.meta({legendText: "<b>Year:</b> 2005"+
65
"<br><b>Quarter</b> second"+
66
"<br><b>Total Sales:</b> "+results2005[1].total+
67
"<br><b>Best Result:</b> "+results2005[1].bestResult
68
});
69
chart2005.column(data2005.mapAs({x: [0], value: [3]}))
70
.name("Quarter 3")
71
.meta({legendText: "<b>Year:</b> 2005"+
72
"<br><b>Quarter</b> third"+
73
"<br><b>Total Sales:</b> "+results2005[2].total+
74
"<br><b>Best Result:</b> "+results2005[2].bestResult
75
});
76
chart2005.column(data2005.mapAs({x: [0], value: [4]}))
77
.name("Quarter 4")
78
.meta({legendText: "<b>Year:</b> 2005"+
79
"<br><b>Quarter</b> forth"+
80
"<br><b>Total Sales:</b> "+results2005[3].total+
81
"<br><b>Best Result:</b> "+results2005[3].bestResult
82
});
83
chart2005.padding().right(0).bottom(0);
84
chart2005.xAxis().title("Manager");
85
chart2005.yAxis().orientation("right").title().text("Sales").rotation(0);
86
87
// create second chart
88
var chart2006 = anychart.column();
89
90
// set title
91
chart2006.title("2006 Sales");
92
93
// set data
94
chart2006.column(data2006.mapAs({x: [0], value: [1]}))
95
// set name
96
.name("Quarter 1")
97
// set legend tooltip content
98
.meta({legendText: "<b>Year:</b> 2006"+
99
"<br><b>Quarter</b> first<br>"+
100
"<b>Total Sales:</b> "+results2006[0].total+
101
"<br><b>Best Result:</b> "+results2006[0].max
102
});
103
chart2006.column(data2006.mapAs({x: [0], value: [2]}))
104
.name("Quarter 2")
105
.meta({legendText: "<b>Year:</b> 2006"+
106
"<br><b>Quarter</b> second<br>"+
107
"<b>Total Sales:</b> "+results2006[1].total+
108
"<br><b>Best Result:</b> "+results2006[1].max
109
});
110
chart2006.column(data2006.mapAs({x: [0], value: [3]}))
111
.name("Quarter 3")
112
.meta({legendText: "<b>Year:</b> 2006"+
113
"<br><b>Quarter</b> third<br>"+
114
"<b>Total Sales:</b> "+results2006[2].total+
115
"<br><b>Best Result:</b> "+results2006[2].max
116
});
117
chart2006.column(data2006.mapAs({x: [0], value: [4]}))
118
.name("Quarter 4")
119
.meta({legendText: "<b>Year:</b> 2006"+
120
"<br><b>Quarter</b> forth<br>"+
121
"<b>Total Sales:</b> "+results2006[3].total+
122
"<br><b>Best Result:</b> "+results2006[3].max
123
});
124
chart2006.xAxis().title("Manager");
125
chart2006.padding().left(0).bottom(0);
126
127
// create custom legend
128
var legend = anychart.standalones.legend();
129
// enable legend
130
legend.enabled(true);
131
// set items source
132
legend.itemsSource([chart2005, chart2006]);
133
// legend title settings
134
var legendTitle = legend.title();
135
// enable title
136
legendTitle.enabled(true);
137
// title text
138
legendTitle.text("2005 - 2006 Sales");
139
// title alignment
140
legendTitle.hAlign("center");
141
142
// legend tooltip settings
143
legend.tooltip()
144
.useHtml(true) // enable using of html tags
145
.fontWeight(400); // set font weigh
146
147
// set legend tooltip content
148
legend.tooltip().format(function(){
149
return this.meta.legendText;
150
});
151
152
// draw legend
153
legend.container(stage).draw();
154
155
// manage charts size and position
156
chart2005.bounds(0,0,"55%","100%");
157
chart2006.bounds("55%",0,"45%","100%");
158
159
// prevent legend overlapping
160
chart2005.margin().bottom(stage.height() - legend.getRemainingBounds().getHeight());
161
chart2006.margin().bottom(stage.height() - legend.getRemainingBounds().getHeight());
162
163
// redraw legend every time the first chart is redrawn
164
chart2005.listen(
165
"chartDraw",
166
function (){
167
// define legend bounds
168
var legendBounds = anychart.math.rect(
169
0,
170
legend.getRemainingBounds().getHeight(),
171
chart2005.container().width(),
172
chart2005.container().height()
173
);
174
175
// set bounds and draw legend
176
legend.parentBounds(legendBounds).draw();
177
}
178
);
179
180
// draw charts
181
chart2005.container(stage).draw();
182
chart2006.container(stage).draw();
183
});