traffic-sources.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!-- 引用域 -->
  2. <template>
  3. <div class="visitor-trend">
  4. <div ref="chartRef" style="width: 100%; height: 300px;"></div>
  5. <div style="position: absolute;top: 20px;right: 0;">
  6. <el-select v-model="value" style="width: 84px;">
  7. <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
  8. </el-select>
  9. </div>
  10. </div>
  11. </template>
  12. <script setup>
  13. import { ref, onMounted, onBeforeUnmount } from 'vue';
  14. import * as echarts from 'echarts';
  15. const value = ref('7');
  16. const options = [
  17. {
  18. value: '7',
  19. label: '7天',
  20. selected: true,
  21. },
  22. {
  23. value: '30',
  24. label: '30天',
  25. },
  26. ]
  27. const chartRef = ref(null);
  28. let chartInstance = null;
  29. // 生成随机数据
  30. const generateRandomData = () => {
  31. const categories = Array.from({ length: 6 }, (_, i) => i + 1);
  32. return categories.map(item => ({
  33. y: '192.168.3.' + item,
  34. x: Math.floor(Math.random() * 10000) + 1 // 生成1-100的随机数
  35. }));
  36. };
  37. // 初始化图表
  38. const initChart = () => {
  39. if (chartInstance) {
  40. chartInstance.dispose();
  41. }
  42. chartInstance = echarts.init(chartRef.value);
  43. const data = generateRandomData();
  44. const option = {
  45. tooltip: {
  46. trigger: 'axis',
  47. axisPointer: {
  48. type: 'shadow'
  49. },
  50. formatter: '{b}[{c}]'
  51. },
  52. grid: {
  53. left: '0%',
  54. right: '5%',
  55. bottom: '0%',
  56. containLabel: true,
  57. },
  58. xAxis: {
  59. type: 'value',
  60. boundaryGap: [0, 0.01],
  61. axisLabel: {
  62. color: 'rgba(100, 100, 100, 1)',
  63. },
  64. axisLine: {
  65. lineStyle: {
  66. color: '#999'
  67. }
  68. },
  69. splitLine: {
  70. lineStyle: {
  71. color: 'rgba(230, 230, 230, 1)',
  72. type: 'dashed',
  73. }
  74. }
  75. },
  76. yAxis: {
  77. type: 'category',
  78. data: data.map(item => item.y),
  79. axisLabel: {
  80. color: 'rgba(100, 100, 100, 1)',
  81. formatter: function(value) {
  82. return value.match(/.{1,15}/g).join('\n');;
  83. }
  84. },
  85. axisLine: {
  86. lineStyle: {
  87. color: 'rgba(230, 230, 230, 1)',
  88. }
  89. },
  90. axisTick: {
  91. alignWithLabel: true,
  92. show: false,
  93. }
  94. },
  95. series: [
  96. {
  97. name: '数值',
  98. type: 'bar',
  99. data: data.map(item => item.x),
  100. itemStyle: {
  101. color: 'rgba(22, 122, 240, 1)',
  102. borderRadius: [0, 8, 8, 0] // 右上和右下圆角
  103. },
  104. barWidth: '16',
  105. label: {
  106. show: false,
  107. // position: 'right',
  108. // formatter: '{c}'
  109. }
  110. }
  111. ]
  112. };
  113. chartInstance.setOption(option);
  114. // 响应式调整
  115. window.addEventListener('resize', () => {
  116. chartInstance.resize();
  117. });
  118. };
  119. onMounted(() => {
  120. initChart();
  121. });
  122. onBeforeUnmount(() => {
  123. if (chartInstance) {
  124. window.removeEventListener('resize', () => {
  125. chartInstance.resize();
  126. });
  127. chartInstance.dispose();
  128. }
  129. });
  130. </script>