LineChart.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div ref="lineChart" class="echarts" />
  3. </template>
  4. <script setup>
  5. import { onMounted, ref } from 'vue';
  6. import * as echarts from 'echarts';
  7. import { executeSqlByKeyAndParam } from '@/api/system/sql.js';
  8. import { useEcharts } from '@/hooks/chart.js';
  9. import { getTimeSpan } from "@/api/largeScreen/largeScreen.js"
  10. const lineChart = ref(null);
  11. const chartOption = ref({
  12. grid: {
  13. left: '30',
  14. top: '30',
  15. right: '30',
  16. bottom: '70',
  17. containLabel: true,
  18. },
  19. xAxis: {
  20. data: [],
  21. axisLine: {
  22. lineStyle: {
  23. color: '#fbffff',
  24. },
  25. },
  26. axisLabel: {
  27. interval: 3,
  28. },
  29. },
  30. yAxis: {
  31. type: 'value',
  32. axisLine: {
  33. lineStyle: {
  34. color: '#fbffff',
  35. },
  36. },
  37. },
  38. series: [
  39. {
  40. type: 'line',
  41. symbol: 'none',
  42. smooth: true,
  43. itemStyle: {
  44. color: '#fc8452',
  45. borderDashOffset: 20,
  46. },
  47. endLabel: {
  48. show: false,
  49. },
  50. areaStyle: {
  51. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  52. {
  53. offset: 0,
  54. color: 'rgb(255, 70, 131)',
  55. },
  56. {
  57. offset: 1,
  58. color: 'rgb(255, 158, 68)',
  59. },
  60. ]),
  61. },
  62. data: [],
  63. },
  64. ],
  65. });
  66. const { setOption } = useEcharts(lineChart);
  67. const pollingInterval = ref(null);
  68. onMounted(() => {
  69. const date = new Date('2023-01-01');
  70. pollingInterval.value = setInterval(fetchData(), 5000);
  71. });
  72. function fetchData() {
  73. getTimeSpan().then(res => {
  74. chartOption.value.series[0].data = res.data.map(el => el.total);
  75. chartOption.value.xAxis.data = res.data.map(el => el.hourOfDay + ':00');
  76. setOption?.(chartOption.value);
  77. }).catch(error => {
  78. console.error(error);
  79. clearInterval(pollingInterval.value);
  80. });
  81. return fetchData
  82. }
  83. </script>
  84. <style scoped>
  85. .echarts {
  86. height: 100%;
  87. width: 100%;
  88. }
  89. </style>