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.

155 lines
5.6KB

  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="addData">Add Data</button>
  23. <button id="removeData">Remove Data</button>
  24. <script>
  25. var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  26. var randomScalingFactor = function() {
  27. return Math.round(Math.random() * 100 * (Math.random() > 0.5 ? -1 : 1));
  28. };
  29. var randomColorFactor = function() {
  30. return Math.round(Math.random() * 255);
  31. };
  32. var randomColor = function(opacity) {
  33. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  34. };
  35. var config = {
  36. type: 'line',
  37. data: {
  38. labels: ["January", "February", "March", "April", "May", "June", "July"],
  39. datasets: [{
  40. label: "dataset - big points",
  41. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  42. fill: false,
  43. borderDash: [5, 5],
  44. pointRadius: 15,
  45. pointHoverRadius: 10,
  46. }, {
  47. label: "dataset - individual point sizes",
  48. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  49. fill: false,
  50. borderDash: [5, 5],
  51. pointRadius: [2, 4, 6, 18, 0, 12, 20],
  52. }, {
  53. label: "dataset - large pointHoverRadius",
  54. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  55. fill: false,
  56. pointHoverRadius: 30,
  57. }, {
  58. label: "dataset - large pointHitRadius",
  59. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  60. fill: false,
  61. pointHitRadius: 20,
  62. }]
  63. },
  64. options: {
  65. responsive: true,
  66. legend: {
  67. position: 'bottom',
  68. },
  69. hover: {
  70. mode: 'label'
  71. },
  72. scales: {
  73. xAxes: [{
  74. display: true,
  75. scaleLabel: {
  76. display: true,
  77. labelString: 'Month'
  78. }
  79. }],
  80. yAxes: [{
  81. display: true,
  82. scaleLabel: {
  83. display: true,
  84. labelString: 'Value'
  85. }
  86. }]
  87. },
  88. title: {
  89. display: true,
  90. text: 'Chart.js Line Chart - Different point sizes'
  91. }
  92. }
  93. };
  94. $.each(config.data.datasets, function(i, dataset) {
  95. var background = randomColor(0.5);
  96. dataset.borderColor = background;
  97. dataset.backgroundColor = background;
  98. dataset.pointBorderColor = background;
  99. dataset.pointBackgroundColor = background;
  100. dataset.pointBorderWidth = 1;
  101. });
  102. window.onload = function() {
  103. var ctx = document.getElementById("canvas").getContext("2d");
  104. window.myLine = new Chart(ctx, config);
  105. };
  106. $('#randomizeData').click(function() {
  107. $.each(config.data.datasets, function(i, dataset) {
  108. dataset.data = dataset.data.map(function() {
  109. return randomScalingFactor();
  110. });
  111. });
  112. window.myLine.update();
  113. });
  114. $('#addData').click(function() {
  115. if (config.data.datasets.length > 0) {
  116. var month = MONTHS[config.data.labels.length % MONTHS.length];
  117. config.data.labels.push(month);
  118. $.each(config.data.datasets, function(i, dataset) {
  119. dataset.data.push(randomScalingFactor());
  120. if (Array.isArray(dataset.pointRadius)) {
  121. dataset.pointRadius.push(Math.random() * 30);
  122. }
  123. });
  124. window.myLine.update();
  125. }
  126. });
  127. $('#removeData').click(function() {
  128. config.data.labels.splice(-1, 1); // remove the label first
  129. config.data.datasets.forEach(function(dataset, datasetIndex) {
  130. dataset.data.pop();
  131. if (Array.isArray(dataset.pointRadius)) {
  132. dataset.pointRadius.pop();
  133. }
  134. });
  135. window.myLine.update();
  136. });
  137. </script>
  138. </body>
  139. </html>