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.

144 lines
5.4KB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Horizontal 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 horizontalBarChartData = {
  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.myHorizontalBar = new Chart(ctx, {
  55. type: 'horizontalBar',
  56. data: horizontalBarChartData,
  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 horizontal bar to be 2px wide and green
  60. elements: {
  61. rectangle: {
  62. borderWidth: 2,
  63. borderColor: 'rgb(0, 255, 0)',
  64. borderSkipped: 'left'
  65. }
  66. },
  67. responsive: true,
  68. legend: {
  69. position: 'right',
  70. },
  71. title: {
  72. display: true,
  73. text: 'Chart.js Horizontal Bar Chart'
  74. }
  75. }
  76. });
  77. };
  78. $('#randomizeData').click(function() {
  79. var zero = Math.random() < 0.2 ? true : false;
  80. $.each(horizontalBarChartData.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.myHorizontalBar.update();
  87. });
  88. $('#addDataset').click(function() {
  89. var newDataset = {
  90. label: 'Dataset ' + horizontalBarChartData.datasets.length,
  91. backgroundColor: randomColor(),
  92. data: []
  93. };
  94. for (var index = 0; index < horizontalBarChartData.labels.length; ++index) {
  95. newDataset.data.push(randomScalingFactor());
  96. }
  97. horizontalBarChartData.datasets.push(newDataset);
  98. window.myHorizontalBar.update();
  99. });
  100. $('#addData').click(function() {
  101. if (horizontalBarChartData.datasets.length > 0) {
  102. var month = MONTHS[horizontalBarChartData.labels.length % MONTHS.length];
  103. horizontalBarChartData.labels.push(month);
  104. for (var index = 0; index < horizontalBarChartData.datasets.length; ++index) {
  105. horizontalBarChartData.datasets[index].data.push(randomScalingFactor());
  106. }
  107. window.myHorizontalBar.update();
  108. }
  109. });
  110. $('#removeDataset').click(function() {
  111. horizontalBarChartData.datasets.splice(0, 1);
  112. window.myHorizontalBar.update();
  113. });
  114. $('#removeData').click(function() {
  115. horizontalBarChartData.labels.splice(-1, 1); // remove the label first
  116. horizontalBarChartData.datasets.forEach(function (dataset, datasetIndex) {
  117. dataset.data.pop();
  118. });
  119. window.myHorizontalBar.update();
  120. });
  121. </script>
  122. </body>
  123. </html>