当前位置:首页 > VUE

柱状图vue实现

2026-01-22 04:42:49VUE

使用 Vue 和 ECharts 实现柱状图

安装 ECharts 依赖:

npm install echarts --save

在 Vue 组件中引入 ECharts:

import * as echarts from 'echarts';

创建基础柱状图组件:

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

<script>
export default {
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.chartDom;
      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);
    }
  }
}
</script>

响应式柱状图实现

添加窗口大小变化监听:

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

动态数据柱状图

通过 props 接收动态数据:

props: {
  chartData: {
    type: Object,
    required: true
  }
},
watch: {
  chartData: {
    deep: true,
    handler(newVal) {
      this.updateChart(newVal);
    }
  }
},
methods: {
  updateChart(data) {
    this.myChart.setOption({
      xAxis: {
        data: data.categories
      },
      series: [{
        data: data.values
      }]
    });
  }
}

多系列柱状图实现

配置多个系列数据:

const option = {
  xAxis: {
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: {},
  series: [
    {
      name: '线上',
      type: 'bar',
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: '线下',
      type: 'bar',
      data: [220, 182, 191, 234, 290, 330, 310]
    }
  ]
};

自定义样式柱状图

添加样式配置:

series: [{
  name: '销量',
  type: 'bar',
  itemStyle: {
    color: '#4ad8ff',
    borderRadius: [4, 4, 0, 0]
  },
  barWidth: '40%',
  data: [5, 20, 36, 10, 10, 20]
}]

使用 Vue-ECharts 封装组件

安装 vue-echarts:

npm install echarts vue-echarts

创建可复用组件:

柱状图vue实现

<template>
  <v-chart :option="chartOption" 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 },
  props: {
    data: Array,
    categories: Array
  },
  computed: {
    chartOption() {
      return {
        xAxis: {
          type: 'category',
          data: this.categories
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.data,
          type: 'bar'
        }]
      };
    }
  }
};
</script>

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

相关文章

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…