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
262
1
anychart.onDocumentReady(function() {
2
3
// set stage
4
var stage = anychart.graphics.create("container");
5
6
// data
7
var data = anychart.data.set([
8
["John", 10000, 12000, 20000, 16000],
9
["Jake", 12000, 15000, 19000, 21000],
10
["Peter", 16000, 17000, 21000, 22000]
11
]);
12
13
// get default color palette
14
var palette = anychart.palettes.distinctColors().items();
15
16
// variables for legend items
17
var johnLegendItems = [];
18
var jakeLegendItems = [];
19
var peterLegendItems = [];
20
21
// push settings into arrays of legend items
22
for (var i0=1;i0<data.data()[0].length;i0++){
23
johnLegendItems.push({
24
text: data.data()[0][i0].toString(), // text of item
25
iconType: "square", // type of icons
26
iconFill: palette[i0-1], // color of icons
27
iconStroke: anychart.color.darken(palette[i0-1], 0.2)
28
});
29
}
30
for (var i1=1;i1<data.data()[1].length;i1++){
31
jakeLegendItems.push({
32
text: data.data()[1][i1].toString(), // text of item
33
iconType: "square", // type of icons
34
iconFill: palette[i1-1], // color of icons
35
iconStroke: anychart.color.darken(palette[i1-1], 0.2)
36
});
37
}
38
for (var i2=1;i2<data.data()[2].length;i2++){
39
peterLegendItems.push({
40
index: i2,
41
text: data.data()[2][i2].toString(), // text of item
42
iconType: "square", // type of icons
43
iconFill: palette[i2-1], // color of icons
44
iconStroke: anychart.color.darken(palette[i2-1], 0.2)
45
});
46
}
47
48
// custom legends
49
var johnLegend = anychart.standalones.legend();
50
var jakeLegend = anychart.standalones.legend();
51
var peterLegend = anychart.standalones.legend();
52
53
// set legend titles
54
var johnTitle = johnLegend.title();
55
johnTitle.text("John");
56
johnTitle.hAlign("center");
57
var jakeTitle = jakeLegend.title();
58
jakeTitle.text("Jake");
59
jakeTitle.hAlign("center");
60
var peterTitle = peterLegend.title();
61
peterTitle.text("Peter");
62
peterTitle.hAlign("center");
63
64
// set legend items
65
johnLegend.itemsFormatter(function(){return johnLegendItems});
66
jakeLegend.itemsFormatter(function(){return jakeLegendItems});
67
peterLegend.itemsFormatter(function(){return peterLegendItems});
68
69
// legends settings
70
johnLegend.position("bottom");
71
johnLegend.align("left");
72
johnLegend.zIndex(1);
73
johnLegend.enabled(true);
74
johnLegend.itemsLayout("vertical");
75
johnLegend.listen("legendItemClick", function(event) {
76
event.preventDefault();
77
var index = event["itemIndex"];
78
if (chart.getSeries(index).data().get(0, "value") == "missing"){
79
chart.getSeries(index).data().set(0, "value", johnLegendItems[index].text);
80
johnLegendItems[index].fontColor = "#000";
81
johnLegendItems[index].iconFill = palette[index];
82
johnLegendItems[index].iconStroke = anychart.color.darken(palette[index], 0.2);
83
johnLegend.itemsFormatter(function(){return johnLegendItems});
84
}
85
else{
86
chart.getSeries(index).data().set(0, "value", "missing");
87
johnLegendItems[index].fontColor = "#999";
88
johnLegendItems[index].iconFill = "#999";
89
johnLegendItems[index].iconStroke = "#999";
90
johnLegend.itemsFormatter(function(){return johnLegendItems});
91
}
92
});
93
johnLegend.listen("legendItemMouseOver", function(event) {
94
chart.getSeries(event.itemIndex).hover(0);
95
});
96
johnLegend.listen("legendItemMouseOut", function(event) {
97
chart.getSeries(event.itemIndex).unhover(0);
98
});
99
100
jakeLegend.position("bottom");
101
jakeLegend.align("center");
102
jakeLegend.zIndex(1);
103
jakeLegend.enabled(true);
104
jakeLegend.itemsLayout("vertical");
105
jakeLegend.listen("legendItemClick", function(event) {
106
event.preventDefault();
107
var index = event["itemIndex"];
108
if (chart.getSeries(index).data().get(1, "value") == "missing"){
109
chart.getSeries(index).data().set(1, "value", jakeLegendItems[index].text);
110
jakeLegendItems[index].fontColor = "#000";
111
jakeLegendItems[index].iconFill = palette[index];
112
jakeLegendItems[index].iconStroke = anychart.color.darken(palette[index], 0.2);
113
jakeLegend.itemsFormatter(function(){return jakeLegendItems});
114
}
115
else{
116
chart.getSeries(index).data().set(1, "value", "missing");
117
jakeLegendItems[index].fontColor = "#999";
118
jakeLegendItems[index].iconFill = "#999";
119
jakeLegendItems[index].iconStroke = "#999";
120
jakeLegend.itemsFormatter(function(){return jakeLegendItems});
121
}
122
});
123
jakeLegend.listen("legendItemMouseOver", function(event) {
124
chart.getSeries(event.itemIndex).hover(1);
125
});
126
jakeLegend.listen("legendItemMouseOut", function(event) {
127
chart.getSeries(event.itemIndex).unhover(1);
128
});
129
130
peterLegend.position("bottom");
131
peterLegend.align("right");
132
peterLegend.zIndex(1);
133
peterLegend.enabled(true);
134
peterLegend.itemsLayout("vertical");
135
peterLegend.listen("legendItemClick", function(event) {
136
event.preventDefault();
137
var index = event["itemIndex"];
138
if (chart.getSeries(index).data().get(2, "value") == "missing"){
139
chart.getSeries(index).data().set(2, "value", peterLegendItems[index].text);
140
peterLegendItems[index].fontColor = "#000";
141
peterLegendItems[index].iconFill = palette[index];
142
peterLegendItems[index].iconStroke = anychart.color.darken(palette[index], 0.2);
143
peterLegend.itemsFormatter(function(){return peterLegendItems});
144
}
145
else{
146
chart.getSeries(index).data().set(2, "value", "missing");
147
peterLegendItems[index].fontColor = "#999";
148
peterLegendItems[index].iconFill = "#999";
149
peterLegendItems[index].iconStroke = "#999";
150
peterLegend.itemsFormatter(function(){return peterLegendItems});
151
}
152
});
153
peterLegend.listen("legendItemMouseOver", function(event) {
154
chart.getSeries(event.itemIndex).hover(2);
155
});
156
peterLegend.listen("legendItemMouseOut", function(event) {
157
chart.getSeries(event.itemIndex).unhover(2);
158
});
159
160
// draw legends
161
johnLegend.container(stage);
162
johnLegend.draw();
163
jakeLegend.container(stage);
164
jakeLegend.draw();
165
peterLegend.container(stage);
166
peterLegend.draw();
167
168
169
// map data
170
var Quarter1 = data.mapAs({x: [0], value: [1]});
171
var Quarter2 = data.mapAs({x: [0], value: [2]});
172
var Quarter3 = data.mapAs({x: [0], value: [3]});
173
var Quarter4 = data.mapAs({x: [0], value: [4]});
174
175
// chart type
176
var chart = anychart.column();
177
178
var xAxis = chart.xAxis();
179
xAxis.title(null);
180
var yAxis = chart.yAxis();
181
yAxis.title(null);
182
183
// set title
184
chart.title("Custom Legend");
185
186
// set data
187
var series1 = chart.column(Quarter1);
188
series1.name("Quarter 1");
189
var series2 = chart.column(Quarter2);
190
series2.name("Quarter 2");
191
var series3 = chart.column(Quarter3);
192
series3.name("Quarter 3");
193
var series4 = chart.column(Quarter4);
194
series4.name("Quarter 4");
195
196
// legend settings
197
var legend = chart.legend();
198
// draw line to separate title
199
legend.titleSeparator(true);
200
// enable legend
201
legend.enabled(true);
202
// set legend position
203
legend.position("right");
204
// set legend align
205
legend.align("top");
206
// set items layout
207
legend.itemsLayout("vertical");
208
209
// disable chart background
210
chart.background(null);
211
212
// settings for legend title
213
var legendTitle = chart.legend().title();
214
legendTitle.useHtml(true);
215
// enables legend title
216
legendTitle.enabled(true);
217
// set title text
218
legendTitle.text("Total sales<br><i style=\"color: #999; font-weight: 400; font-size: 11px;\">(Year 2004)</i>");
219
// set font size
220
legendTitle.fontSize(14);
221
legendTitle.hAlign("center");
222
223
// prevent chart overlapping by legends
224
var margin = chart.margin();
225
margin.bottom(stage.height() - johnLegend.getRemainingBounds().getHeight() - 10);
226
var yScale = chart.yScale();
227
yScale.minimum(0);
228
// redraw legends if chart is redrawn
229
chart.container(stage);
230
chart.draw();
231
chart.listen(
232
"chartDraw",
233
function (){
234
johnLegend.parentBounds(
235
anychart.math.rect(
236
0,
237
0,
238
chart.container().width(),
239
chart.container().height()
240
)
241
);
242
johnLegend.draw();
243
jakeLegend.parentBounds(
244
anychart.math.rect(
245
0,
246
0,
247
chart.container().width(),
248
chart.container().height()
249
)
250
);
251
jakeLegend.draw();
252
peterLegend.parentBounds(
253
anychart.math.rect(
254
0,
255
0,
256
chart.container().width(),
257
chart.container().height()
258
)
259
);
260
peterLegend.draw();
261
});
262
});