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
// add the volume data mapping
20
var volumeMapping = dataTable.mapAs({
21
"value": 5
22
});
23
24
// create a stock chart
25
var chart = anychart.stock();
26
27
// create the chart plot
28
var plot = chart.plot(0);
29
30
// create an ohlc series and bind it to the mapped data
31
var ohlcSeries = plot.ohlc(mapping);
32
33
// set the chart background color
34
chart.background().fill("#edf4f5");
35
36
// add the volume data as the scroller series
37
chart.scroller().line(volumeMapping);
38
39
// customize the color of the ohlc bars
40
ohlcSeries.fallingFill("#ff0d0d");
41
ohlcSeries.fallingStroke("#ff0d0d");
42
ohlcSeries.risingFill("#43ff43");
43
ohlcSeries.risingStroke("#43ff43");
44
45
// additional customization for the ohlc series
46
ohlcSeries.normal().risingStroke("#33ccff");
47
ohlcSeries.hovered().risingStroke("#33ccff", 1.5);
48
ohlcSeries.selected().risingStroke("#33ccff", 3);
49
ohlcSeries.normal().fallingStroke("#ff33cc");
50
ohlcSeries.hovered().fallingStroke("#ff33cc", 1.5);
51
ohlcSeries.selected().fallingStroke("#ff33cc", 3);
52
53
// name the ohlc series
54
ohlcSeries.name("S&P");
55
56
// customize the legend item for the ohlc series
57
// to display a rising/falling icon
58
ohlcSeries.legendItem().iconType("rising-falling");
59
60
// create a date range picker
61
var rangePicker = anychart.ui.rangePicker();
62
rangePicker.render(chart);
63
64
// create a date range selector
65
var rangeSelector = anychart.ui.rangeSelector();
66
rangeSelector.render(chart);
67
68
// add event markers
69
plot.eventMarkers({
70
"groups": [
71
{
72
"data": [
73
{
74
"symbol": "1",
75
"date": "2020-03-11",
76
"description": "The WHO declares COVID-19 a global pandemic",
77
"normal": {
78
"type": "rect", "width": 40,
79
"fill": "#ead9d1", "stroke": "2 #990033",
80
"fontColor": "#990033", "fontWeight": 600,
81
"connector": { "stroke": "2 #990033" }
82
}
83
},
84
{
85
"symbol": "2",
86
"date": "2021-03-08",
87
"description": "The first COVID-19 vaccine is approved for use in the U.S.",
88
"normal": {
89
"type": "circle",
90
"fill": "#d1ead9", "stroke": "2 #009933",
91
"fontColor": "#009933", "fontWeight": 600,
92
"connector": { "stroke": "2 #009933" }
93
}
94
},
95
{
96
"symbol": "3",
97
"date": "2022-02-24",
98
"description": "Russia starts a military operation in Ukraine",
99
"normal": {
100
"type": "rect", "width": 40,
101
"fill": "#ead9d1", "stroke": "2 #990033",
102
"fontColor": "#990033", "fontWeight": 600,
103
"connector": { "stroke": "2 #990033" }
104
}
105
},
106
{
107
"symbol": "4",
108
"date": "2023-05-03",
109
"description": "The Federal Reserve announces a rate hike to combat inflation, exceeding 5%",
110
"normal": {
111
"type": "circle",
112
"fill": "#d1ead9", "stroke": "2 #009933",
113
"fontColor": "#009933", "fontWeight": 600,
114
"connector": { "stroke": "2 #009933" }
115
},
116
"hovered": {
117
"fill": "white", "stroke": "2 #990033",
118
"fontColor": "#990033",
119
"connector": { "stroke": "2 #990033" }
120
},
121
"selected": {
122
"fill": "white", "stroke": "2 #4d1a00",
123
"fontColor": "#4d1a00",
124
"connector": { "stroke": "2 #4d1a00" }
125
}
126
},
127
]
128
}
129
]
130
});
131
132
// set the event marker symbol
133
plot.eventMarkers().format(function () {
134
return this.getData("symbol");
135
});
136
137
// create annotations
138
var annotation = plot.annotations();
139
// create a rectangle annotation
140
annotation.rectangle({
141
xAnchor: "2022-02-24",
142
valueAnchor: 3491,
143
secondXAnchor: "2022-06-04",
144
secondValueAnchor: 4637,
145
stroke: "3 #e65a37",
146
fill: "#e65a37 0.25"
147
});
148
// create a text label annotation
149
annotation.label()
150
.xAnchor("2022-02-24")
151
.valueAnchor(4900)
152
.anchor("left-top")
153
.offsetY(5)
154
.padding(6)
155
.text("First 100 days since Ukraine was attacked")
156
.fontColor("#fff")
157
.background({
158
fill: "#e65a37 0.75",
159
stroke: "0.5 #2d2d2d",
160
corners: 2
161
});
162
163
// create a kdj indicator plot
164
var kdjPlot = chart.plot(1);
165
// map the necessary data for the kdj calculation
166
var kdjMapping = dataTable.mapAs({
167
high: 2,
168
low: 3,
169
close: 4
170
});
171
// create a kdj indicator
172
var kdj = kdjPlot.kdj(kdjMapping, 14, 3, 3);
173
// (default values: 14 periods for %k, 3 periods for %d, 3 periods for %j)
174
kdj.kSeries().stroke("purple"); // customize the %k line color
175
kdj.dSeries().stroke("green"); // customize the %d line color
176
kdj.jSeries().stroke("orange"); // customize the %j line color
177
// set the kdj plot's height
178
kdjPlot.height("20%");
179
180
// set the date/time range displayed by default
181
chart.selectRange("2021-03-01", "2023-08-20");
182
183
// set the chart title
184
chart.title("S&P 500 OHLC Chart");
185
186
// set the container id for the chart
187
chart.container("container");
188
189
// initiate the chart drawing
190
chart.draw();
191
192
});
193
194
});