echarts3d柱状图

12,390次阅读
没有评论

共计 4869 个字符,预计需要花费 13 分钟才能阅读完成。

// 画立方体三个面
const CubeLeft = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
    width: 9.5, // 柱状图宽
    zWidth: 4, // 阴影折角宽
    zHeight: 3, // 阴影折角高
  },
  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.closePath();},
});
const CubeRight = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
    width: 9,
    zWidth: 9.5,
    zHeight: 5.5,
  },
  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();},
});
const CubeTop = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
    width: 9,
    zWidth: 9.5,
    zHeight: 6,
  },
  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.closePath();},
});
echarts.graphic.registerShape("CubeLeft", CubeLeft);
echarts.graphic.registerShape("CubeRight", CubeRight);
echarts.graphic.registerShape("CubeTop", CubeTop);
// 定义颜色值
const colors = [
  [{ offset: 0, color: "rgb(254, 223, 34)" },
    {offset: 0.5, color: "rgba(254, 223, 34,0.2)" },
    {offset: 1, color: "rgba(0, 0, 0,0.3)" },
  ],
  [{ offset: 0, color: "rgba(23, 252, 238)" },
    {offset: 0.5, color: "rgba(23, 252, 238,0.2)" },
    {offset: 1, color: "rgba(0, 0, 0,0.3)" },
  ],
];
const leftColors = [
  [{ offset: 0, color: "rgb(254, 223, 34,0.7)" },
    {offset: 0.5, color: "rgba(254, 223, 34,0.9)" },
    {offset: 1, color: "rgba(0, 0, 0,0.3)" },
  ],
  [{ offset: 0, color: "rgba(23, 252, 238,0.7)" },
    {offset: 0.5, color: "rgba(23, 252, 238,0.9)" },
    {offset: 1, color: "rgba(0, 0, 0,0.3)" },
  ],
];
const topColors = [
  [
    {
      offset: 0,
      color: "rgba(204, 186, 49,0.5)",
    },
    {
      offset: 1,
      color: "rgba(204, 186, 49,0.8)",
    },
  ],
  [
    {
      offset: 0,
      color: "rgba(17, 159, 174,0.5)",
    },
    {
      offset: 1,
      color: "rgba(17, 159, 174,0.8)",
    },
  ],
];
function getSeriesData() {const data = [];
  seriesData.forEach((item, index) => {
    data.push({
      type: "custom",
      name: item.label,
      renderItem: function (params, api) {return getRenderItem(params, api);
      },
      // label:{
      //  show:true,
      // },
      data: item.data,
      itemStyle: {color: () => {return new echarts.graphic.LinearGradient(0, 0, 0, 1, colors[index]);
        },
      },
    });
  });
  data.push(
    {
      type: "bar",
      itemStyle: {color: "transparent",},
      label: {
        normal: {
          show: true,
          position: "top",
          fontSize: 12,
          color: "rgba(255, 255, 255, 0.9)",
          offset: [-5, -15],
        },
      },
      data: seriesData[0].data,
    },
    {
      type: "bar",
      itemStyle: {color: "transparent",},
      //  showBackground: true,

      label: {
        normal: {
          show: true,
          position: "top",
          fontSize: 12,
          color: "rgba(255, 255, 255, 0.9)",
          offset: [-7, -15],
        },
      },
      data: seriesData[1].data,
    }
  );
  return data;
}

function getRenderItem(params, api) {

  const index = params.seriesIndex;
  let location = api.coord([api.value(0) + index, api.value(1)]);
  const shape = {
    api,
    xValue: api.value(0) + index,
    yValue: api.value(1),
    x: location[0],
    y: location[1],
    xAxisPoint: api.coord([api.value(0), 0]),
  };
  return {
    type: "group",
    children: [
      {position: seriesData[index].position,
        type: "CubeTop",
        shape: shape,
        style: {
          fill: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            topColors[index]
          ),
        },
      },
      {position: seriesData[index].position,
        type: "CubeLeft",
        shape: shape,
        style: {
          fill: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            leftColors[index]
          ),
        },
      },
      {position: seriesData[index].position,
        type: "CubeRight",
        shape: shape,
        style: api.style(),},
    ],
  };
}


const seriesData = [
  {
    label: "话务",
    position: [-20, 0],
    data: [386, 247, 298, 476],
  },
  {
    label: "工单",
    position: [-60, 0],
    data: [200, 374, 154, 200],
  },
];

let option = reactive({
  tooltip: {show: false,},
  xAxis: {data: ["投诉", "咨询", "建议", "保修"],
    axisTick: {show: false,},
    axisLine: {
      lineStyle: {
        color: "#0b3b67",
        width: 3,
      },
    },
    axisLabel: {
      show: true,
      color: "#fff",
    },
  },

  yAxis: {
    show: true,
    axisLabel: {show: false,},
    splitNumber: 7,
    splitLine: {
      show: true,
      lineStyle: {color: "#0b3b67",},
    },
  },
  series: getSeriesData(),});

echarts3d 柱状图

 

原文地址: echarts3d 柱状图

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-07发表,共计4869字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)