CPUChart.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <apexchart type="area" height="200" :options="chartOptions" :series="series" ref="chart"/>
  3. </template>
  4. <script>
  5. import VueApexCharts from 'vue-apexcharts'
  6. import Vue from 'vue'
  7. Vue.use(VueApexCharts)
  8. Vue.component('apexchart', VueApexCharts)
  9. const fontColor = () => {
  10. return window.matchMedia('(prefers-color-scheme: dark)').matches ? '#b4b4b4' : undefined
  11. }
  12. export default {
  13. name: 'CPUChart',
  14. props: {
  15. series: Array
  16. },
  17. watch: {
  18. series: {
  19. deep: true,
  20. handler() {
  21. this.$refs.chart.updateSeries(this.series)
  22. }
  23. }
  24. },
  25. mounted() {
  26. let media = window.matchMedia('(prefers-color-scheme: dark)')
  27. let callback = () => {
  28. this.chartOptions.xaxis = {
  29. type: 'datetime',
  30. labels: {
  31. datetimeUTC: false,
  32. style: {
  33. colors: fontColor()
  34. }
  35. }
  36. }
  37. this.chartOptions.yaxis = {
  38. max: 100,
  39. tickAmount: 4,
  40. min: 0,
  41. labels: {
  42. style: {
  43. colors: fontColor()
  44. }
  45. }
  46. }
  47. this.chartOptions.legend = {
  48. labels: {
  49. colors: fontColor()
  50. },
  51. onItemClick: {
  52. toggleDataSeries: false
  53. },
  54. onItemHover: {
  55. highlightDataSeries: false
  56. },
  57. }
  58. this.$refs.chart.updateOptions(this.chartOptions)
  59. }
  60. if (typeof media.addEventListener === 'function') {
  61. media.addEventListener('change', callback)
  62. } else if (typeof media.addListener === 'function') {
  63. media.addListener(callback)
  64. }
  65. },
  66. data() {
  67. return {
  68. chartOptions: {
  69. series: this.series,
  70. chart: {
  71. type: 'area',
  72. zoom: {
  73. enabled: false
  74. },
  75. animations: {
  76. enabled: false,
  77. },
  78. toolbar: {
  79. show: false
  80. },
  81. },
  82. colors: ['#ff6385', '#36a3eb'],
  83. fill: {
  84. // type: ['solid', 'gradient'],
  85. gradient: {
  86. shade: 'light'
  87. }
  88. //colors: ['#ff6385', '#36a3eb'],
  89. },
  90. dataLabels: {
  91. enabled: false
  92. },
  93. stroke: {
  94. curve: 'smooth',
  95. width: 0,
  96. },
  97. xaxis: {
  98. type: 'datetime',
  99. labels: {
  100. datetimeUTC: false,
  101. style: {
  102. colors: fontColor()
  103. }
  104. }
  105. },
  106. tooltip: {
  107. enabled: false
  108. },
  109. yaxis: {
  110. max: 100,
  111. tickAmount: 4,
  112. min: 0,
  113. labels: {
  114. style: {
  115. colors: fontColor()
  116. }
  117. }
  118. },
  119. legend: {
  120. labels: {
  121. colors: fontColor()
  122. },
  123. onItemClick: {
  124. toggleDataSeries: false
  125. },
  126. onItemHover: {
  127. highlightDataSeries: false
  128. },
  129. }
  130. },
  131. }
  132. },
  133. }
  134. </script>
  135. <style scoped>
  136. </style>