Skip to content

Commit 0ac215b

Browse files
benmccannsimonbrunel
authored andcommitted
Improve financial sample tooltips and interactions (#6089)
1 parent 653e9a9 commit 0ac215b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

samples/scales/time/financial.html

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
}
3434

3535
function randomBar(date, lastClose) {
36-
var open = randomNumber(lastClose * 0.95, lastClose * 1.05);
37-
var close = randomNumber(open * 0.95, open * 1.05);
36+
var open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
37+
var close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
3838
return {
3939
t: date.valueOf(),
4040
y: close
@@ -44,12 +44,10 @@
4444
var dateFormat = 'MMMM DD YYYY';
4545
var date = moment('April 01 2017', dateFormat);
4646
var data = [randomBar(date, 30)];
47-
var labels = [date];
4847
while (data.length < 60) {
4948
date = date.clone().add(1, 'd');
5049
if (date.isoWeekday() <= 5) {
5150
data.push(randomBar(date, data[data.length - 1].y));
52-
labels.push(date);
5351
}
5452
}
5553

@@ -61,7 +59,6 @@
6159
var cfg = {
6260
type: 'bar',
6361
data: {
64-
labels: labels,
6562
datasets: [{
6663
label: 'CHRT - Chart.js Corporation',
6764
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
@@ -80,7 +77,8 @@
8077
type: 'time',
8178
distribution: 'series',
8279
ticks: {
83-
source: 'labels'
80+
source: 'data',
81+
autoSkip: true
8482
}
8583
}],
8684
yAxes: [{
@@ -89,9 +87,24 @@
8987
labelString: 'Closing price ($)'
9088
}
9189
}]
90+
},
91+
tooltips: {
92+
intersect: false,
93+
mode: 'index',
94+
callbacks: {
95+
label: function(tooltipItem, myData) {
96+
var label = myData.datasets[tooltipItem.datasetIndex].label || '';
97+
if (label) {
98+
label += ': ';
99+
}
100+
label += parseFloat(tooltipItem.value).toFixed(2);
101+
return label;
102+
}
103+
}
92104
}
93105
}
94106
};
107+
95108
var chart = new Chart(ctx, cfg);
96109

97110
document.getElementById('update').addEventListener('click', function() {

0 commit comments

Comments
 (0)