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 data
4
anychart.data.loadCsvFile(
5
'https://gist.githubusercontent.com/awanshrestha/6481638c175e82dc6a91a499a8730057/raw/1791ef1f55f0be268479286284a0452129371003/TSMC.csv',
6
function (data) {
7
8
// create a data table on the loaded data
9
var dataTable = anychart.data.table();
10
dataTable.addData(data);
11
12
// map the loaded data for the candlestick series
13
var mapping = dataTable.mapAs({
14
open: 1,
15
high: 2,
16
low: 3,
17
close: 4
18
});
19
20
// create a stock chart
21
var chart = anychart.stock();
22
23
// change the color theme
24
anychart.theme('darkGlamour');
25
26
// create the chart plot
27
var plot = chart.plot(0);
28
29
// set the grid settings
30
plot.yGrid(true).xGrid(true).yMinorGrid(true).xMinorGrid(true);
31
32
// create the candlestick series
33
var series = plot.candlestick(mapping);
34
series.name('TSMC');
35
series.legendItem().iconType('rising-falling');
36
37
// create a range picker
38
var rangePicker = anychart.ui.rangePicker();
39
rangePicker.render(chart);
40
41
// create a range selector
42
var rangeSelector = anychart.ui.rangeSelector();
43
rangeSelector.render(chart);
44
45
// modify the color of the candlesticks
46
series.fallingFill("#FF0D0D");
47
series.fallingStroke("#FF0D0D");
48
series.risingFill("#43FF43");
49
series.risingStroke("#43FF43");
50
51
// set the event markers
52
var eventMarkers = plot.eventMarkers();
53
54
// set the symbol of the event markers
55
plot.eventMarkers().format(function() {
56
return this.getData("symbol");
57
});
58
59
// set the event markers data
60
eventMarkers.data([
61
{ "symbol": 1, date: '2020-03-11', description: 'WHO declares COVID-19 a global pandemic' },
62
{ "symbol": 2, date: '2020-11-20', description: 'TSMC wins approval from Arizona for $12 billion chip plant' },
63
{ "symbol": 3, date: '2021-07-12', description: 'TSMC and Foxconn reach deals to buy COVID-19 vaccines for Taiwan' },
64
{ "symbol": 4, date: '2021-11-09', description: 'TSMC announces to build a specialty technology fab in Japan with Sony' },
65
{ "symbol": 5, date: '2022-02-24', description: 'Russia-Ukraine war begins' },
66
{ "symbol": 6, date: '2022-11-15', description: 'Berkshire Hathaway discloses a $4.1 billion stake in TSMC' },
67
68
]);
69
70
// create an annotation
71
var annotation = plot.annotations();
72
73
// create a rectangle
74
annotation.rectangle({
75
// X - part of the first anchor
76
xAnchor: '2020-03-11',
77
// Y - part of the first anchor
78
valueAnchor: 45,
79
// X - part of the second anchor
80
secondXAnchor: '2020-12-31',
81
// Y - part of the second anchor
82
secondValueAnchor: 125,
83
// set the stroke settings
84
stroke: '3 #098209',
85
// set the fill settings
86
fill: '#43FF43 0.25'
87
});
88
89
// create a text label
90
annotation
91
.label()
92
.xAnchor('2020-03-11')
93
.valueAnchor(75)
94
.anchor('left-top')
95
.offsetY(5)
96
.padding(6)
97
.text('Global Lockdowns — Rise in demand for semiconductor chips')
98
.fontColor('#fff')
99
.background({
100
fill: '#098209 0.75',
101
stroke: '0.5 #098209',
102
corners: 2
103
});
104
105
// add a second plot to show macd values
106
var indicatorPlot = chart.plot(1);
107
108
// map the macd values
109
var macdIndicator = indicatorPlot.macd(mapping);
110
111
// set the histogram series
112
macdIndicator.histogramSeries('area');
113
macdIndicator
114
.histogramSeries().normal().fill('green .3').stroke('green');
115
macdIndicator
116
.histogramSeries().normal().negativeFill('red .3').negativeStroke('red');
117
118
// set the second plot's height
119
indicatorPlot.height('30%');
120
121
// set the chart display for the selected date/time range
122
chart.selectRange('2020-01-01', '2022-12-31');
123
124
// set the title of the chart
125
chart.title('TSMC Stock Chart');
126
127
// set the container id for the chart
128
chart.container('container');
129
130
// initiate the chart drawing
131
chart.draw();
132
}
133
);
134
});
135