您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

145 行
5.2KB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Bar Chart</title>
  5. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  6. <script src="../dist/Chart.bundle.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 id="container" style="width: 75%;">
  17. <canvas id="canvas"></canvas>
  18. </div>
  19. <button id="randomizeData">Randomize Data</button>
  20. <button id="addDataset">Add Dataset</button>
  21. <button id="removeDataset">Remove Dataset</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.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
  28. };
  29. var randomColorFactor = function() {
  30. return Math.round(Math.random() * 255);
  31. };
  32. var randomColor = function() {
  33. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',.7)';
  34. };
  35. var barChartData = {
  36. labels: ["January", "February", "March", "April", "May", "June", "July"],
  37. datasets: [{
  38. label: 'Dataset 1',
  39. backgroundColor: "rgba(220,220,220,0.5)",
  40. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  41. }, {
  42. hidden: true,
  43. label: 'Dataset 2',
  44. backgroundColor: "rgba(151,187,205,0.5)",
  45. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  46. }, {
  47. label: 'Dataset 3',
  48. backgroundColor: "rgba(151,187,205,0.5)",
  49. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  50. }]
  51. };
  52. window.onload = function() {
  53. var ctx = document.getElementById("canvas").getContext("2d");
  54. window.myBar = new Chart(ctx, {
  55. type: 'bar',
  56. data: barChartData,
  57. options: {
  58. // Elements options apply to all of the options unless overridden in a dataset
  59. // In this case, we are setting the border of each bar to be 2px wide and green
  60. elements: {
  61. rectangle: {
  62. borderWidth: 2,
  63. borderColor: 'rgb(0, 255, 0)',
  64. borderSkipped: 'bottom'
  65. }
  66. },
  67. responsive: true,
  68. legend: {
  69. position: 'top',
  70. },
  71. title: {
  72. display: true,
  73. text: 'Chart.js Bar Chart'
  74. }
  75. }
  76. });
  77. };
  78. $('#randomizeData').click(function() {
  79. var zero = Math.random() < 0.2 ? true : false;
  80. $.each(barChartData.datasets, function(i, dataset) {
  81. dataset.backgroundColor = randomColor();
  82. dataset.data = dataset.data.map(function() {
  83. return zero ? 0.0 : randomScalingFactor();
  84. });
  85. });
  86. window.myBar.update();
  87. });
  88. $('#addDataset').click(function() {
  89. var newDataset = {
  90. label: 'Dataset ' + barChartData.datasets.length,
  91. backgroundColor: randomColor(),
  92. data: []
  93. };
  94. for (var index = 0; index < barChartData.labels.length; ++index) {
  95. newDataset.data.push(randomScalingFactor());
  96. }
  97. barChartData.datasets.push(newDataset);
  98. window.myBar.update();
  99. });
  100. $('#addData').click(function() {
  101. if (barChartData.datasets.length > 0) {
  102. var month = MONTHS[barChartData.labels.length % MONTHS.length];
  103. barChartData.labels.push(month);
  104. for (var index = 0; index < barChartData.datasets.length; ++index) {
  105. //window.myBar.addData(randomScalingFactor(), index);
  106. barChartData.datasets[index].data.push(randomScalingFactor());
  107. }
  108. window.myBar.update();
  109. }
  110. });
  111. $('#removeDataset').click(function() {
  112. barChartData.datasets.splice(0, 1);
  113. window.myBar.update();
  114. });
  115. $('#removeData').click(function() {
  116. barChartData.labels.splice(-1, 1); // remove the label first
  117. barChartData.datasets.forEach(function(dataset, datasetIndex) {
  118. dataset.data.pop();
  119. });
  120. window.myBar.update();
  121. });
  122. </script>
  123. </body>
  124. </html>