【Echarts】实现长方体形状的立体柱状图

先上效果图

话不多说上参数

import * as echarts from 'echarts';

//长方体左侧面
const leftRect = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0,
      width: 19, //柱状图宽
      zWidth: 8, //阴影折角宽
      zHeight: 4 //阴影折角高
    },
    buildPath: function (ctx, shape) {
      const api = shape.api;
      const xAxisPoint = api.coord([shape.xValue, 0]);
      const p0 = [shape.x - shape.width / 2, shape.y - shape.zHeight];
      const p1 = [shape.x - shape.width / 2, shape.y - shape.zHeight];
      const p2 = [xAxisPoint[0] - shape.width / 2, xAxisPoint[1]];
      const p3 = [xAxisPoint[0] + shape.width / 2, xAxisPoint[1]];
      const p4 = [shape.x + shape.width / 2, shape.y];
      ctx.moveTo(p0[0], p0[1]);
      ctx.lineTo(p1[0], p1[1]);
      ctx.lineTo(p2[0], p2[1]);
      ctx.lineTo(p3[0], p3[1]);
      ctx.lineTo(p4[0], p4[1]);
      ctx.lineTo(p0[0], p0[1]);
      ctx.closePath();
    }
  });

//长方体右侧面以及顶部图形
const rightRect = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0,
      width: 18,
      zWidth: 15,
      zHeight: 8
    },
    buildPath: function (ctx, shape) {
      const api = shape.api;
      const xAxisPoint = api.coord([shape.xValue, 0]);
      const p1 = [shape.x - shape.width / 2, shape.y - shape.zHeight / 2];
      const p3 = [xAxisPoint[0] + shape.width / 2, xAxisPoint[1]];
      const p4 = [shape.x + shape.width / 2, shape.y];
      const p5 = [xAxisPoint[0] + shape.width / 2 + shape.zWidth, xAxisPoint[1]];
      const p6 = [
        shape.x + shape.width / 2 + shape.zWidth,
        shape.y - shape.zHeight / 2
      ];
      const p7 = [
        shape.x - shape.width / 2 + shape.zWidth,
        shape.y - shape.zHeight
      ];
      ctx.moveTo(p4[0], p4[1]);
      ctx.lineTo(p3[0], p3[1]);
      ctx.lineTo(p5[0], p5[1]);
      ctx.lineTo(p6[0], p6[1]);
      ctx.lineTo(p4[0], p4[1]);
      ctx.moveTo(p4[0], p4[1]);
      ctx.lineTo(p6[0], p6[1]);
      ctx.lineTo(p7[0], p7[1]);
      ctx.lineTo(p1[0], p1[1]);
      ctx.lineTo(p4[0], p4[1]);
      ctx.closePath();
    }
  });


 // 注册图形元素
  echarts.graphic.registerShape("leftRect", leftRect);
  echarts.graphic.registerShape("rightRect", rightRect);


//最后定义options
const options = {
    xAxis: {
      type: "category",
      data: ['111', '222', '333'],
      axisLabel: {
        textStyle: {
          color: "#AEC3DC",
          fontSize: "14px",
        },
      },
    },
    yAxis: {
      type: "value",
      axisLabel: {
        textStyle: {
          color: "#8FABBF",
          fontSize: "14px",
        },
      },
      splitLine: {
        lineStyle: {
          type: "solid",
          color: ["rgba(255,255,255,0.1)"],
        },
        show: true,
      },
    },
    grid: {
      left: "40px", // 左侧内边距
      right: 0, // 右侧内边距
      bottom: "50px", // 底部内边距
      top: "30px",
    },
    series: [
      {
        type: "custom",
        renderItem: function (params, api) {
          const index = 0;
          let location = api.coord([api.value(0) + index, api.value(1)]);
          return {
            type: "group",
            children: [
              {
                type: "leftRect",
                shape: {
                  api,
                  xValue: api.value(0) + index,
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: "#2DAEFF",
                    },
                    {
                      offset: 1,
                      color: "rgba(1,219,243,0)",
                    },
                  ]),
                },
              },
              {
                type: "rightRect",
                shape: {
                  api,
                  xValue: api.value(0) + index,
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: "#1C4667",
                    },
                    {
                      offset: 1,
                      color: "rgba(1,219,243,0)",
                    },
                  ]),
                },
              },
            ],
          };
        },
        data: [1,2,3],
      },
    ],
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Echarts实现立体柱状图,可以使用Echarts的3D效果。以下是使用Vue框架和Echarts实现立体柱状图的步骤: 1. 安装Echarts库:在Vue项目中使用npm或yarn安装Echarts库。 ``` npm install echarts --save ``` 2. 在Vue组件中引入Echarts库: ``` import echarts from 'echarts' ``` 3. 创建一个Echarts图表容器,例如: ``` <div ref="chart" style="width:600px;height:400px;"></div> ``` 4. 在Vue组件中使用Echarts库的API创建一个立体柱状图: ``` mounted() { // 基于准备好的dom,初始化echarts实例 let myChart = echarts.init(this.$refs.chart); // 指定图表的配置项和数据 let option = { tooltip: {}, visualMap: { max: 20, inRange: { color: ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026'] } }, xAxis3D: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, yAxis3D: { type: 'category', data: ['2012', '2013', '2014', '2015', '2016'] }, zAxis3D: { type: 'value' }, grid3D: { boxWidth: 200, boxDepth: 80, viewControl: { // projection: 'orthographic' }, light: { main: { intensity: 1.2, shadow: true }, ambient: { intensity: 0.3 } } }, series: [{ type: 'bar3D', data: [ [0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 2], [0, 4, 1], [1, 0, 1], [1, 1, 5], [1, 2, 3], [1, 3, 0], [1, 4, 2], [2, 0, 0], [2, 1, 3], [2, 2, 5], [2, 3, 1], [2, 4, 1], [3, 0, 2], [3, 1, 0], [3, 2, 3], [3, 3, 5], [3, 4, 2], [4, 0, 1], [4, 1, 2], [4, 2, 1], [4, 3, 2], [4, 4, 5] ], shading: 'lambert', label: { textStyle: { fontSize: 16, borderWidth: 1 } }, emphasis: { label: { textStyle: { fontSize: 20, color: '#900' } }, itemStyle: { color: '#900' } } }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } ``` 这是一个简单的立体柱状图示例,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值