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
// create annotations
130
var annotation = plot.annotations();
131
// create a rectangle annotation
132
annotation.rectangle({
133
xAnchor: "2022-02-24",
134
valueAnchor: 3491,
135
secondXAnchor: "2022-06-04",
136
secondValueAnchor: 4637,
137
stroke: "3 #e65a37",
138
fill: "#e65a37 0.25"
139
});
140
// create a text label annotation
141
annotation.label()
142
.xAnchor("2022-02-24")
143
.valueAnchor(4900)
144
.anchor("left-top")
145
.offsetY(5)
146
.padding(6)
147
.text("First 100 days since Ukraine was attacked")
148
.fontColor("#fff")
149
.background({
150
fill: "#e65a37 0.75",
151
stroke: "0.5 #2d2d2d",
152
corners: 2
153
});
154
155
// create a kdj indicator plot
156
var kdjPlot = chart.plot(1);
157
// map the necessary data for the kdj calculation
158
var kdjMapping = dataTable.mapAs({
159
high: 2,
160
low: 3,
161
close: 4
162
});
163
// create a kdj indicator
164
var kdj = kdjPlot.kdj(kdjMapping, 14, 3, 3);
165
// (default values: 14 periods for %k, 3 periods for %d, 3 periods for %j)
166
kdj.kSeries().stroke("purple"); // customize the %k line color
167
kdj.dSeries().stroke("green"); // customize the %d line color
168
kdj.jSeries().stroke("orange"); // customize the %j line color
169
// set the kdj plot's height
170
kdjPlot.height("20%");
171
172
// add the volume data mapping
173
var volumeMapping = dataTable.mapAs({
174
"value": 5
175
});
176
177
// set the date/time range displayed by default
178
chart.selectRange("2021-03-01", "2023-08-20");
179
180
// set the chart title
181
chart.title("S&P 500 OHLC Chart");
182
183
// set the container id for the chart
184
chart.container("container");
185
186
// initiate the chart drawing
187
chart.draw();
188
189
});
190
191
});