Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve timeseries sample #6089

Merged
merged 1 commit into from Mar 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions samples/scales/time/financial.html
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