positioning.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Legend Positions</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. .chart-container {
  14. width: 500px;
  15. margin-left: 40px;
  16. margin-right: 40px;
  17. }
  18. .container {
  19. display: flex;
  20. flex-direction: row;
  21. flex-wrap: wrap;
  22. justify-content: center;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="container">
  28. <div class="chart-container">
  29. <canvas id="chart-legend-top"></canvas>
  30. </div>
  31. <div class="chart-container">
  32. <canvas id="chart-legend-right"></canvas>
  33. </div>
  34. <div class="chart-container">
  35. <canvas id="chart-legend-bottom"></canvas>
  36. </div>
  37. <div class="chart-container">
  38. <canvas id="chart-legend-left"></canvas>
  39. </div>
  40. </div>
  41. <script>
  42. var color = Chart.helpers.color;
  43. function createConfig(legendPosition, colorName) {
  44. return {
  45. type: 'line',
  46. data: {
  47. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  48. datasets: [{
  49. label: 'My First dataset',
  50. data: [
  51. randomScalingFactor(),
  52. randomScalingFactor(),
  53. randomScalingFactor(),
  54. randomScalingFactor(),
  55. randomScalingFactor(),
  56. randomScalingFactor(),
  57. randomScalingFactor()
  58. ],
  59. backgroundColor: color(window.chartColors[colorName]).alpha(0.5).rgbString(),
  60. borderColor: window.chartColors[colorName],
  61. borderWidth: 1
  62. }]
  63. },
  64. options: {
  65. responsive: true,
  66. legend: {
  67. position: legendPosition,
  68. },
  69. scales: {
  70. xAxes: [{
  71. display: true,
  72. scaleLabel: {
  73. display: true,
  74. labelString: 'Month'
  75. }
  76. }],
  77. yAxes: [{
  78. display: true,
  79. scaleLabel: {
  80. display: true,
  81. labelString: 'Value'
  82. }
  83. }]
  84. },
  85. title: {
  86. display: true,
  87. text: 'Legend Position: ' + legendPosition
  88. }
  89. }
  90. };
  91. }
  92. window.onload = function() {
  93. [{
  94. id: 'chart-legend-top',
  95. legendPosition: 'top',
  96. color: 'red'
  97. }, {
  98. id: 'chart-legend-right',
  99. legendPosition: 'right',
  100. color: 'blue'
  101. }, {
  102. id: 'chart-legend-bottom',
  103. legendPosition: 'bottom',
  104. color: 'green'
  105. }, {
  106. id: 'chart-legend-left',
  107. legendPosition: 'left',
  108. color: 'yellow'
  109. }].forEach(function(details) {
  110. var ctx = document.getElementById(details.id).getContext('2d');
  111. var config = createConfig(details.legendPosition, details.color);
  112. new Chart(ctx, config);
  113. });
  114. };
  115. </script>
  116. </body>
  117. </html>