HTMLcopy
1
<div id="container"></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
3
// load stock data:
4
// for apple
5
anychart.data.loadCsvFile(
6
"https://cdn.anychart.com/csv-data/AAPL.csv",
7
function (appleData) {
8
// for microsoft
9
anychart.data.loadCsvFile(
10
"https://cdn.anychart.com/csv-data/MSFT.csv",
11
function (msData) {
12
// for alphabet (google)
13
anychart.data.loadCsvFile(
14
"https://cdn.anychart.com/csv-data/GOOG.csv",
15
function (googleData) {
16
17
// create data tables:
18
// for apple
19
let appleDataTable = anychart.data.table();
20
appleDataTable.addData(appleData);
21
// for microsoft
22
let msftDataTable = anychart.data.table();
23
msftDataTable.addData(msData);
24
// for alphabet (google)
25
let googleDataTable = anychart.data.table();
26
googleDataTable.addData(googleData);
27
28
// create a stock chart
29
let chart = anychart.stock();
30
31
// create a stock plot with three line series
32
let plot = chart.plot(0);
33
plot
34
.line()
35
.name("AAPL")
36
.data(appleDataTable.mapAs({ value: 4 }))
37
.stroke("1.5 #7CC7FF");
38
plot
39
.line()
40
.name("MSFT")
41
.data(msftDataTable.mapAs({ value: 4 }))
42
.stroke("1.5 #3498DB");
43
plot
44
.line()
45
.name("GOOG")
46
.data(googleDataTable.mapAs({ value: 4 }))
47
.stroke("1.5 #0000DD");
48
49
// create annotations:
50
// access the object
51
let controller = plot.annotations();
52
// create a marker annotation
53
let marker = controller.marker({
54
xAnchor: "2021-11-11",
55
valueAnchor: 368,
56
markerType: 'arrow-down',
57
size: 20,
58
offsetY: 0,
59
offsetX: 0
60
});
61
// create a label annotation
62
controller
63
.label()
64
.xAnchor("2021-09-01")
65
.valueAnchor(374)
66
.anchor("right-top")
67
.offsetY(5)
68
.padding(6)
69
.text("Record highs after 20 months of the pandemic")
70
.fontColor("#666")
71
.background({
72
fill: "#fff 0.65",
73
stroke: "0.5 #c20000",
74
corners: 2
75
});
76
77
// create event markers:
78
// access the object
79
let eventMarkers = plot.eventMarkers();
80
// set the symbol
81
plot.eventMarkers().format(function () {
82
return this.getData("symbol");
83
});
84
// set the data
85
eventMarkers.data([
86
{
87
symbol: "1",
88
date: "2020-03-11",
89
description: "COVID-19 declared a global pandemic by the WHO"
90
},
91
{
92
symbol: "2",
93
date: "2022-02-24",
94
description: "Ukraine attacked by Russia"
95
},
96
{
97
symbol: "3",
98
date: "2022-05-10",
99
description: "Start of the 2022 crypto winter"
100
}
101
]);
102
103
// create a scroller series with msft data
104
chart.scroller().area(msftDataTable.mapAs({ value: 4 }));
105
106
// create a date range ui:
107
// picker
108
let rangePicker = anychart.ui.rangePicker();
109
rangePicker.render(chart);
110
// selector
111
let rangeSelector = anychart.ui.rangeSelector();
112
rangeSelector.render(chart);
113
chart.selectRange("2017-01-01", "2023-09-01");
114
115
// set the stock chart title
116
chart
117
.title()
118
.enabled(true)
119
.useHtml(true)
120
.text(
121
'<span style = "color: #111; font-size:18px; margin-bottom:15px; dy:20px">Decade of Tech Titan Stocks</span>' +
122
'<br/><span style="color:grey; font-size: 14px; margin-bottom:10px;">Comparing <span style="color:#7CC7FF;">Apple</span>, <span style="color:#3498DB;">Microsoft</span>, and <span style="color:#0000DD;">Alphabet</span> stock prices from 2013 to 2023</span>'
123
);
124
125
// set the stock chart container id
126
chart.container("container");
127
128
// initiate the stock chart drawing
129
chart.draw();
130
131
}
132
);
133
}
134
);
135
}
136
);
137
});