-
Notifications
You must be signed in to change notification settings - Fork 12k
Add a financial time series sample #4554
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
Merged
+107
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <!doctype html> | ||
| <html> | ||
|
|
||
| <head> | ||
| <title>Line Chart</title> | ||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> | ||
| <script src="../../../dist/Chart.js"></script> | ||
| <script src="../../utils.js"></script> | ||
| <style> | ||
| canvas { | ||
| -moz-user-select: none; | ||
| -webkit-user-select: none; | ||
| -ms-user-select: none; | ||
| } | ||
| </style> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div style="width:1000px"> | ||
| <canvas id="chart1"></canvas> | ||
| <div> | ||
| <br> | ||
| <br> | ||
| Chart Type: | ||
| <select id="type"> | ||
| <option value="line">Line</option> | ||
| <option value="bar">Bar</option> | ||
| </select> | ||
| <button id="update">update</button> | ||
| <script> | ||
| function randomNumber(min, max) { | ||
| return Math.random() * (max - min) + min; | ||
| } | ||
|
|
||
| function randomBar(date, lastClose) { | ||
| var open = randomNumber(lastClose * .95, lastClose * 1.05); | ||
| var close = randomNumber(open * .95, open * 1.05); | ||
| var high = randomNumber(Math.max(open, close), Math.max(open, close) * 1.1); | ||
| var low = randomNumber(Math.min(open, close) * .9, Math.min(open, close)); | ||
| return { | ||
| t: date.valueOf(), | ||
| y: close | ||
| }; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| var ctx = document.getElementById("chart1").getContext("2d"); | ||
| ctx.canvas.width = 1000; | ||
| ctx.canvas.height = 300; | ||
| var cfg = { | ||
| type: 'bar', | ||
| data: { | ||
| labels: labels, | ||
| datasets: [{ | ||
| label: "CHRT - Chart.js Corporation", | ||
| data: data, | ||
| type: 'line', | ||
| pointRadius: 0, | ||
| fill: false, | ||
| lineTension: 0, | ||
| borderWidth: 2 | ||
| }] | ||
| }, | ||
| options: { | ||
| scales: { | ||
| xAxes: [{ | ||
| type: 'time', | ||
| distribution: 'series', | ||
| ticks: { | ||
| source: 'labels' | ||
| } | ||
| }], | ||
| yAxes: [{ | ||
| scaleLabel: { | ||
| display: true, | ||
| labelString: 'Closing price ($)' | ||
| } | ||
| }] | ||
| } | ||
| } | ||
| }; | ||
| var chart = new Chart(ctx, cfg); | ||
|
|
||
| document.getElementById('update').addEventListener('click', function() { | ||
| var type = document.getElementById('type').value; | ||
| chart.config.data.datasets[0].type = type; | ||
| chart.update(); | ||
| }); | ||
|
|
||
| </script> | ||
| </body> | ||
|
|
||
| </html> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benmccann can you use
Chart.bundle.jsinstead and remove thecdnjs.cloudflare.comdependency, like that the example can be open locally / offline from the ZIP archive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work unfortunately. It results in
Uncaught ReferenceError: moment is not defined. These couple lines are in the other time samples as well