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

响应式柱状图实现

添加窗口大小变化监听:

柱状图vue实现

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
      }]
    });
  }
}

多系列柱状图实现

配置多个系列数据:

柱状图vue实现

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 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…