annotations-example.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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>Line Chart with Annotations</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. <script src="../../assets/stock-prices.js"></script>
  31. </head>
  32. <body>
  33. <div id="chart"></div>
  34. <script>
  35. var options = {
  36. series: [{
  37. type: 'bar',
  38. data: series.monthDataSeries2.prices
  39. }, {
  40. type: 'line',
  41. data: series.monthDataSeries1.prices
  42. }],
  43. chart: {
  44. type: 'line',
  45. id: 'chart',
  46. sparkline: {
  47. // enabled: true
  48. }
  49. },
  50. annotations: {
  51. yaxis: [{
  52. y: 8200,
  53. borderColor: '#FEB019',
  54. label: {
  55. borderColor: '#333',
  56. style: {
  57. fontSize: '15px',
  58. color: '#333',
  59. background: '#FEB019',
  60. },
  61. text: 'Y-axis annotation',
  62. }
  63. }],
  64. xaxis: [{
  65. x: new Date('23 Nov 2017').getTime(),
  66. borderColor: '#00E396',
  67. label: {
  68. borderColor: '#00E396',
  69. style: {
  70. fontSize: '15px',
  71. color: '#fff',
  72. background: '#00E396',
  73. },
  74. offsetY: -10,
  75. text: 'Vertical',
  76. }
  77. }],
  78. points: [{
  79. x: new Date('01 Dec 2017').getTime(),
  80. y: 9025,
  81. label: {
  82. borderColor: '#FF4560',
  83. offsetY: 0,
  84. style: {
  85. fontSize: '15px',
  86. color: '#fff',
  87. background: '#FF4560',
  88. },
  89. text: 'All time high',
  90. }
  91. }]
  92. },
  93. plotOptions: {
  94. bar: {
  95. columnWidth: '50%'
  96. }
  97. },
  98. markers: {
  99. size: 0
  100. },
  101. dataLabels: {
  102. enabled: false
  103. },
  104. stroke: {
  105. curve: 'straight'
  106. },
  107. legend: {
  108. show: false,
  109. },
  110. labels: series.monthDataSeries1.dates,
  111. xaxis: {
  112. type: 'datetime',
  113. }
  114. };
  115. var chart = new ApexCharts(document.querySelector("#chart"), options);
  116. chart.render();
  117. function addAnnos() {
  118. // adding annotation through chart instance by calling direct method
  119. chart.addYaxisAnnotation({
  120. id: 'yaxis-anno',
  121. y: 9000,
  122. borderColor: '#FEB019',
  123. label: {
  124. borderColor: '#333',
  125. style: {
  126. fontSize: '15px',
  127. color: '#333',
  128. background: '#FEB019'
  129. },
  130. text: 'Y-axis - runtime'
  131. }
  132. })
  133. chart.addXaxisAnnotation({
  134. id: 'xaxis-anno',
  135. x: new Date('25 Nov 2017').getTime(),
  136. borderColor: '#00E396',
  137. label: {
  138. orientation: 'vertical',
  139. borderColor: '#00E396',
  140. style: {
  141. fontSize: '15px',
  142. color: '#fff',
  143. background: '#00E396'
  144. },
  145. offsetY: -10,
  146. text: 'xaxis - runtime'
  147. }
  148. })
  149. }
  150. addAnnos()
  151. chart.addPointAnnotation({
  152. id: 'point-anno',
  153. x: new Date('17 Nov 2017').getTime(),
  154. y: 9425,
  155. label: {
  156. borderColor: '#FF4560',
  157. offsetY: 0,
  158. style: {
  159. fontSize: '15px',
  160. color: '#fff',
  161. background: '#FF4560'
  162. },
  163. text: 'Point - runtime'
  164. }
  165. })
  166. // removing annotation with method name
  167. chart.removeAnnotation('point-anno')
  168. // removing annotations with .exec() method
  169. ApexCharts.exec('chart', 'removeAnnotation', 'xaxis-anno')
  170. ApexCharts.exec('chart', 'removeAnnotation', 'yaxis-anno')
  171. // add annos again
  172. // addAnnos()
  173. </script>
  174. </body>
  175. </html>