point-style.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Legend Point Style</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-normal"></canvas>
  30. </div>
  31. <div class="chart-container">
  32. <canvas id="chart-legend-pointstyle"></canvas>
  33. </div>
  34. </div>
  35. <script>
  36. var color = Chart.helpers.color;
  37. function createConfig(colorName) {
  38. return {
  39. type: 'line',
  40. data: {
  41. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  42. datasets: [{
  43. label: 'My First dataset',
  44. data: [
  45. randomScalingFactor(),
  46. randomScalingFactor(),
  47. randomScalingFactor(),
  48. randomScalingFactor(),
  49. randomScalingFactor(),
  50. randomScalingFactor(),
  51. randomScalingFactor()
  52. ],
  53. backgroundColor: color(window.chartColors[colorName]).alpha(0.5).rgbString(),
  54. borderColor: window.chartColors[colorName],
  55. borderWidth: 1,
  56. pointStyle: 'rectRot',
  57. pointRadius: 5,
  58. pointBorderColor: 'rgb(0, 0, 0)'
  59. }]
  60. },
  61. options: {
  62. responsive: true,
  63. legend: {
  64. labels: {
  65. usePointStyle: false
  66. }
  67. },
  68. scales: {
  69. xAxes: [{
  70. display: true,
  71. scaleLabel: {
  72. display: true,
  73. labelString: 'Month'
  74. }
  75. }],
  76. yAxes: [{
  77. display: true,
  78. scaleLabel: {
  79. display: true,
  80. labelString: 'Value'
  81. }
  82. }]
  83. },
  84. title: {
  85. display: true,
  86. text: 'Normal Legend'
  87. }
  88. }
  89. };
  90. }
  91. function createPointStyleConfig(colorName) {
  92. var config = createConfig(colorName);
  93. config.options.legend.labels.usePointStyle = true;
  94. config.options.title.text = 'Point Style Legend';
  95. return config;
  96. }
  97. window.onload = function() {
  98. [{
  99. id: 'chart-legend-normal',
  100. config: createConfig('red')
  101. }, {
  102. id: 'chart-legend-pointstyle',
  103. config: createPointStyleConfig('blue')
  104. }].forEach(function(details) {
  105. var ctx = document.getElementById(details.id).getContext('2d');
  106. new Chart(ctx, details.config);
  107. });
  108. };
  109. </script>
  110. </body>
  111. </html>