chart-bar-demo.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Set new default font family and font color to mimic Bootstrap's default styling
  2. Chart.defaults.global.defaultFontFamily = 'Nunito', '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
  3. Chart.defaults.global.defaultFontColor = '#858796';
  4. function number_format(number, decimals, dec_point, thousands_sep) {
  5. // * example: number_format(1234.56, 2, ',', ' ');
  6. // * return: '1 234,56'
  7. number = (number + '').replace(',', '').replace(' ', '');
  8. var n = !isFinite(+number) ? 0 : +number,
  9. prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
  10. sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
  11. dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
  12. s = '',
  13. toFixedFix = function(n, prec) {
  14. var k = Math.pow(10, prec);
  15. return '' + Math.round(n * k) / k;
  16. };
  17. // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  18. s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  19. if (s[0].length > 3) {
  20. s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  21. }
  22. if ((s[1] || '').length < prec) {
  23. s[1] = s[1] || '';
  24. s[1] += new Array(prec - s[1].length + 1).join('0');
  25. }
  26. return s.join(dec);
  27. }
  28. // Bar Chart Example
  29. var ctx = document.getElementById("myBarChart");
  30. var myBarChart = new Chart(ctx, {
  31. type: 'bar',
  32. data: {
  33. labels: ["January", "February", "March", "April", "May", "June"],
  34. datasets: [{
  35. label: "Revenue",
  36. backgroundColor: "#4e73df",
  37. hoverBackgroundColor: "#2e59d9",
  38. borderColor: "#4e73df",
  39. data: [4215, 5312, 6251, 7841, 9821, 14984],
  40. }],
  41. },
  42. options: {
  43. maintainAspectRatio: false,
  44. layout: {
  45. padding: {
  46. left: 10,
  47. right: 25,
  48. top: 25,
  49. bottom: 0
  50. }
  51. },
  52. scales: {
  53. xAxes: [{
  54. time: {
  55. unit: 'month'
  56. },
  57. gridLines: {
  58. display: false,
  59. drawBorder: false
  60. },
  61. ticks: {
  62. maxTicksLimit: 6
  63. },
  64. maxBarThickness: 25,
  65. }],
  66. yAxes: [{
  67. ticks: {
  68. min: 0,
  69. max: 15000,
  70. maxTicksLimit: 5,
  71. padding: 10,
  72. // Include a dollar sign in the ticks
  73. callback: function(value, index, values) {
  74. return '$' + number_format(value);
  75. }
  76. },
  77. gridLines: {
  78. color: "rgb(234, 236, 244)",
  79. zeroLineColor: "rgb(234, 236, 244)",
  80. drawBorder: false,
  81. borderDash: [2],
  82. zeroLineBorderDash: [2]
  83. }
  84. }],
  85. },
  86. legend: {
  87. display: false
  88. },
  89. tooltips: {
  90. titleMarginBottom: 10,
  91. titleFontColor: '#6e707e',
  92. titleFontSize: 14,
  93. backgroundColor: "rgb(255,255,255)",
  94. bodyFontColor: "#858796",
  95. borderColor: '#dddfeb',
  96. borderWidth: 1,
  97. xPadding: 15,
  98. yPadding: 15,
  99. displayColors: false,
  100. caretPadding: 10,
  101. callbacks: {
  102. label: function(tooltipItem, chart) {
  103. var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label || '';
  104. return datasetLabel + ': $' + number_format(tooltipItem.yLabel);
  105. }
  106. }
  107. },
  108. }
  109. });