当前位置:首页 > VUE

vue实现双折线图

2026-01-07 05:34:56VUE

实现双折线图的步骤

安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例:

npm install echarts --save

在 Vue 组件中引入 ECharts 并初始化图表:

import * as echarts from 'echarts';

创建图表容器和初始化方法:

<template>
  <div ref="chartRef" style="width: 600px; height: 400px;"></div>
</template>

mounted 钩子中初始化图表并配置双折线图选项:

mounted() {
  this.initChart();
},
methods: {
  initChart() {
    const chartDom = this.$refs.chartRef;
    const myChart = echarts.init(chartDom);

    const option = {
      title: {
        text: '双折线图示例'
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['系列1', '系列2']
      },
      xAxis: {
        type: 'category',
        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '系列1',
          type: 'line',
          data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
          name: '系列2',
          type: 'line',
          data: [220, 182, 191, 234, 290, 330, 310]
        }
      ]
    };

    myChart.setOption(option);
  }
}

响应式处理

添加窗口大小变化时的图表重绘逻辑:

mounted() {
  this.initChart();
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    if (this.myChart) {
      this.myChart.resize();
    }
  }
}

动态数据更新

如果需要动态更新数据,可以使用以下方法:

updateChart(newData1, newData2) {
  const option = {
    series: [
      { data: newData1 },
      { data: newData2 }
    ]
  };
  this.myChart.setOption(option);
}

样式自定义

可以通过 ECharts 的配置项自定义折线样式:

vue实现双折线图

series: [
  {
    name: '系列1',
    type: 'line',
    itemStyle: {
      color: '#FF0000'
    },
    lineStyle: {
      width: 3,
      type: 'dashed'
    },
    data: [120, 132, 101, 134, 90, 230, 210]
  },
  {
    name: '系列2',
    type: 'line',
    itemStyle: {
      color: '#00FF00'
    },
    smooth: true,
    data: [220, 182, 191, 234, 290, 330, 310]
  }
]

标签: 折线图vue
分享给朋友:

相关文章

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

vue 实现table

vue 实现table

Vue 实现 Table 的方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格场景。 <template> <table>…

vue实现搜索查询

vue实现搜索查询

Vue 实现搜索查询的方法 双向绑定实现实时搜索 在Vue中可以通过v-model绑定输入框的值,结合计算属性或watch监听输入变化实现实时搜索。 <template> <i…