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

Fix data in financial sample #6244

Merged
merged 7 commits into from
May 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions samples/scales/time/financial.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<body>
<div style="width:1000px">
<p>This example demonstrates a time series scale by drawing a financial line chart using just the core library. For more specific functionality for financial charts, please see <a href="https://github.com/chartjs/chartjs-chart-financial">chartjs-chart-financial</a></p>
<canvas id="chart1"></canvas>
</div>
<br>
Expand All @@ -37,6 +38,27 @@
<button id="update">update</button>
<script>
function generateData() {
var unit = document.getElementById('unit').value;

function unitLessThanDay() {
return unit === 'second' || unit === 'minute' || unit === 'hour';
}

function beforeNineThirty(date) {
return date.hour() < 9 || (date.hour() === 9 && date.minute() < 30);
}

// Returns true if outside 9:30am-4pm on a weekday
function outsideMarketHours(date) {
if (date.isoWeekday() > 5) {
return true;
}
if (unitLessThanDay() && (beforeNineThirty(date) || date.hour() > 16)) {
return true;
}
return false;
}

function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
Expand All @@ -53,11 +75,17 @@
var date = moment('Jan 01 1990', 'MMM DD YYYY');
var now = moment();
var data = [];
var unit = document.getElementById('unit').value;
for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit)) {
if (date.isoWeekday() <= 5) {
data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
var lessThanDay = unitLessThanDay();
for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit).startOf(unit)) {
if (outsideMarketHours(date)) {
if (!lessThanDay || !beforeNineThirty(date)) {
date = date.clone().add(date.isoWeekday() >= 5 ? 8 - date.isoWeekday() : 1, 'day');
}
benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (lessThanDay) {
date = date.hour(9).minute(30).second(0);
}
}
data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
}

return data;
Expand Down