You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart</title>
  5. <script src="../dist/Chart.bundle.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  7. <style>
  8. canvas {
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="width:75%;">
  17. <canvas id="canvas"></canvas>
  18. </div>
  19. <br>
  20. <br>
  21. <button id="randomizeData">Randomize Data</button>
  22. <button id="addDataset">Add Dataset</button>
  23. <button id="removeDataset">Remove Dataset</button>
  24. <button id="addData">Add Data</button>
  25. <button id="removeData">Remove Data</button>
  26. <script>
  27. var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  28. var randomScalingFactor = function() {
  29. return Math.round(Math.random() * 100);
  30. //return 0;
  31. };
  32. var randomColorFactor = function() {
  33. return Math.round(Math.random() * 255);
  34. };
  35. var randomColor = function(opacity) {
  36. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  37. };
  38. var config = {
  39. type: 'line',
  40. data: {
  41. labels: ["January", "February", "March", "April", "May", "June", "July"],
  42. datasets: [{
  43. label: "My First dataset",
  44. // Skip a point in the middle
  45. data: [randomScalingFactor(), randomScalingFactor(), NaN, randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  46. fill: true,
  47. borderDash: [5, 5],
  48. }, {
  49. label: "My Second dataset",
  50. // Skip first and last points
  51. data: [NaN, randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), NaN],
  52. }]
  53. },
  54. options: {
  55. responsive: true,
  56. title:{
  57. display:true,
  58. text:'Chart.js Line Chart - Skip Points'
  59. },
  60. tooltips: {
  61. mode: 'label',
  62. },
  63. hover: {
  64. mode: 'label'
  65. },
  66. scales: {
  67. xAxes: [{
  68. display: true,
  69. scaleLabel: {
  70. display: true,
  71. labelString: 'Month'
  72. }
  73. }],
  74. yAxes: [{
  75. display: true,
  76. scaleLabel: {
  77. display: true,
  78. labelString: 'Value'
  79. },
  80. }]
  81. }
  82. }
  83. };
  84. $.each(config.data.datasets, function(i, dataset) {
  85. dataset.borderColor = randomColor(0.4);
  86. dataset.backgroundColor = randomColor(0.5);
  87. dataset.pointBorderColor = randomColor(0.7);
  88. dataset.pointBackgroundColor = randomColor(0.5);
  89. dataset.pointBorderWidth = 1;
  90. });
  91. window.onload = function() {
  92. var ctx = document.getElementById("canvas").getContext("2d");
  93. window.myLine = new Chart(ctx, config);
  94. };
  95. $('#randomizeData').click(function() {
  96. $.each(config.data.datasets, function(i, dataset) {
  97. dataset.data = dataset.data.map(function() {
  98. return randomScalingFactor();
  99. });
  100. });
  101. window.myLine.update();
  102. });
  103. $('#addDataset').click(function() {
  104. var newDataset = {
  105. label: 'Dataset ' + config.data.datasets.length,
  106. borderColor: randomColor(0.4),
  107. backgroundColor: randomColor(0.5),
  108. pointBorderColor: randomColor(0.7),
  109. pointBackgroundColor: randomColor(0.5),
  110. pointBorderWidth: 1,
  111. data: [],
  112. };
  113. for (var index = 0; index < config.data.labels.length; ++index) {
  114. newDataset.data.push(randomScalingFactor());
  115. }
  116. config.data.datasets.push(newDataset);
  117. window.myLine.update();
  118. });
  119. $('#addData').click(function() {
  120. if (config.data.datasets.length > 0) {
  121. var month = MONTHS[config.data.labels.length % MONTHS.length];
  122. config.data.labels.push(month);
  123. $.each(config.data.datasets, function(i, dataset) {
  124. dataset.data.push(randomScalingFactor());
  125. });
  126. window.myLine.update();
  127. }
  128. });
  129. $('#removeDataset').click(function() {
  130. config.data.datasets.splice(0, 1);
  131. window.myLine.update();
  132. });
  133. $('#removeData').click(function() {
  134. config.data.labels.splice(-1, 1); // remove the label first
  135. config.data.datasets.forEach(function(dataset, datasetIndex) {
  136. dataset.data.pop();
  137. });
  138. window.myLine.update();
  139. });
  140. </script>
  141. </body>
  142. </html>