Skip to content

Commit

Permalink
Improve financial sample tooltips and interactions (chartjs#6089)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Mar 3, 2019
1 parent f49c9ea commit 1d6d5c8
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions samples/scales/time/financial.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
}

function randomBar(date, lastClose) {
var open = randomNumber(lastClose * 0.95, lastClose * 1.05);
var close = randomNumber(open * 0.95, open * 1.05);
var open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
var close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
return {
t: date.valueOf(),
y: close
Expand All @@ -44,12 +44,10 @@
var dateFormat = 'MMMM DD YYYY';
var date = moment('April 01 2017', dateFormat);
var data = [randomBar(date, 30)];
var labels = [date];
while (data.length < 60) {
date = date.clone().add(1, 'd');
if (date.isoWeekday() <= 5) {
data.push(randomBar(date, data[data.length - 1].y));
labels.push(date);
}
}

Expand All @@ -61,7 +59,6 @@
var cfg = {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'CHRT - Chart.js Corporation',
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
Expand All @@ -80,7 +77,8 @@
type: 'time',
distribution: 'series',
ticks: {
source: 'labels'
source: 'data',
autoSkip: true
}
}],
yAxes: [{
Expand All @@ -89,9 +87,24 @@
labelString: 'Closing price ($)'
}
}]
},
tooltips: {
intersect: false,
mode: 'index',
callbacks: {
label: function(tooltipItem, myData) {
var label = myData.datasets[tooltipItem.datasetIndex].label || '';
if (label) {
label += ': ';
}
label += parseFloat(tooltipItem.value).toFixed(2);
return label;
}
}
}
}
};

var chart = new Chart(ctx, cfg);

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

0 comments on commit 1d6d5c8

Please sign in to comment.