Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

164 rindas
4.8KB

  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 * (Math.random() > 0.5 ? -1 : 1));
  30. };
  31. var randomColorFactor = function() {
  32. return Math.round(Math.random() * 255);
  33. };
  34. var randomColor = function(opacity) {
  35. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '1') + ')';
  36. };
  37. var config = {
  38. type: 'line',
  39. data: {
  40. labels: ["January", "February", "March", "April", "May", "June", "July"],
  41. datasets: [{
  42. label: "My First dataset",
  43. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  44. }, {
  45. label: "My Second dataset",
  46. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  47. }, {
  48. label: "My Third dataset",
  49. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  50. }, {
  51. label: "My Third dataset",
  52. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  53. }]
  54. },
  55. options: {
  56. responsive: true,
  57. title:{
  58. display:true,
  59. text:"Chart.js Line Chart - Stacked Area"
  60. },
  61. tooltips: {
  62. mode: 'label',
  63. },
  64. hover: {
  65. mode: 'label'
  66. },
  67. scales: {
  68. xAxes: [{
  69. scaleLabel: {
  70. display: true,
  71. labelString: 'Month'
  72. }
  73. }],
  74. yAxes: [{
  75. stacked: true,
  76. scaleLabel: {
  77. display: true,
  78. labelString: 'Value'
  79. }
  80. }]
  81. }
  82. }
  83. };
  84. $.each(config.data.datasets, function(i, dataset) {
  85. var color = randomColor(1);
  86. dataset.borderColor = color;
  87. dataset.backgroundColor = color;
  88. dataset.pointBorderColor = color;
  89. dataset.pointBackgroundColor = color;
  90. dataset.pointBorderWidth = 1;
  91. });
  92. window.onload = function() {
  93. var ctx = document.getElementById("canvas").getContext("2d");
  94. window.myLine = new Chart(ctx, config);
  95. };
  96. $('#randomizeData').click(function() {
  97. $.each(config.data.datasets, function(i, dataset) {
  98. dataset.data = dataset.data.map(function() {
  99. return randomScalingFactor();
  100. });
  101. });
  102. window.myLine.update();
  103. });
  104. $('#addDataset').click(function() {
  105. var newDataset = {
  106. label: 'Dataset ' + config.data.datasets.length,
  107. borderColor: randomColor(0.4),
  108. backgroundColor: randomColor(0.5),
  109. pointBorderColor: randomColor(0.7),
  110. pointBackgroundColor: randomColor(0.5),
  111. pointBorderWidth: 1,
  112. data: [],
  113. };
  114. for (var index = 0; index < config.data.labels.length; ++index) {
  115. newDataset.data.push(randomScalingFactor());
  116. }
  117. config.data.datasets.push(newDataset);
  118. window.myLine.update();
  119. });
  120. $('#addData').click(function() {
  121. if (config.data.datasets.length > 0) {
  122. var month = MONTHS[config.data.labels.length % MONTHS.length];
  123. config.data.labels.push(month);
  124. $.each(config.data.datasets, function(i, dataset) {
  125. dataset.data.push(randomScalingFactor());
  126. });
  127. window.myLine.update();
  128. }
  129. });
  130. $('#removeDataset').click(function() {
  131. config.data.datasets.splice(0, 1);
  132. window.myLine.update();
  133. });
  134. $('#removeData').click(function() {
  135. config.data.labels.splice(-1, 1); // remove the label first
  136. config.data.datasets.forEach(function(dataset, datasetIndex) {
  137. dataset.data.pop();
  138. });
  139. window.myLine.update();
  140. });
  141. </script>
  142. </body>
  143. </html>