当前位置:首页 > VUE

vue实现柱形图

2026-02-21 15:26:38VUE

Vue 中使用 ECharts 实现柱形图

安装 ECharts 依赖

npm install echarts --save

引入 ECharts 并初始化图表

import * as echarts from 'echarts';

在 Vue 组件中创建图表容器

<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: {},
        legend: {
          data: ['销量']
        },
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      };

      myChart.setOption(option);
    }
  }
}

响应式调整

添加窗口大小变化监听

methods: {
  initBarChart() {
    // ...之前的初始化代码

    window.addEventListener('resize', function() {
      myChart.resize();
    });
  }
}

使用 Vue-ECharts 封装组件

安装 vue-echarts

npm install vue-echarts

封装可复用组件

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

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

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

export default {
  components: { VChart },
  data() {
    return {
      option: {
        // 同上配置
      }
    }
  }
}
</script>

动态数据更新

通过 props 传递数据

props: {
  chartData: {
    type: Array,
    required: true
  },
  xAxisData: {
    type: Array,
    required: true
  }
},
watch: {
  chartData: {
    handler(newVal) {
      this.option.series[0].data = newVal;
      this.$refs.chart.setOption(this.option);
    },
    deep: true
  }
}

自定义样式配置

设置柱形图样式

vue实现柱形图

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

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

相关文章

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现例子

vue实现例子

以下是一些常见的 Vue 实现例子,涵盖基础功能到进阶应用场景: 基础数据绑定 使用 v-model 实现双向数据绑定: <template> <div> &l…

mui实现vue

mui实现vue

mui 实现 Vue 的步骤 mui 是一个轻量级的前端框架,常用于移动端开发。结合 Vue 可以实现高效的开发体验。以下是具体实现方法。 安装 mui 和 Vue 通过 npm 或 yarn 安装…

简单实现vue github

简单实现vue github

实现一个简单的 Vue 项目并上传到 GitHub 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保已安装 Node.js 和 npm/yarn。运行以下命令创建项目:…

vue实现div单选

vue实现div单选

Vue 实现 div 单选 在 Vue 中实现 div 单选可以通过 v-model 结合 v-for 和事件绑定来完成。以下是具体实现方法: 模板部分 <template> &l…