当前位置:首页 > VUE

vue实现柱形图

2026-01-20 23:48:18VUE

使用 ECharts 实现柱形图

安装 ECharts 依赖:

npm install echarts --save

在 Vue 组件中引入 ECharts:

import * as echarts from 'echarts';

创建柱形图容器:

<template>
  <div id="barChart" style="width: 600px; height: 400px;"></div>
</template>

初始化图表并配置数据:

export default {
  mounted() {
    this.initBarChart();
  },
  methods: {
    initBarChart() {
      const chartDom = document.getElementById('barChart');
      const myChart = echarts.init(chartDom);

      const option = {
        title: {
          text: '柱形图示例'
        },
        tooltip: {},
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      };

      myChart.setOption(option);
    }
  }
}

使用 Vue-ECharts 封装组件

安装 vue-echarts:

vue实现柱形图

npm install echarts vue-echarts

创建可复用的柱形图组件:

import ECharts from 'vue-echarts';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { BarChart } from 'echarts/charts';
import {
  GridComponent,
  TooltipComponent,
  TitleComponent,
  LegendComponent
} from 'echarts/components';

use([
  CanvasRenderer,
  BarChart,
  GridComponent,
  TooltipComponent,
  TitleComponent,
  LegendComponent
]);

export default {
  components: {
    'v-chart': ECharts
  },
  props: {
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      options: {
        title: {
          text: this.chartData.title
        },
        tooltip: {},
        xAxis: {
          data: this.chartData.categories
        },
        yAxis: {},
        series: [
          {
            name: this.chartData.seriesName,
            type: 'bar',
            data: this.chartData.values
          }
        ]
      }
    };
  }
}

使用组件:

<template>
  <v-chart :options="options" autoresize />
</template>

响应式处理

添加窗口大小变化监听:

vue实现柱形图

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

动态更新数据

使用 watch 监听数据变化:

watch: {
  chartData: {
    deep: true,
    handler(newVal) {
      if (this.myChart) {
        const option = this.myChart.getOption();
        option.xAxis[0].data = newVal.categories;
        option.series[0].data = newVal.values;
        this.myChart.setOption(option);
      }
    }
  }
}

自定义样式配置

修改柱形图颜色和样式:

series: [
  {
    name: '销量',
    type: 'bar',
    data: [5, 20, 36, 10, 10, 20],
    itemStyle: {
      color: '#5470c6',
      barBorderRadius: [4, 4, 0, 0]
    },
    emphasis: {
      itemStyle: {
        color: '#83a0ed'
      }
    }
  }
]

添加渐变色效果:

itemStyle: {
  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    { offset: 0, color: '#83bff6' },
    { offset: 0.5, color: '#188df0' },
    { offset: 1, color: '#188df0' }
  ])
}

标签: vue柱形图
分享给朋友:

相关文章

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使用…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件…

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addToC…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键词…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…