custom-legend.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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>Custom Legend</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. #chart-wrap {
  11. max-width: 650px;
  12. position: relative;
  13. margin: 35px auto;
  14. }
  15. #chart {
  16. padding-bottom: 30px;
  17. max-width: 650px;
  18. }
  19. .legend {
  20. max-width: 650px;
  21. left: 80px;
  22. bottom: 20px;
  23. position: absolute;
  24. text-align: center;
  25. margin: 0 auto;
  26. }
  27. </style>
  28. <script>
  29. window.Promise ||
  30. document.write(
  31. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  32. )
  33. window.Promise ||
  34. document.write(
  35. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  36. )
  37. window.Promise ||
  38. document.write(
  39. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  40. )
  41. </script>
  42. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  43. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pretty-checkbox@3.0/dist/pretty-checkbox.min.css">
  44. </head>
  45. <body>
  46. <div id="chart-wrap">
  47. <div id="chart"></div>
  48. <div class="legend">
  49. <div class="pretty p-svg p-smooth">
  50. <input type="checkbox" checked onclick="toggleSeries(this)" value="Series Column">
  51. <div class="state p-primary">
  52. <svg class="svg svg-icon" viewBox="0 0 20 20">
  53. <path d="M7.629,14.566c0.125,0.125,0.291,0.188,0.456,0.188c0.164,0,0.329-0.062,0.456-0.188l8.219-8.221c0.252-0.252,0.252-0.659,0-0.911c-0.252-0.252-0.659-0.252-0.911,0l-7.764,7.763L4.152,9.267c-0.252-0.251-0.66-0.251-0.911,0c-0.252,0.252-0.252,0.66,0,0.911L7.629,14.566z" style="stroke: #fff;fill:#fff;"></path>
  54. </svg>
  55. <label>Column</label>
  56. </div>
  57. </div>
  58. <div class="pretty p-svg p-smooth">
  59. <input type="checkbox" checked onclick="toggleSeries(this)" value="Series Area">
  60. <div class="state p-success">
  61. <svg class="svg svg-icon" viewBox="0 0 20 20">
  62. <path d="M7.629,14.566c0.125,0.125,0.291,0.188,0.456,0.188c0.164,0,0.329-0.062,0.456-0.188l8.219-8.221c0.252-0.252,0.252-0.659,0-0.911c-0.252-0.252-0.659-0.252-0.911,0l-7.764,7.763L4.152,9.267c-0.252-0.251-0.66-0.251-0.911,0c-0.252,0.252-0.252,0.66,0,0.911L7.629,14.566z" style="stroke: #fff;fill:#fff;"></path>
  63. </svg>
  64. <label>Area</label>
  65. </div>
  66. </div>
  67. <div class="pretty p-svg p-smooth">
  68. <input type="checkbox" onclick="toggleSeries(this)" value="Series Line">
  69. <div class="state p-warning">
  70. <svg class="svg svg-icon" viewBox="0 0 20 20">
  71. <path d="M7.629,14.566c0.125,0.125,0.291,0.188,0.456,0.188c0.164,0,0.329-0.062,0.456-0.188l8.219-8.221c0.252-0.252,0.252-0.659,0-0.911c-0.252-0.252-0.659-0.252-0.911,0l-7.764,7.763L4.152,9.267c-0.252-0.251-0.66-0.251-0.911,0c-0.252,0.252-0.252,0.66,0,0.911L7.629,14.566z" style="stroke: #fff;fill:#fff;"></path>
  72. </svg>
  73. <label>Line</label>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. <script>
  79. var options = {
  80. series: [{
  81. name: 'Series Column',
  82. type: 'column',
  83. data: [23, 11, 22, 27, 13, 22, 37, 21, 44, 22, 30]
  84. }, {
  85. name: 'Series Area',
  86. type: 'area',
  87. data: [44, 55, 41, 67, 22, 43, 21, 41, 56, 27, 43]
  88. }, {
  89. name: 'Series Line',
  90. type: 'line',
  91. data: [30, 25, 36, 30, 45, 35, 64, 52, 59, 36, 39]
  92. }],
  93. chart: {
  94. height: 310,
  95. type: 'line',
  96. stacked: false,
  97. },
  98. stroke: {
  99. width: [0, 2, 5],
  100. curve: 'smooth'
  101. },
  102. plotOptions: {
  103. bar: {
  104. columnWidth: '50%'
  105. }
  106. },
  107. fill: {
  108. opacity: [0.85,0.25,1],
  109. gradient: {
  110. inverseColors: false,
  111. shade: 'light',
  112. type: "vertical",
  113. opacityFrom: 0.85,
  114. opacityTo: 0.55,
  115. stops: [0, 100, 100, 100]
  116. }
  117. },
  118. labels: ['01/01/2003', '02/01/2003','03/01/2003','04/01/2003','05/01/2003','06/01/2003','07/01/2003','08/01/2003','09/01/2003','10/01/2003','11/01/2003'],
  119. markers: {
  120. size: 0
  121. },
  122. xaxis: {
  123. type:'datetime'
  124. },
  125. yaxis: {
  126. title: {
  127. text: 'Points',
  128. },
  129. min: 0
  130. },
  131. legend: {
  132. show: false
  133. },
  134. tooltip: {
  135. shared: true,
  136. intersect: false,
  137. y: {
  138. formatter: function (y) {
  139. if(typeof y !== "undefined") {
  140. return y.toFixed(0) + " points";
  141. }
  142. return y;
  143. }
  144. }
  145. }
  146. };
  147. var chart = new ApexCharts(document.querySelector("#chart"), options);
  148. chart.render();
  149. // check if the checkbox has any unchecked item
  150. checkLegends()
  151. function checkLegends() {
  152. var allLegends = document.querySelectorAll(".legend input[type='checkbox']")
  153. for(var i = 0; i < allLegends.length; i++) {
  154. if(!allLegends[i].checked) {
  155. chart.toggleSeries(allLegends[i].value)
  156. }
  157. }
  158. }
  159. // toggleSeries accepts a single argument which should match the series name you're trying to toggle
  160. function toggleSeries(checkbox) {
  161. chart.toggleSeries(checkbox.value)
  162. }
  163. </script>
  164. </body>
  165. </html>