AreaChart.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <script setup lang="ts">
  2. import type { Series } from '@/components/Chart/types'
  3. import type { Ref } from 'vue'
  4. import { useSettingsStore } from '@/pinia'
  5. import { storeToRefs } from 'pinia'
  6. import VueApexCharts from 'vue3-apexcharts'
  7. const { series, max, yFormatter } = defineProps<{
  8. series: Series[]
  9. max?: number
  10. yFormatter?: (value: number) => string
  11. }>()
  12. const settings = useSettingsStore()
  13. const { theme } = storeToRefs(settings)
  14. function fontColor() {
  15. return theme.value === 'dark' ? '#b4b4b4' : undefined
  16. }
  17. const chart: Ref<ApexCharts | undefined> = ref()
  18. let chartOptions = {
  19. chart: {
  20. type: 'area',
  21. zoom: {
  22. enabled: false,
  23. },
  24. animations: {
  25. enabled: false,
  26. },
  27. toolbar: {
  28. show: false,
  29. },
  30. },
  31. colors: ['#ff6385', '#36a3eb'],
  32. fill: {
  33. // type: ['solid', 'gradient'],
  34. gradient: {
  35. shade: 'light',
  36. },
  37. // colors: ['#ff6385', '#36a3eb'],
  38. },
  39. dataLabels: {
  40. enabled: false,
  41. },
  42. stroke: {
  43. curve: 'smooth',
  44. width: 0,
  45. },
  46. xaxis: {
  47. type: 'datetime',
  48. labels: {
  49. datetimeUTC: false,
  50. style: {
  51. colors: fontColor(),
  52. },
  53. },
  54. },
  55. tooltip: {
  56. enabled: false,
  57. },
  58. yaxis: {
  59. max,
  60. tickAmount: 4,
  61. min: 0,
  62. labels: {
  63. style: {
  64. colors: fontColor(),
  65. },
  66. formatter: yFormatter,
  67. },
  68. },
  69. legend: {
  70. labels: {
  71. colors: fontColor(),
  72. },
  73. onItemClick: {
  74. toggleDataSeries: false,
  75. },
  76. onItemHover: {
  77. highlightDataSeries: false,
  78. },
  79. },
  80. }
  81. function callback() {
  82. chartOptions = {
  83. ...chartOptions,
  84. ...{
  85. xaxis: {
  86. type: 'datetime',
  87. labels: {
  88. datetimeUTC: false,
  89. style: {
  90. colors: fontColor(),
  91. },
  92. },
  93. },
  94. yaxis: {
  95. max,
  96. tickAmount: 4,
  97. min: 0,
  98. labels: {
  99. style: {
  100. colors: fontColor(),
  101. },
  102. formatter: yFormatter,
  103. },
  104. },
  105. legend: {
  106. labels: {
  107. colors: fontColor(),
  108. },
  109. onItemClick: {
  110. toggleDataSeries: false,
  111. },
  112. onItemHover: {
  113. highlightDataSeries: false,
  114. },
  115. },
  116. },
  117. }
  118. chart.value?.updateOptions?.(chartOptions)
  119. }
  120. watch(theme, callback)
  121. </script>
  122. <template>
  123. <!-- Use theme as key to rerender the chart when theme changes to prevent style issues -->
  124. <VueApexCharts
  125. :key="theme"
  126. ref="chart"
  127. type="area"
  128. height="200"
  129. :options="chartOptions"
  130. :series="series"
  131. />
  132. </template>
  133. <style scoped>
  134. </style>