multi-axis.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Scatter 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 color = Chart.helpers.color;
  22. var scatterChartData = {
  23. datasets: [{
  24. label: 'My First dataset',
  25. xAxisID: 'x-axis-1',
  26. yAxisID: 'y-axis-1',
  27. borderColor: window.chartColors.red,
  28. backgroundColor: color(window.chartColors.red).alpha(0.2).rgbString(),
  29. data: [{
  30. x: randomScalingFactor(),
  31. y: randomScalingFactor(),
  32. }, {
  33. x: randomScalingFactor(),
  34. y: randomScalingFactor(),
  35. }, {
  36. x: randomScalingFactor(),
  37. y: randomScalingFactor(),
  38. }, {
  39. x: randomScalingFactor(),
  40. y: randomScalingFactor(),
  41. }, {
  42. x: randomScalingFactor(),
  43. y: randomScalingFactor(),
  44. }, {
  45. x: randomScalingFactor(),
  46. y: randomScalingFactor(),
  47. }, {
  48. x: randomScalingFactor(),
  49. y: randomScalingFactor(),
  50. }]
  51. }, {
  52. label: 'My Second dataset',
  53. xAxisID: 'x-axis-1',
  54. yAxisID: 'y-axis-2',
  55. borderColor: window.chartColors.blue,
  56. backgroundColor: color(window.chartColors.blue).alpha(0.2).rgbString(),
  57. data: [{
  58. x: randomScalingFactor(),
  59. y: randomScalingFactor(),
  60. }, {
  61. x: randomScalingFactor(),
  62. y: randomScalingFactor(),
  63. }, {
  64. x: randomScalingFactor(),
  65. y: randomScalingFactor(),
  66. }, {
  67. x: randomScalingFactor(),
  68. y: randomScalingFactor(),
  69. }, {
  70. x: randomScalingFactor(),
  71. y: randomScalingFactor(),
  72. }, {
  73. x: randomScalingFactor(),
  74. y: randomScalingFactor(),
  75. }, {
  76. x: randomScalingFactor(),
  77. y: randomScalingFactor(),
  78. }]
  79. }]
  80. };
  81. window.onload = function() {
  82. var ctx = document.getElementById('canvas').getContext('2d');
  83. window.myScatter = Chart.Scatter(ctx, {
  84. data: scatterChartData,
  85. options: {
  86. responsive: true,
  87. hoverMode: 'nearest',
  88. intersect: true,
  89. title: {
  90. display: true,
  91. text: 'Chart.js Scatter Chart - Multi Axis'
  92. },
  93. scales: {
  94. xAxes: [{
  95. position: 'bottom',
  96. gridLines: {
  97. zeroLineColor: 'rgba(0,0,0,1)'
  98. }
  99. }],
  100. yAxes: [{
  101. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  102. display: true,
  103. position: 'left',
  104. id: 'y-axis-1',
  105. }, {
  106. type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  107. display: true,
  108. position: 'right',
  109. reverse: true,
  110. id: 'y-axis-2',
  111. // grid line settings
  112. gridLines: {
  113. drawOnChartArea: false, // only want the grid lines for one axis to show up
  114. },
  115. }],
  116. }
  117. }
  118. });
  119. };
  120. document.getElementById('randomizeData').addEventListener('click', function() {
  121. scatterChartData.datasets.forEach(function(dataset) {
  122. dataset.data = dataset.data.map(function() {
  123. return {
  124. x: randomScalingFactor(),
  125. y: randomScalingFactor()
  126. };
  127. });
  128. });
  129. window.myScatter.update();
  130. });
  131. </script>
  132. </body>
  133. </html>