echarts 3D立体柱状图加折线

本文详细展示了如何在ECharts库中创建一个带有3D效果的立体柱状图和折线图,包括定制图形形状、颜色渐变和数据绑定等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:

3D立体柱状图加折线
在这里插入图片描述

代码:

let config = {
  maxList: [15, 17, 16, 17, 15, 14, 14],
  valueList: [
    11, 10, 10, 12, 9, 7, 7
  ]
};
// 绘制左侧面
const CubeLeft = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath: function (ctx, shape) {
    const xAxisPoint = shape.xAxisPoint;
    const c0 = [shape.x, shape.y];
    const c1 = [shape.x - 10, shape.y - 8];
    const c2 = [xAxisPoint[0] - 10, xAxisPoint[1] - 6];
    const c3 = [xAxisPoint[0], xAxisPoint[1]];
    ctx
      .moveTo(c0[0], c0[1])
      .lineTo(c1[0], c1[1])
      .lineTo(c2[0], c2[1])
      .lineTo(c3[0], c3[1])
      .closePath();
  }
});
// 绘制右侧面
const CubeRight = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath: function (ctx, shape) {
    const xAxisPoint = shape.xAxisPoint;
    const c1 = [shape.x, shape.y];
    const c2 = [xAxisPoint[0], xAxisPoint[1]];
    const c3 = [xAxisPoint[0] + 12, xAxisPoint[1] - 6]; 
    const c4 = [shape.x + 12, shape.y - 6]; 
    ctx
      .moveTo(c1[0], c1[1])
      .lineTo(c2[0], c2[1])
      .lineTo(c3[0], c3[1])
      .lineTo(c4[0], c4[1])
      .closePath();
  }
});
// 绘制顶面
const CubeTop = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath: function (ctx, shape) {
    const c1 = [shape.x, shape.y];
    const c2 = [shape.x + 12, shape.y - 6];
    const c3 = [shape.x + 2, shape.y - 14];
    const c4 = [shape.x - 10, shape.y - 8];
    ctx
      .moveTo(c1[0], c1[1])
      .lineTo(c2[0], c2[1])
      .lineTo(c3[0], c3[1])
      .lineTo(c4[0], c4[1])
      .closePath();
  }
});
// 注册
echarts.graphic.registerShape('CubeLeft', CubeLeft);
echarts.graphic.registerShape('CubeRight', CubeRight);
echarts.graphic.registerShape('CubeTop', CubeTop);
const { bgColors, colors, maxList, valueList } = config || {};
const MAX = maxList || [];
const VALUE = valueList || [];
option = {
  backgroundColor: '#140a69',
  color: ['#ED4848'],
  tooltip: {
    trigger: 'axis',
    backgroundColor: 'rgba(69, 14, 255,0.5)',
    borderWidth: '0',
    confine: true,
    textStyle: {
      fontSize: 12,
      color: '#6DFFFF'
    },
    axisPointer: {
      type: 'shadow'
    },
    formatter: function (params, ticket, callback) {
      const item = params[1];
      return (
        item.name +
        '<br />' +
        `当前值:${item.value}` +
        '<br />' +
        `阀值:${params[0].value}`
      );
    }
  },
  grid: {
    left: 25,
    right: 25,
    bottom: 40,
    top: 50,
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: [
      '周1',
      '周2',
      '周3',
      '周4',
      '周5',
      '周6',
      '周7',
    ],
    axisLine: {
      show: false
    },
    nameTextStyle: {
      color: '#9899A5'
    },
    axisTick: {
      show: false
    }
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      formatter: '{value}个'
    },
    splitLine: {
      show: true, // 显示分隔线
      lineStyle: {
        type: 'dashed',
        color: 'rgb(109 255 249 / 20%)'
      }
    }
  },
  series: [
    {
      type: 'custom',
      renderItem: function (params, api) {
        const location = api.coord([api.value(0), api.value(1)]);
        var color = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: (bgColors && bgColors[0]) || 'rgba(9, 51, 209, 0.3)'
          },
          {
            offset: 1,
            color: (bgColors && bgColors[1]) || 'rgba(0, 208, 242, 0.3)'
          }
        ]);
        return {
          type: 'group',
          children: [
            {
              type: 'CubeLeft',
              shape: {
                api,
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            },
            {
              type: 'CubeRight',
              shape: {
                api,
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            },
            {
              type: 'CubeTop',
              shape: {
                api,
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            }
          ]
        };
      },
      data: MAX
    },
    {
      type: 'custom',
      renderItem: (params, api) => {
        const location = api.coord([api.value(0), api.value(1)]);
        var color = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: (colors && colors[0]) || '#0933D1'
          },
          {
            offset: 1,
            color: (colors && colors[1]) || '#00D0F2'
          }
        ]);
        return {
          type: 'group',
          children: [
            {
              type: 'CubeLeft',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            },
            {
              type: 'CubeRight',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            },
            {
              type: 'CubeTop',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: color
              }
            }
          ]
        };
      },
      data: VALUE
    },
    {
      name: '阀值',
      data: MAX,
      type: 'line'
    }
  ]
};

您好!关于echarts立体柱状堆叠图和折线图的使用,您可以按照以下步骤进行操作: 1. 首先,确保您已经引入了echarts库。可以通过在HTML文件中添如下代码来引入: ``` <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.0/dist/echarts.min.js"></script> ``` 2. 创建一个具有一定宽度和高度的DOM容器,用于展示图表。例如: ``` <div id="chart" style="width: 600px; height: 400px;"></div> ``` 3. 使用JavaScript代码初始化图表,并配置数据和图表样式。以下是一个简单的示例: ```javascript // 初始化图表 var chart = echarts.init(document.getElementById('chart')); // 配置数据 var data = { categories: ['类别1', '类别2', '类别3', '类别4'], seriesData1: [120, 200, 150, 80], seriesData2: [80, 120, 100, 60], seriesData3: [60, 80, 70, 50], lineData: [50, 60, 80, 70] }; // 配置图表样式 var option = { tooltip: {}, legend: { data: ['系列1', '系列2', '系列3', '折线'] }, xAxis: { data: data.categories }, yAxis: {}, series: [ { name: '系列1', type: 'bar', stack: '堆叠', data: data.seriesData1 }, { name: '系列2', type: 'bar', stack: '堆叠', data: data.seriesData2 }, { name: '系列3', type: 'bar', stack: '堆叠', data: data.seriesData3 }, { name: '折线', type: 'line', data: data.lineData } ] }; // 渲染图表 chart.setOption(option); ``` 以上代码示例创建了一个包含立体柱状堆叠图和折线图echarts图表。您可以根据自己的数据和需求进行配置和调整。 希望能对您有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南桥几经秋_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值