HTMLcopy
1
<div id="container">
2
<div class="container-title">
3
<p>
4
See also
5
<a href="https://www.anychart.com/products/anystock/drawing_tools/" target="_blank">Drawing Tools and
6
Annotations Demo</a>
7
</p>
8
</div>
9
</div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
// The data that have been used for this sample can be taken from the CDN
3
// https://cdn.anychart.com/csv-data/msft-daily-short.js
4
// https://cdn.anychart.com/csv-data/orcl-daily-short.js
5
// https://cdn.anychart.com/csv-data/csco-daily-short.js
6
// https://cdn.anychart.com/csv-data/empty_data(2009-2010).js
7
8
// create data tables on loaded data
9
var msftDataTable = anychart.data.table();
10
msftDataTable.addData(get_msft_daily_short_data()); // eslint-disable-line no-undef
11
12
var orclDataTable = anychart.data.table();
13
orclDataTable.addData(get_orcl_daily_short_data()); // eslint-disable-line no-undef
14
15
var cscoDataTable = anychart.data.table();
16
cscoDataTable.addData(get_csco_daily_short_data()); // eslint-disable-line no-undef
17
cscoDataTable.addData(get_empty_data()); // eslint-disable-line no-undef
18
19
// create stock chart
20
var chart = anychart.stock();
21
22
// set chart title
23
chart.title(
24
'Annotated Combination of Multiple Line Chart and Candlestick Chart'
25
);
26
27
// set chart padding
28
chart.padding([15, 50, 57, 50]);
29
30
// create plot on the chart
31
var plot = chart.plot();
32
// grid settings
33
plot.yGrid(true).xGrid(true).xMinorGrid(true).yMinorGrid(true);
34
35
// format legend items
36
plot.legend().itemsFormatter(function (items) {
37
items.splice(3, 1);
38
// return items array
39
return items;
40
});
41
42
// create plot series with mapped data
43
var ORCL = plot.line(
44
orclDataTable.mapAs({
45
value: 4
46
})
47
);
48
ORCL.name('ORCL').stroke('#3C8AD8');
49
ORCL.tooltip().format(function () {
50
return tooltipFormatter(this, false);
51
});
52
53
var CSCO = plot.line(
54
cscoDataTable.mapAs({
55
value: 4
56
})
57
);
58
CSCO.name('CSCO').stroke('#96a6a6');
59
CSCO.tooltip().format(function () {
60
return tooltipFormatter(this, false);
61
});
62
63
var MSFT = plot.candlestick(
64
msftDataTable.mapAs({
65
open: 1,
66
high: 2,
67
low: 3,
68
close: 4
69
})
70
);
71
MSFT.name('MSFT');
72
MSFT.legendItem().iconType('rising-falling');
73
MSFT.tooltip().format(function () {
74
return tooltipFormatter(this, true);
75
});
76
77
var annotation = plot.annotations();
78
// create annotation horizontalLine
79
// bottom dashed line
80
annotation
81
.horizontalLine({
82
// X - part of the first anchor
83
xAnchor: '2008-08-31',
84
// Y - part of the first anchor
85
valueAnchor: 14.18,
86
// X - part of the second anchor
87
secondXAnchor: '2010-01-01',
88
// Y - part of the second anchor
89
secondValueAnchor: 14.18,
90
// set stroke settings
91
stroke: {
92
thickness: 1,
93
color: '#60727B',
94
dash: '10 15'
95
}
96
// disable interaction with Annotation
97
})
98
.allowEdit(false);
99
// create annotation line
100
// top dashed line
101
annotation
102
.line({
103
// X - part of the first anchor
104
xAnchor: '2009-03-01',
105
// Y - part of the first anchor
106
valueAnchor: 31,
107
// X - part of the second anchor
108
secondXAnchor: '2010-05-09',
109
// Y - part of the second anchor
110
secondValueAnchor: 31,
111
// set stroke settings
112
stroke: {
113
thickness: 1,
114
color: '#60727B',
115
dash: '10 15'
116
}
117
// disable interaction with Annotation
118
})
119
.allowEdit(false);
120
// create annotation line
121
// top solid line
122
annotation
123
.line({
124
// X - part of the first anchor
125
xAnchor: '2008-08-10',
126
// Y - part of the first anchor
127
valueAnchor: 28.5,
128
// X - part of the second anchor
129
secondXAnchor: '2010-01-03',
130
// Y - part of the second anchor
131
secondValueAnchor: 28.5,
132
stroke: '2 #6E9C4E'
133
// disable interaction with Annotation
134
})
135
.allowEdit(false);
136
// create annotation line
137
// MSTF annotations line
138
annotation
139
.line({
140
// X - part of the first anchor
141
xAnchor: '2008-08-10',
142
// Y - part of the first anchor
143
valueAnchor: 27.86,
144
// X - part of the second anchor
145
secondXAnchor: '2009-03-01',
146
// Y - part of the second anchor
147
secondValueAnchor: 15.96,
148
stroke: '2 #EF730E'
149
// disable interaction with Annotation
150
})
151
.allowEdit(false);
152
// create annotation line
153
// CSCO annotations line
154
annotation
155
.line({
156
// X - part of the first anchor
157
xAnchor: '2008-08-10',
158
// Y - part of the first anchor
159
valueAnchor: 24.91,
160
// X - part of the second anchor
161
secondXAnchor: '2009-03-01',
162
// Y - part of the second anchor
163
secondValueAnchor: 14.18,
164
stroke: '2 #96a6a6'
165
// disable interaction with Annotation
166
})
167
.allowEdit(false);
168
// create annotation line
169
// ORCL annotations line
170
annotation
171
.line({
172
// X - part of the first anchor
173
xAnchor: '2008-08-10',
174
// Y - part of the first anchor
175
valueAnchor: 23.06,
176
// X - part of the second anchor
177
secondXAnchor: '2009-03-01',
178
// Y - part of the second anchor
179
secondValueAnchor: 14.47,
180
stroke: '2 #3C8AD8'
181
// disable interaction with Annotation
182
})
183
.allowEdit(false);
184
// create annotation triangle
185
annotation
186
.triangle({
187
// X - part of the first anchor
188
xAnchor: '2009-06-28',
189
// Y - part of the first anchor
190
valueAnchor: 23.6,
191
// X - part of the second anchor
192
secondXAnchor: '2009-06-28',
193
// Y - part of the second anchor
194
secondValueAnchor: 18.5,
195
// X - part of the third anchor
196
thirdXAnchor: '2010-01-03',
197
// Y - part of the third anchor
198
thirdValueAnchor: 28.5,
199
// set stroke settings
200
stroke: '2 #6E9C4E 0.75',
201
// set fill settings
202
fill: '#6E9C4E 0.3'
203
// disable interaction with Annotation
204
})
205
.allowEdit(false);
206
// create annotation andrewsPitchfork
207
annotation.andrewsPitchfork({
208
// X - part of the first anchor
209
xAnchor: '2010-01-24',
210
// Y - part of the first anchor
211
valueAnchor: 29.5,
212
// X - part of the second anchor
213
secondXAnchor: '2010-01-03',
214
// Y - part of the second anchor
215
secondValueAnchor: 28.5,
216
// X - part of the third anchor
217
thirdXAnchor: '2010-03-14',
218
// Y - part of the third anchor
219
thirdValueAnchor: 28.5,
220
// set stroke settings
221
stroke: {
222
thickness: 2,
223
color: '#6E9C4E',
224
dash: '10 10'
225
},
226
// set hover stroke settings
227
hovered: {
228
stroke: {
229
thickness: 3,
230
color: 'green',
231
dash: '10 10'
232
}
233
}
234
});
235
236
// create scroller series with mapped data
237
chart.scroller().candlestick(
238
msftDataTable.mapAs({
239
open: 1,
240
high: 2,
241
low: 3,
242
close: 4
243
})
244
);
245
chart.tooltip().useHtml(true);
246
247
// set chart selected date/time range
248
chart.selectRange('2008-06-29', '2010-06-27');
249
250
// set container id for the chart
251
chart.container('container');
252
// initiate chart drawing
253
chart.draw();
254
255
// create range picker
256
var rangePicker = anychart.ui.rangePicker();
257
// init range picker
258
rangePicker.render(chart);
259
260
// create range selector
261
var rangeSelector = anychart.ui.rangeSelector();
262
// init range selector
263
rangeSelector.render(chart);
264
});
265
266
function tooltipFormatter(item, isOHLC) {
267
if (isOHLC) {
268
return (
269
'<br/><br/><span>' +
270
item.seriesName +
271
'</span><br/>' +
272
'<span style="color: #ccc">Open</span>: ' +
273
item.open +
274
'<br/><span style="color: #ccc">High</span>: ' +
275
item.high +
276
'<br/><span style="color: #ccc">Low</span>: ' +
277
item.low +
278
'<br/><span style="color: #ccc">Close</span>: ' +
279
item.close
280
);
281
}
282
if (item.seriesName === 'CSCO') {
283
var value = item.value;
284
if (isNaN(item.value)) {
285
value = 'undefined';
286
}
287
return (
288
'<br/><span style="color: #ccc">' +
289
item.seriesName +
290
'</span>: ' +
291
value +
292
'<br/>'
293
);
294
}
295
return (
296
'<span style="color: #ccc">' +
297
item.seriesName +
298
'</span>: ' +
299
item.value
300
);
301
}