123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <!-- 引用域 -->
- <template>
- <div class="visitor-trend">
- <div ref="chartRef" style="width: 100%; height: 300px;"></div>
- <div style="position: absolute;top: 20px;right: 0;">
- <el-select v-model="value" style="width: 84px;">
- <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
- </el-select>
- </div>
- </div>
- </template>
- <script setup>
- import { ref, onMounted, onBeforeUnmount } from 'vue';
- import * as echarts from 'echarts';
- const value = ref('7');
- const options = [
- {
- value: '7',
- label: '7天',
- selected: true,
- },
- {
- value: '30',
- label: '30天',
- },
- ]
- const chartRef = ref(null);
- let chartInstance = null;
- // 生成随机数据
- const generateRandomData = () => {
- const categories = Array.from({ length: 6 }, (_, i) => i + 1);
- return categories.map(item => ({
- y: '192.168.3.' + item,
- x: Math.floor(Math.random() * 10000) + 1 // 生成1-100的随机数
- }));
- };
- // 初始化图表
- const initChart = () => {
- if (chartInstance) {
- chartInstance.dispose();
- }
-
- chartInstance = echarts.init(chartRef.value);
- const data = generateRandomData();
-
- const option = {
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'shadow'
- },
- formatter: '{b}[{c}]'
- },
- grid: {
- left: '0%',
- right: '5%',
- bottom: '0%',
- containLabel: true,
- },
- xAxis: {
- type: 'value',
- boundaryGap: [0, 0.01],
- axisLabel: {
- color: 'rgba(100, 100, 100, 1)',
- },
- axisLine: {
- lineStyle: {
- color: '#999'
- }
- },
- splitLine: {
- lineStyle: {
- color: 'rgba(230, 230, 230, 1)',
- type: 'dashed',
- }
- }
- },
- yAxis: {
- type: 'category',
- data: data.map(item => item.y),
- axisLabel: {
- color: 'rgba(100, 100, 100, 1)',
- formatter: function(value) {
- return value.match(/.{1,15}/g).join('\n');;
- }
- },
- axisLine: {
- lineStyle: {
- color: 'rgba(230, 230, 230, 1)',
- }
- },
- axisTick: {
- alignWithLabel: true,
- show: false,
- }
- },
- series: [
- {
- name: '数值',
- type: 'bar',
- data: data.map(item => item.x),
- itemStyle: {
- color: 'rgba(22, 122, 240, 1)',
- borderRadius: [0, 8, 8, 0] // 右上和右下圆角
- },
- barWidth: '16',
- label: {
- show: false,
- // position: 'right',
- // formatter: '{c}'
- }
- }
- ]
- };
-
- chartInstance.setOption(option);
-
- // 响应式调整
- window.addEventListener('resize', () => {
- chartInstance.resize();
- });
- };
- onMounted(() => {
- initChart();
- });
- onBeforeUnmount(() => {
- if (chartInstance) {
- window.removeEventListener('resize', () => {
- chartInstance.resize();
- });
- chartInstance.dispose();
- }
- });
- </script>
|