chart-area-demo.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Area Chart Example
  29. var ctx = document.getElementById("myAreaChart");
  30. var myLineChart = new Chart(ctx, {
  31. type: 'line',
  32. data: {
  33. labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  34. datasets: [{
  35. label: "Earnings",
  36. lineTension: 0.3,
  37. backgroundColor: "rgba(78, 115, 223, 0.05)",
  38. borderColor: "rgba(78, 115, 223, 1)",
  39. pointRadius: 3,
  40. pointBackgroundColor: "rgba(78, 115, 223, 1)",
  41. pointBorderColor: "rgba(78, 115, 223, 1)",
  42. pointHoverRadius: 3,
  43. pointHoverBackgroundColor: "rgba(78, 115, 223, 1)",
  44. pointHoverBorderColor: "rgba(78, 115, 223, 1)",
  45. pointHitRadius: 10,
  46. pointBorderWidth: 2,
  47. data: [0, 10000, 5000, 15000, 10000, 20000, 15000, 25000, 20000, 30000, 25000, 40000],
  48. }],
  49. },
  50. options: {
  51. maintainAspectRatio: false,
  52. layout: {
  53. padding: {
  54. left: 10,
  55. right: 25,
  56. top: 25,
  57. bottom: 0
  58. }
  59. },
  60. scales: {
  61. xAxes: [{
  62. time: {
  63. unit: 'date'
  64. },
  65. gridLines: {
  66. display: false,
  67. drawBorder: false
  68. },
  69. ticks: {
  70. maxTicksLimit: 7
  71. }
  72. }],
  73. yAxes: [{
  74. ticks: {
  75. maxTicksLimit: 5,
  76. padding: 10,
  77. // Include a dollar sign in the ticks
  78. callback: function(value, index, values) {
  79. return '$' + number_format(value);
  80. }
  81. },
  82. gridLines: {
  83. color: "rgb(234, 236, 244)",
  84. zeroLineColor: "rgb(234, 236, 244)",
  85. drawBorder: false,
  86. borderDash: [2],
  87. zeroLineBorderDash: [2]
  88. }
  89. }],
  90. },
  91. legend: {
  92. display: false
  93. },
  94. tooltips: {
  95. backgroundColor: "rgb(255,255,255)",
  96. bodyFontColor: "#858796",
  97. titleMarginBottom: 10,
  98. titleFontColor: '#6e707e',
  99. titleFontSize: 14,
  100. borderColor: '#dddfeb',
  101. borderWidth: 1,
  102. xPadding: 15,
  103. yPadding: 15,
  104. displayColors: false,
  105. intersect: false,
  106. mode: 'index',
  107. caretPadding: 10,
  108. callbacks: {
  109. label: function(tooltipItem, chart) {
  110. var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label || '';
  111. return datasetLabel + ': $' + number_format(tooltipItem.yLabel);
  112. }
  113. }
  114. }
  115. }
  116. });