multi-axis.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Bar Chart Multi Axis</title>
  5. <script src="../../../dist/Chart.bundle.js"></script>
  6. <script src="../../utils.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. <button id="randomizeData">Randomize Data</button>
  20. <script>
  21. var barChartData = {
  22. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  23. datasets: [{
  24. label: 'Dataset 1',
  25. backgroundColor: [
  26. window.chartColors.red,
  27. window.chartColors.orange,
  28. window.chartColors.yellow,
  29. window.chartColors.green,
  30. window.chartColors.blue,
  31. window.chartColors.purple,
  32. window.chartColors.red
  33. ],
  34. yAxisID: 'y-axis-1',
  35. data: [
  36. randomScalingFactor(),
  37. randomScalingFactor(),
  38. randomScalingFactor(),
  39. randomScalingFactor(),
  40. randomScalingFactor(),
  41. randomScalingFactor(),
  42. randomScalingFactor()
  43. ]
  44. }, {
  45. label: 'Dataset 2',
  46. backgroundColor: window.chartColors.grey,
  47. yAxisID: 'y-axis-2',
  48. data: [
  49. randomScalingFactor(),
  50. randomScalingFactor(),
  51. randomScalingFactor(),
  52. randomScalingFactor(),
  53. randomScalingFactor(),
  54. randomScalingFactor(),
  55. randomScalingFactor()
  56. ]
  57. }]
  58. };
  59. window.onload = function() {
  60. var ctx = document.getElementById('canvas').getContext('2d');
  61. window.myBar = new Chart(ctx, {
  62. type: 'bar',
  63. data: barChartData,
  64. options: {
  65. responsive: true,
  66. title: {
  67. display: true,
  68. text: 'Chart.js Bar Chart - Multi Axis'
  69. },
  70. tooltips: {
  71. mode: 'index',
  72. intersect: true
  73. },
  74. scales: {
  75. yAxes: [{
  76. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  77. display: true,
  78. position: 'left',
  79. id: 'y-axis-1',
  80. }, {
  81. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  82. display: true,
  83. position: 'right',
  84. id: 'y-axis-2',
  85. gridLines: {
  86. drawOnChartArea: false
  87. }
  88. }],
  89. }
  90. }
  91. });
  92. };
  93. document.getElementById('randomizeData').addEventListener('click', function() {
  94. barChartData.datasets.forEach(function(dataset) {
  95. dataset.data = dataset.data.map(function() {
  96. return randomScalingFactor();
  97. });
  98. });
  99. window.myBar.update();
  100. });
  101. </script>
  102. </body>
  103. </html>