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 csv data
4
anychart.data.loadCsvFile("https://gist.githubusercontent.com/awanshrestha/f234904bcc41b38dc3e6cb98ee563777/raw/c033b77d9ae698b322a6446befee4a7ed88be10e/SandP500Final.csv", function (data) {
5
6
// create a data table
7
var dataTable = anychart.data.table();
8
dataTable.addData(data);
9
10
// map the columns from the data table into the ohlc format
11
var mapping = dataTable.mapAs({
12
date: 0,
13
open: 1,
14
high: 2,
15
low: 3,
16
close: 4
17
});
18
19
// create a stock chart
20
var chart = anychart.stock();
21
22
// create the chart plot
23
var plot = chart.plot(0);
24
25
// create an ohlc series and bind it to the mapped data
26
var ohlcSeries = plot.ohlc(mapping);
27
28
// set the chart background color
29
chart.background().fill("#edf4f5");
30
31
// customize the color of the ohlc bars
32
ohlcSeries.fallingFill("#ff0d0d");
33
ohlcSeries.fallingStroke("#ff0d0d");
34
ohlcSeries.risingFill("#43ff43");
35
ohlcSeries.risingStroke("#43ff43");
36
37
// additional customization for the ohlc series
38
ohlcSeries.normal().risingStroke("#33ccff");
39
ohlcSeries.hovered().risingStroke("#33ccff", 1.5);
40
ohlcSeries.selected().risingStroke("#33ccff", 3);
41
ohlcSeries.normal().fallingStroke("#ff33cc");
42
ohlcSeries.hovered().fallingStroke("#ff33cc", 1.5);
43
ohlcSeries.selected().fallingStroke("#ff33cc", 3);
44
45
// name the ohlc series
46
ohlcSeries.name("S&P");
47
48
// customize the legend item for the ohlc series
49
// to display a rising/falling icon
50
ohlcSeries.legendItem().iconType("rising-falling");
51
52
// create a date range picker
53
var rangePicker = anychart.ui.rangePicker();
54
rangePicker.render(chart);
55
56
// create a date range selector
57
var rangeSelector = anychart.ui.rangeSelector();
58
rangeSelector.render(chart);
59
60
// add event markers
61
plot.eventMarkers({
62
"groups": [
63
{
64
"data": [
65
{
66
"symbol": "1",
67
"date": "2020-03-11",
68
"description": "The WHO declares COVID-19 a global pandemic",
69
"normal": {
70
"type": "rect", "width": 40,
71
"fill": "#ead9d1", "stroke": "2 #990033",
72
"fontColor": "#990033", "fontWeight": 600,
73
"connector": { "stroke": "2 #990033" }
74
}
75
},
76
{
77
"symbol": "2",
78
"date": "2021-03-08",
79
"description": "The first COVID-19 vaccine is approved for use in the U.S.",
80
"normal": {
81
"type": "circle",
82
"fill": "#d1ead9", "stroke": "2 #009933",
83
"fontColor": "#009933", "fontWeight": 600,
84
"connector": { "stroke": "2 #009933" }
85
}
86
},
87
{
88
"symbol": "3",
89
"date": "2022-02-24",
90
"description": "Russia starts a military operation in Ukraine",
91
"normal": {
92
"type": "rect", "width": 40,
93
"fill": "#ead9d1", "stroke": "2 #990033",
94
"fontColor": "#990033", "fontWeight": 600,
95
"connector": { "stroke": "2 #990033" }
96
}
97
},
98
{
99
"symbol": "4",
100
"date": "2023-05-03",
101
"description": "The Federal Reserve announces a rate hike to combat inflation, exceeding 5%",
102
"normal": {
103
"type": "circle",
104
"fill": "#d1ead9", "stroke": "2 #009933",
105
"fontColor": "#009933", "fontWeight": 600,
106
"connector": { "stroke": "2 #009933" }
107
},
108
"hovered": {
109
"fill": "white", "stroke": "2 #990033",
110
"fontColor": "#990033",
111
"connector": { "stroke": "2 #990033" }
112
},
113
"selected": {
114
"fill": "white", "stroke": "2 #4d1a00",
115
"fontColor": "#4d1a00",
116
"connector": { "stroke": "2 #4d1a00" }
117
}
118
},
119
]
120
}
121
]
122
});
123
124
// set the event marker symbol
125
plot.eventMarkers().format(function () {
126
return this.getData("symbol");
127
});
128
129
// set the date/time range displayed by default
130
chart.selectRange("2021-03-01", "2023-08-20");
131
132
// set the chart title
133
chart.title("S&P 500 OHLC Chart");
134
135
// set the container id for the chart
136
chart.container("container");
137
138
// initiate the chart drawing
139
chart.draw();
140
141
});
142
143
});