index-charts.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. /* Chart.js docs: https://www.chartjs.org/ */
  3. window.chartColors = {
  4. green: '#75c181',
  5. gray: '#a9b5c9',
  6. text: '#252930',
  7. border: '#e7e9ed'
  8. };
  9. /* Random number generator for demo purpose */
  10. var randomDataPoint = function(){ return Math.round(Math.random()*10000)};
  11. //Chart.js Line Chart Example
  12. var lineChartConfig = {
  13. type: 'line',
  14. data: {
  15. labels: ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6', 'Day 7'],
  16. datasets: [{
  17. label: 'Current week',
  18. fill: false,
  19. backgroundColor: window.chartColors.green,
  20. borderColor: window.chartColors.green,
  21. data: [
  22. randomDataPoint(),
  23. randomDataPoint(),
  24. randomDataPoint(),
  25. randomDataPoint(),
  26. randomDataPoint(),
  27. randomDataPoint(),
  28. randomDataPoint()
  29. ],
  30. }, {
  31. label: 'Previous week',
  32. borderDash: [3, 5],
  33. backgroundColor: window.chartColors.gray,
  34. borderColor: window.chartColors.gray,
  35. data: [
  36. randomDataPoint(),
  37. randomDataPoint(),
  38. randomDataPoint(),
  39. randomDataPoint(),
  40. randomDataPoint(),
  41. randomDataPoint(),
  42. randomDataPoint()
  43. ],
  44. fill: false,
  45. }]
  46. },
  47. options: {
  48. responsive: true,
  49. aspectRatio: 1.5,
  50. legend: {
  51. display: true,
  52. position: 'bottom',
  53. align: 'end',
  54. },
  55. title: {
  56. display: true,
  57. text: 'Chart.js Line Chart Example',
  58. },
  59. tooltips: {
  60. mode: 'index',
  61. intersect: false,
  62. titleMarginBottom: 10,
  63. bodySpacing: 10,
  64. xPadding: 16,
  65. yPadding: 16,
  66. borderColor: window.chartColors.border,
  67. borderWidth: 1,
  68. backgroundColor: '#fff',
  69. bodyFontColor: window.chartColors.text,
  70. titleFontColor: window.chartColors.text,
  71. callbacks: {
  72. //Ref: https://stackoverflow.com/questions/38800226/chart-js-add-commas-to-tooltip-and-y-axis
  73. label: function(tooltipItem, data) {
  74. if (parseInt(tooltipItem.value) >= 1000) {
  75. return "$" + tooltipItem.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  76. } else {
  77. return '$' + tooltipItem.value;
  78. }
  79. }
  80. },
  81. },
  82. hover: {
  83. mode: 'nearest',
  84. intersect: true
  85. },
  86. scales: {
  87. xAxes: [{
  88. display: true,
  89. gridLines: {
  90. drawBorder: false,
  91. color: window.chartColors.border,
  92. },
  93. scaleLabel: {
  94. display: false,
  95. }
  96. }],
  97. yAxes: [{
  98. display: true,
  99. gridLines: {
  100. drawBorder: false,
  101. color: window.chartColors.border,
  102. },
  103. scaleLabel: {
  104. display: false,
  105. },
  106. ticks: {
  107. beginAtZero: true,
  108. userCallback: function(value, index, values) {
  109. return '$' + value.toLocaleString(); //Ref: https://stackoverflow.com/questions/38800226/chart-js-add-commas-to-tooltip-and-y-axis
  110. }
  111. },
  112. }]
  113. }
  114. }
  115. };
  116. // Chart.js Bar Chart Example
  117. var barChartConfig = {
  118. type: 'bar',
  119. data: {
  120. labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  121. datasets: [{
  122. label: 'Orders',
  123. backgroundColor: window.chartColors.green,
  124. borderColor: window.chartColors.green,
  125. borderWidth: 1,
  126. maxBarThickness: 16,
  127. data: [
  128. 23,
  129. 45,
  130. 76,
  131. 75,
  132. 62,
  133. 37,
  134. 83
  135. ]
  136. }]
  137. },
  138. options: {
  139. responsive: true,
  140. aspectRatio: 1.5,
  141. legend: {
  142. position: 'bottom',
  143. align: 'end',
  144. },
  145. title: {
  146. display: true,
  147. text: 'Chart.js Bar Chart Example'
  148. },
  149. tooltips: {
  150. mode: 'index',
  151. intersect: false,
  152. titleMarginBottom: 10,
  153. bodySpacing: 10,
  154. xPadding: 16,
  155. yPadding: 16,
  156. borderColor: window.chartColors.border,
  157. borderWidth: 1,
  158. backgroundColor: '#fff',
  159. bodyFontColor: window.chartColors.text,
  160. titleFontColor: window.chartColors.text,
  161. },
  162. scales: {
  163. xAxes: [{
  164. display: true,
  165. gridLines: {
  166. drawBorder: false,
  167. color: window.chartColors.border,
  168. },
  169. }],
  170. yAxes: [{
  171. display: true,
  172. gridLines: {
  173. drawBorder: false,
  174. color: window.chartColors.borders,
  175. },
  176. }]
  177. }
  178. }
  179. }
  180. // Generate charts on load
  181. window.addEventListener('load', function(){
  182. var lineChart = document.getElementById('canvas-linechart').getContext('2d');
  183. window.myLine = new Chart(lineChart, lineChartConfig);
  184. var barChart = document.getElementById('canvas-barchart').getContext('2d');
  185. window.myBar = new Chart(barChart, barChartConfig);
  186. });