multi-axis.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart Multiple Axes</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 lineChartData = {
  22. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  23. datasets: [{
  24. label: 'My First dataset',
  25. borderColor: window.chartColors.red,
  26. backgroundColor: window.chartColors.red,
  27. fill: false,
  28. data: [
  29. randomScalingFactor(),
  30. randomScalingFactor(),
  31. randomScalingFactor(),
  32. randomScalingFactor(),
  33. randomScalingFactor(),
  34. randomScalingFactor(),
  35. randomScalingFactor()
  36. ],
  37. yAxisID: 'y-axis-1',
  38. }, {
  39. label: 'My Second dataset',
  40. borderColor: window.chartColors.blue,
  41. backgroundColor: window.chartColors.blue,
  42. fill: false,
  43. data: [
  44. randomScalingFactor(),
  45. randomScalingFactor(),
  46. randomScalingFactor(),
  47. randomScalingFactor(),
  48. randomScalingFactor(),
  49. randomScalingFactor(),
  50. randomScalingFactor()
  51. ],
  52. yAxisID: 'y-axis-2'
  53. }]
  54. };
  55. window.onload = function() {
  56. var ctx = document.getElementById('canvas').getContext('2d');
  57. window.myLine = Chart.Line(ctx, {
  58. data: lineChartData,
  59. options: {
  60. responsive: true,
  61. hoverMode: 'index',
  62. stacked: false,
  63. title: {
  64. display: true,
  65. text: 'Chart.js Line Chart - Multi Axis'
  66. },
  67. scales: {
  68. yAxes: [{
  69. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  70. display: true,
  71. position: 'left',
  72. id: 'y-axis-1',
  73. }, {
  74. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  75. display: true,
  76. position: 'right',
  77. id: 'y-axis-2',
  78. // grid line settings
  79. gridLines: {
  80. drawOnChartArea: false, // only want the grid lines for one axis to show up
  81. },
  82. }],
  83. }
  84. }
  85. });
  86. };
  87. document.getElementById('randomizeData').addEventListener('click', function() {
  88. lineChartData.datasets.forEach(function(dataset) {
  89. dataset.data = dataset.data.map(function() {
  90. return randomScalingFactor();
  91. });
  92. });
  93. window.myLine.update();
  94. });
  95. </script>
  96. </body>
  97. </html>