indices.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" type="text/css" href="../style.css">
  8. <link rel="icon" type="image/ico" href="../favicon.ico">
  9. <script src="https://cdn.jsdelivr.net/npm/chart.js@2.7.0/dist/Chart.min.js"></script>
  10. <script src="../../dist/chartjs-plugin-datalabels.js"></script>
  11. <script src="../utils.js"></script>
  12. </head>
  13. <body>
  14. <div id="side">
  15. <div id="header"></div>
  16. <div id="actions">
  17. <button onclick="randomize(this)">Randomize</button>
  18. <button onclick="addData(this)">Add Data</button>
  19. <button onclick="removeData(this)">Remove Data</button>
  20. </div>
  21. </div>
  22. <div id="charts">
  23. <div class="wrapper"><canvas id="chart-0"></canvas></div>
  24. </div>
  25. <script>
  26. var SAMPLE_INFO = {
  27. group: 'Scriptable',
  28. name: 'Indices',
  29. desc: 'The label styles alternate based on the data indices.'
  30. };
  31. </script>
  32. <script id="script-init">
  33. var DATA_COUNT = 16;
  34. var labels = [];
  35. Samples.srand(4);
  36. for (var i = 0; i < DATA_COUNT; ++i) {
  37. labels.push('' + i);
  38. }
  39. Chart.helpers.merge(Chart.defaults.global, {
  40. aspectRatio: 4/3,
  41. tooltips: false,
  42. layout: {
  43. padding: {
  44. top: 32,
  45. right: 24,
  46. bottom: 32,
  47. left: 0
  48. }
  49. },
  50. elements: {
  51. line: {
  52. fill: false
  53. }
  54. },
  55. plugins: {
  56. legend: false,
  57. title: false
  58. }
  59. });
  60. </script>
  61. <script id="script-construct">
  62. var chart = new Chart('chart-0', {
  63. type: 'line',
  64. data: {
  65. labels: labels,
  66. datasets: [{
  67. backgroundColor: Samples.color(0),
  68. borderColor: Samples.color(0),
  69. data: Samples.numbers({
  70. count: DATA_COUNT,
  71. min: 0,
  72. max: 100
  73. })
  74. }]
  75. },
  76. options: {
  77. plugins: {
  78. datalabels: {
  79. align: function(context) {
  80. return context.dataIndex % 2 ? 'end' : 'center';
  81. },
  82. backgroundColor: function(context) {
  83. return context.dataIndex % 2 ?
  84. context.dataset.borderColor :
  85. 'rgba(255, 255, 255, 0.8)';
  86. },
  87. borderColor: function(context) {
  88. return context.dataIndex % 2 ? null : context.dataset.borderColor;
  89. },
  90. color: function(context) {
  91. return context.dataIndex % 2 ? 'white' : context.dataset.borderColor;
  92. },
  93. borderWidth: function(context) {
  94. return context.dataIndex % 2 ? 0 : 1;
  95. },
  96. formatter: function(value, context) {
  97. return context.dataIndex + ': ' + Math.round(value) + '\'';
  98. },
  99. offset: 8
  100. }
  101. }
  102. }
  103. });
  104. </script>
  105. <script id="script-actions">
  106. function randomize() {
  107. chart.data.datasets.forEach(function(dataset, i) {
  108. var color = Samples.color();
  109. dataset.backgroundColor = color;
  110. dataset.borderColor = color;
  111. dataset.data = dataset.data.map(function(value) {
  112. return Samples.rand(0, 100);
  113. });
  114. });
  115. chart.update();
  116. }
  117. function addData() {
  118. chart.data.labels.push(chart.data.labels.length);
  119. chart.data.datasets.forEach(function(dataset, i) {
  120. dataset.data.push(Samples.rand(0, 100));
  121. });
  122. chart.update();
  123. }
  124. function removeData() {
  125. chart.data.labels.shift();
  126. chart.data.datasets.forEach(function(dataset, i) {
  127. dataset.data.shift();
  128. });
  129. chart.update();
  130. }
  131. </script>
  132. </body>
  133. </html>