financial.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.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. <canvas id="chart1"></canvas>
  19. </div>
  20. <br>
  21. <br>
  22. Chart Type:
  23. <select id="type">
  24. <option value="line">Line</option>
  25. <option value="bar">Bar</option>
  26. </select>
  27. <button id="update">update</button>
  28. <script>
  29. function randomNumber(min, max) {
  30. return Math.random() * (max - min) + min;
  31. }
  32. function randomBar(date, lastClose) {
  33. var open = randomNumber(lastClose * 0.95, lastClose * 1.05);
  34. var close = randomNumber(open * 0.95, open * 1.05);
  35. return {
  36. t: date.valueOf(),
  37. y: close
  38. };
  39. }
  40. var dateFormat = 'MMMM DD YYYY';
  41. var date = moment('April 01 2017', dateFormat);
  42. var data = [randomBar(date, 30)];
  43. var labels = [date];
  44. while (data.length < 60) {
  45. date = date.clone().add(1, 'd');
  46. if (date.isoWeekday() <= 5) {
  47. data.push(randomBar(date, data[data.length - 1].y));
  48. labels.push(date);
  49. }
  50. }
  51. var ctx = document.getElementById('chart1').getContext('2d');
  52. ctx.canvas.width = 1000;
  53. ctx.canvas.height = 300;
  54. var cfg = {
  55. type: 'bar',
  56. data: {
  57. labels: labels,
  58. datasets: [{
  59. label: 'CHRT - Chart.js Corporation',
  60. data: data,
  61. type: 'line',
  62. pointRadius: 0,
  63. fill: false,
  64. lineTension: 0,
  65. borderWidth: 2
  66. }]
  67. },
  68. options: {
  69. scales: {
  70. xAxes: [{
  71. type: 'time',
  72. distribution: 'series',
  73. ticks: {
  74. source: 'labels'
  75. }
  76. }],
  77. yAxes: [{
  78. scaleLabel: {
  79. display: true,
  80. labelString: 'Closing price ($)'
  81. }
  82. }]
  83. }
  84. }
  85. };
  86. var chart = new Chart(ctx, cfg);
  87. document.getElementById('update').addEventListener('click', function() {
  88. var type = document.getElementById('type').value;
  89. chart.config.data.datasets[0].type = type;
  90. chart.update();
  91. });
  92. </script>
  93. </body>
  94. </html>