line-point-data.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Time Scale Point Data</title>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/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:75%;">
  18. <canvas id="canvas"></canvas>
  19. </div>
  20. <br>
  21. <br>
  22. <button id="randomizeData">Randomize Data</button>
  23. <button id="addData">Add Data</button>
  24. <button id="removeData">Remove Data</button>
  25. <script>
  26. function newDate(days) {
  27. return moment().add(days, 'd').toDate();
  28. }
  29. function newDateString(days) {
  30. return moment().add(days, 'd').format();
  31. }
  32. var color = Chart.helpers.color;
  33. var config = {
  34. type: 'line',
  35. data: {
  36. datasets: [{
  37. label: 'Dataset with string point data',
  38. backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
  39. borderColor: window.chartColors.red,
  40. fill: false,
  41. data: [{
  42. x: newDateString(0),
  43. y: randomScalingFactor()
  44. }, {
  45. x: newDateString(2),
  46. y: randomScalingFactor()
  47. }, {
  48. x: newDateString(4),
  49. y: randomScalingFactor()
  50. }, {
  51. x: newDateString(5),
  52. y: randomScalingFactor()
  53. }],
  54. }, {
  55. label: 'Dataset with date object point data',
  56. backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(),
  57. borderColor: window.chartColors.blue,
  58. fill: false,
  59. data: [{
  60. x: newDate(0),
  61. y: randomScalingFactor()
  62. }, {
  63. x: newDate(2),
  64. y: randomScalingFactor()
  65. }, {
  66. x: newDate(4),
  67. y: randomScalingFactor()
  68. }, {
  69. x: newDate(5),
  70. y: randomScalingFactor()
  71. }]
  72. }]
  73. },
  74. options: {
  75. responsive: true,
  76. title: {
  77. display: true,
  78. text: 'Chart.js Time Point Data'
  79. },
  80. scales: {
  81. xAxes: [{
  82. type: 'time',
  83. display: true,
  84. scaleLabel: {
  85. display: true,
  86. labelString: 'Date'
  87. },
  88. ticks: {
  89. major: {
  90. fontStyle: 'bold',
  91. fontColor: '#FF0000'
  92. }
  93. }
  94. }],
  95. yAxes: [{
  96. display: true,
  97. scaleLabel: {
  98. display: true,
  99. labelString: 'value'
  100. }
  101. }]
  102. }
  103. }
  104. };
  105. window.onload = function() {
  106. var ctx = document.getElementById('canvas').getContext('2d');
  107. window.myLine = new Chart(ctx, config);
  108. };
  109. document.getElementById('randomizeData').addEventListener('click', function() {
  110. config.data.datasets.forEach(function(dataset) {
  111. dataset.data.forEach(function(dataObj) {
  112. dataObj.y = randomScalingFactor();
  113. });
  114. });
  115. window.myLine.update();
  116. });
  117. // TODO : fix issue with addData
  118. // See https://github.com/chartjs/Chart.js/issues/5197
  119. // The Add Data button for this sample has no effect.
  120. // An error is logged in the console.
  121. document.getElementById('addData').addEventListener('click', function() {
  122. if (config.data.datasets.length > 0) {
  123. var numTicks = window.myLine.scales['x-axis-0'].ticksAsTimestamps.length;
  124. var lastTime = numTicks ? moment(window.myLine.scales['x-axis-0'].ticksAsTimestamps[numTicks - 1]) : moment();
  125. var newTime = lastTime
  126. .clone()
  127. .add(1, 'day')
  128. .format('MM/DD/YYYY HH:mm');
  129. for (var index = 0; index < config.data.datasets.length; ++index) {
  130. config.data.datasets[index].data.push({
  131. x: newTime,
  132. y: randomScalingFactor()
  133. });
  134. }
  135. window.myLine.update();
  136. }
  137. });
  138. document.getElementById('removeData').addEventListener('click', function() {
  139. config.data.datasets.forEach(function(dataset) {
  140. dataset.data.pop();
  141. });
  142. window.myLine.update();
  143. });
  144. </script>
  145. </body>
  146. </html>