【Echarts】Vue项目echarts柱状堆叠图配置代码

前言

注意:请先引入 ecahrts,如果不会则直接跳到第三步

一、效果图:

在这里插入图片描述

二、完整代码:

<template>
  <div class="echart">
    <!-- 这个 div 就会被解析为 echarts图 -->
    <div class="barChart" ref="barChart"></div>
  </div>
</template>

<script>
export default {
  mounted() {
    this.initBarChart();
  },
  methods: {
    initBarChart() {
      let myChart = this.$echarts.init(this.$refs.barChart);
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        grid: {
          //调整图表位置
          show: false, //是否显示直角坐标系网格
          top: "15%", // 一下数值可为百分比也可为具体像素值
          right: "5%",
          bottom: "10%",
          left: "8%",
        },
        legend: {
          top: "5%", // 控制 板块控制器的位置
          // icon: 'rect',//形状  类型包括 circle,rect,line,roundRect,triangle,diamond,pin,arrow,none
          // itemWidth: 10,  // 设置宽度
          // itemHeight: 4, // 设置高度
          itemGap: 80, // 设置顶部图标间距
          right: "center",
          data: [
            {
              name: "A柱子的名字",
              textStyle: {
                color: "#fff", // 单独设置某一个图列的颜色
              },
            },
            {
              name: "B柱子的名字",
              textStyle: {
                color: "#fff", // 单独设置某一个图列的颜色
              },
            },
          ],
        },
        xAxis: [
          {
            type: "category",
            data: [
              "1月", "2月","3月","4月","5月","6月", "7月","8月","9月","10月","11月","12月",
            ],
            axisPointer: {
              type: "shadow",
            },
            axisLine: {
              //横轴样式
              lineStyle: {
                color: "#08426D",
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#85B0C4",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#08426D"],
                width: 1,
                type: "solid",
              },
              textStyle: {
                color: "green",
              },
            },
            axisLabel: {
              show: true,
              formatter: "{value}",
              textStyle: {
                color: "#85B0C4",
              },
            },
          },
        ],
        series: [
          {
            name: "A柱子的名字",
            type: "bar",
            barWidth: "25%", // 设置柱子的宽度
            stack: "柱子", //同个类目轴上系列配置相同的stack值可以堆叠放置
            data: [20, 30, 40, 60, 20, 50, 40, 30, 30, 50, 40, 30],
            itemStyle: {
              color: {
                type: "linear",
                x: 0, // 右
                y: 1, // 下
                x2: 0, // 左
                y2: 0, // 上
                colorStops: [
                  {
                    offset: 0,
                    color: "#013560", // 0% 处的颜色
                  },
                  {
                    offset: 0.9,
                    color: "#00F5EE", // 90% 处的颜色
                  },
                ],
              },
            },
          },
          {
            name: "B柱子的名字",
            type: "bar",
            barWidth: "25%", // 设置柱子的宽度
            stack: "柱子", //同个类目轴上系列配置相同的stack值可以堆叠放置
            data: [50, 40, 70, 15, 30, 45, 25, 60, 70, 30, 10, 65],
            itemStyle: {
              color: {
                type: "linear",
                x: 0, // 右
                y: 1, // 下
                x2: 0, // 左
                y2: 0, // 上
                colorStops: [
                  {
                    offset: 0,
                    color: "#1B2E6E", // 0% 处的颜色
                  },
                  {
                    offset: 0.9,
                    color: "#00C0FF", // 90% 处的颜色
                  },
                ],
              },
            },
            label: {
              show: true, // 开启显示
              position: "top", // 在上方显示
              distance: 10, // 距离图形元素的距离。当 position 为字符描述值(如 'top'、'insideRight')时候有效。
              verticalAlign: "middle",
              textStyle: {
                // 数值样式
                color: "#D2CC17",
                fontSize: 12,
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
  },
};
</script>
<style scoped lang="less">
.echart {
  width: 800px;
  height: 400px;
  margin: auto;
  border: 3px solid lightcoral;
  background-color: #0a1a3b;

  //  宽高是必须给的,可以给百分比、具体的像素等....
  .barChart {
    width: 800px;
    height: 400px;
  }
}
</style>

三、echarts引入

echarts引入教程:https://blog.csdn.net/qq_61402485/article/details/127131530
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ECharts 是一个强大的数据可视化库,用于创建交互式表。在 Vue 中使用 ECharts 来创建柱状图的步骤如下: 1. 首先,你需要安装 EChartsVue 的兼容版本。在你的 Vue 项目中,可以通过 npm 或 yarn 安装 ECharts: ```bash npm install echarts vue-echarts # 或者 yarn add echarts vue-echarts ``` 2. 在你的组件中导入 EChartsVue-ECharts 插件: ```javascript import ECharts from 'echarts'; import { EChartsCore } from 'vue-echarts/core'; import 'echarts/theme/macarons'; // 选择你喜欢的主题 ``` 3. 注册 Vue-ECharts 插件: ```javascript Vue.use(EChartsCore, { options: { theme: 'macarons', // 设置主题 }, }); ``` 4. 使用模板或 JSX 语法创建柱状图实例,并绑定数据到表: ```html <template> <div id="chart" style="height: 400px;"></div> </template> <script> export default { data() { return { chartData: [ { name: 'Series 1', value: 30 }, { name: 'Series 2', value: 40 }, { name: 'Series 3', value: 50 }, ], }; }, mounted() { this.renderChart(); }, methods: { renderChart() { const myChart = this.$echarts.init(document.getElementById('chart')); const option = { title: { text: '柱状图示例', }, xAxis: { data: this.chartData.map(item => item.name), }, yAxis: {}, series: [ { type: 'bar', data: this.chartData.map(item => item.value), }, ], }; myChart.setOption(option); }, }, }; </script> ``` 在这个例子中,`chartData` 是一个数组,包含了每个系列名称和对应的值。`renderChart` 方法在组件挂载后初始化表并设置选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

礼貌而已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值