financial.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart</title>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
  6. <script src="../../../dist/Chart.min.js"></script>
  7. <script src="../../utils.js"></script>
  8. <style>
  9. canvas {
  10. -moz-user-select: none;
  11. -webkit-user-select: none;
  12. -ms-user-select: none;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div style="width:1000px">
  18. <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>
  19. <canvas id="chart1"></canvas>
  20. </div>
  21. <br>
  22. <br>
  23. Chart Type:
  24. <select id="type">
  25. <option value="line">Line</option>
  26. <option value="bar">Bar</option>
  27. </select>
  28. <select id="unit">
  29. <option value="second">Second</option>
  30. <option value="minute">Minute</option>
  31. <option value="hour">Hour</option>
  32. <option value="day" selected>Day</option>
  33. <option value="month">Month</option>
  34. <option value="year">Year</option>
  35. </select>
  36. <button id="update">update</button>
  37. <script>
  38. function generateData() {
  39. var unit = document.getElementById('unit').value;
  40. function unitLessThanDay() {
  41. return unit === 'second' || unit === 'minute' || unit === 'hour';
  42. }
  43. function beforeNineThirty(date) {
  44. return date.hour() < 9 || (date.hour() === 9 && date.minute() < 30);
  45. }
  46. // Returns true if outside 9:30am-4pm on a weekday
  47. function outsideMarketHours(date) {
  48. if (date.isoWeekday() > 5) {
  49. return true;
  50. }
  51. if (unitLessThanDay() && (beforeNineThirty(date) || date.hour() > 16)) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. function randomNumber(min, max) {
  57. return Math.random() * (max - min) + min;
  58. }
  59. function randomBar(date, lastClose) {
  60. var open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
  61. var close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
  62. return {
  63. t: date.valueOf(),
  64. y: close
  65. };
  66. }
  67. var date = moment('Jan 01 1990', 'MMM DD YYYY');
  68. var now = moment();
  69. var data = [];
  70. var lessThanDay = unitLessThanDay();
  71. for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit).startOf(unit)) {
  72. if (outsideMarketHours(date)) {
  73. if (!lessThanDay || !beforeNineThirty(date)) {
  74. date = date.clone().add(date.isoWeekday() >= 5 ? 8 - date.isoWeekday() : 1, 'day');
  75. }
  76. if (lessThanDay) {
  77. date = date.hour(9).minute(30).second(0);
  78. }
  79. }
  80. data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
  81. }
  82. return data;
  83. }
  84. var ctx = document.getElementById('chart1').getContext('2d');
  85. ctx.canvas.width = 1000;
  86. ctx.canvas.height = 300;
  87. var color = Chart.helpers.color;
  88. var cfg = {
  89. type: 'bar',
  90. data: {
  91. datasets: [{
  92. label: 'CHRT - Chart.js Corporation',
  93. backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
  94. borderColor: window.chartColors.red,
  95. data: generateData(),
  96. type: 'line',
  97. pointRadius: 0,
  98. fill: false,
  99. lineTension: 0,
  100. borderWidth: 2
  101. }]
  102. },
  103. options: {
  104. scales: {
  105. xAxes: [{
  106. type: 'time',
  107. distribution: 'series',
  108. ticks: {
  109. source: 'data',
  110. autoSkip: true
  111. }
  112. }],
  113. yAxes: [{
  114. scaleLabel: {
  115. display: true,
  116. labelString: 'Closing price ($)'
  117. }
  118. }]
  119. },
  120. tooltips: {
  121. intersect: false,
  122. mode: 'index',
  123. callbacks: {
  124. label: function(tooltipItem, myData) {
  125. var label = myData.datasets[tooltipItem.datasetIndex].label || '';
  126. if (label) {
  127. label += ': ';
  128. }
  129. label += parseFloat(tooltipItem.value).toFixed(2);
  130. return label;
  131. }
  132. }
  133. }
  134. }
  135. };
  136. var chart = new Chart(ctx, cfg);
  137. document.getElementById('update').addEventListener('click', function() {
  138. var type = document.getElementById('type').value;
  139. var dataset = chart.config.data.datasets[0];
  140. dataset.type = type;
  141. dataset.data = generateData();
  142. chart.update();
  143. });
  144. </script>
  145. </body>
  146. </html>