当前位置:首页 > 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

创建可复用组件:

<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 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debo…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app…