Skip to content

Commit 30cb91d

Browse files
benmccannsimonbrunel
authored andcommitted
Demonstrate multiple units on financial timeseries sample (#6119)
1 parent 07fae61 commit 30cb91d

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

samples/scales/time/financial.html

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,41 @@
2626
<option value="line">Line</option>
2727
<option value="bar">Bar</option>
2828
</select>
29+
<select id="unit">
30+
<option value="second">Second</option>
31+
<option value="minute">Minute</option>
32+
<option value="hour">Hour</option>
33+
<option value="day" selected>Day</option>
34+
<option value="month">Month</option>
35+
<option value="year">Year</option>
36+
</select>
2937
<button id="update">update</button>
3038
<script>
31-
function randomNumber(min, max) {
32-
return Math.random() * (max - min) + min;
33-
}
39+
function generateData() {
40+
function randomNumber(min, max) {
41+
return Math.random() * (max - min) + min;
42+
}
3443

35-
function randomBar(date, lastClose) {
36-
var open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
37-
var close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
38-
return {
39-
t: date.valueOf(),
40-
y: close
41-
};
42-
}
44+
function randomBar(date, lastClose) {
45+
var open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
46+
var close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
47+
return {
48+
t: date.valueOf(),
49+
y: close
50+
};
51+
}
4352

44-
var dateFormat = 'MMMM DD YYYY';
45-
var date = moment('April 01 2017', dateFormat);
46-
var data = [randomBar(date, 30)];
47-
while (data.length < 60) {
48-
date = date.clone().add(1, 'd');
49-
if (date.isoWeekday() <= 5) {
50-
data.push(randomBar(date, data[data.length - 1].y));
53+
var date = moment('Jan 01 1990', 'MMM DD YYYY');
54+
var now = moment();
55+
var data = [randomBar(date, 30)];
56+
var unit = document.getElementById('unit').value;
57+
for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit)) {
58+
if (date.isoWeekday() <= 5) {
59+
data.push(randomBar(date, data[data.length - 1].y));
60+
}
5161
}
62+
63+
return data;
5264
}
5365

5466
var ctx = document.getElementById('chart1').getContext('2d');
@@ -63,7 +75,7 @@
6375
label: 'CHRT - Chart.js Corporation',
6476
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
6577
borderColor: window.chartColors.red,
66-
data: data,
78+
data: generateData(),
6779
type: 'line',
6880
pointRadius: 0,
6981
fill: false,
@@ -109,7 +121,9 @@
109121

110122
document.getElementById('update').addEventListener('click', function() {
111123
var type = document.getElementById('type').value;
112-
chart.config.data.datasets[0].type = type;
124+
var dataset = chart.config.data.datasets[0];
125+
dataset.type = type;
126+
dataset.data = generateData();
113127
chart.update();
114128
});
115129

0 commit comments

Comments
 (0)