bar-with-custom-data-labels.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Bar with Custom DataLabels</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. #chart {
  11. max-width: 650px;
  12. margin: 35px auto;
  13. }
  14. </style>
  15. <script>
  16. window.Promise ||
  17. document.write(
  18. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  19. )
  20. window.Promise ||
  21. document.write(
  22. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  23. )
  24. window.Promise ||
  25. document.write(
  26. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  27. )
  28. </script>
  29. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  30. </head>
  31. <body>
  32. <div id="chart"></div>
  33. <script>
  34. var options = {
  35. series: [{
  36. data: [400, 430, 448, 470, 540, 580, 690, 1100, 1200, 1380]
  37. }],
  38. chart: {
  39. type: 'bar',
  40. height: 380
  41. },
  42. plotOptions: {
  43. bar: {
  44. barHeight: '100%',
  45. distributed: true,
  46. horizontal: true,
  47. dataLabels: {
  48. position: 'bottom'
  49. },
  50. }
  51. },
  52. colors: ['#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e',
  53. '#f48024', '#69d2e7'
  54. ],
  55. dataLabels: {
  56. enabled: true,
  57. textAnchor: 'start',
  58. style: {
  59. colors: ['#fff']
  60. },
  61. formatter: function (val, opt) {
  62. return opt.w.globals.labels[opt.dataPointIndex] + ": " + val
  63. },
  64. offsetX: 0,
  65. dropShadow: {
  66. enabled: true
  67. }
  68. },
  69. stroke: {
  70. width: 1,
  71. colors: ['#fff']
  72. },
  73. xaxis: {
  74. categories: ['South Korea', 'Canada', 'United Kingdom', 'Netherlands', 'Italy', 'France', 'Japan',
  75. 'United States', 'China', 'India'
  76. ],
  77. },
  78. yaxis: {
  79. labels: {
  80. show: false
  81. }
  82. },
  83. title: {
  84. text: 'Custom DataLabels',
  85. align: 'center',
  86. floating: true
  87. },
  88. subtitle: {
  89. text: 'Category Names as DataLabels inside bars',
  90. align: 'center',
  91. },
  92. tooltip: {
  93. theme: 'dark',
  94. x: {
  95. show: false
  96. },
  97. y: {
  98. title: {
  99. formatter: function () {
  100. return ''
  101. }
  102. }
  103. }
  104. }
  105. };
  106. var chart = new ApexCharts(document.querySelector("#chart"), options);
  107. chart.render();
  108. </script>
  109. </body>
  110. </html>