echarts 实现 3d 柱状图

这篇具有很好参考价值的文章主要介绍了echarts 实现 3d 柱状图。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

echarts 实现 3d 柱状图

柱状图

实现要求

  1. 能够调整大小
  2. 实现3d效果,可以改变颜色

前置环境

vue: ^3.1.2
echarts: ^5.3.3
lodash: ^4.17.21

效果

echarts 实现 3d 柱状图
调整大小和颜色

echarts 实现 3d 柱状图

代码

baseCharts.vue

<template>
  <div
    id="baseBarCharts"
    ref="echartsRef"
  ></div>
</template>
<script>
import { onMounted, ref } from "vue"
import * as echarts from 'echarts'
import _ from 'lodash'
export default {
  name: "barCharts",
  setup () {
    let baseEcharts = null
    let echartsRef = ref(null)
    let linearArr = [
      "#0079e4 ",
      "#000af5 ",
      "#36c9ff ",
      "#003efa",
      "#0099e4",
      "#4a9df7",
      "rgba(0, 31, 117, 0.3)",
      "rgba(0, 153, 228, 0.3)",
    ]
    function threeDimensionalLine (offsetX = 20, sliderWidth = 8, offsetTick = 14) {
      // 绘制左侧面
      const CubeLeft = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          // 会canvas的应该都能看得懂,shape是从custom传入的
          const xAxisPoint = shape.xAxisPoint
          const c0 = [shape.x - offsetTick, shape.y]
          const c1 = [shape.x - offsetTick + offsetX, shape.y]
          const c2 = [xAxisPoint[0] - offsetTick + offsetX, xAxisPoint[1]]
          const c3 = [xAxisPoint[0] - offsetTick, 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 - offsetTick + offsetX, shape.y]
          const c2 = [shape.x - offsetTick + offsetX + sliderWidth, shape.y - sliderWidth]
          const c3 = [
            xAxisPoint[0] - offsetTick + offsetX + sliderWidth,
            xAxisPoint[1] - sliderWidth + 4,
          ]
          const c4 = [shape.x - offsetTick + offsetX, xAxisPoint[1]]
          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 - offsetTick, shape.y]
          const c2 = [shape.x - offsetTick + offsetX, shape.y] // 右点
          const c3 = [shape.x - offsetTick + offsetX + sliderWidth, shape.y - sliderWidth]
          const c4 = [shape.x - offsetTick + sliderWidth, shape.y - sliderWidth]
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .lineTo(c1[0], c1[1])
            .closePath()
        },
      })

      const CubeBottom = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint
          const c1 = [xAxisPoint[0] - offsetTick, xAxisPoint[1]]
          const c2 = [xAxisPoint[0] - offsetTick, xAxisPoint[1] + 6] // 右点
          const c3 = [xAxisPoint[0] - offsetTick + offsetX + sliderWidth, xAxisPoint[1] + 6]
          const c4 = [xAxisPoint[0] - offsetTick + offsetX + sliderWidth, xAxisPoint[1] - 6]
          const c5 = [xAxisPoint[0] - offsetTick + offsetX, xAxisPoint[1]]
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .lineTo(c5[0], c5[1])
            .lineTo(c1[0], c1[1])
            .closePath()
        },
      })
      // 注册三个面图形
      echarts.graphic.registerShape("CubeLeft", CubeLeft)
      echarts.graphic.registerShape("CubeRight", CubeRight)
      echarts.graphic.registerShape("CubeTop", CubeTop)
      echarts.graphic.registerShape("CubeBottom", CubeBottom)
    }
    let defalutOptions = ref({
      textStyle: {
        fontFamily: "PingFang",
      },
      grid: {
        top: "12%",
        left: "1%",
        right: "2%",
        bottom: "10%",
        containLabel: true,
      },
      xAxis: {
        type: "category",
        data: [],
        axisLine: {
          lineStyle: {
            color: "#fff",
          },
        },
        axisTick: {
          alignWithLabel: true,
        },
      },
      yAxis: {
        type: "value",
        axisLine: {
          show: true,
          lineStyle: {
            color: "#fff",
          },
        },
        splitLine: {
          lineStyle: {
            color: "#3a79c2",
          },
        },
      },
      tooltip: {
        show: true,
        position: "bottom",
        backgroundColor: "#fff",
        textStyle: {
          color: "#000",
          //   fontStyle: "PingFang",
          fontSize: 12,
          //   fontWeight: "normal",
          lineHeight: 24,
        },
      },
      series: [
        {
          type: "custom",
          renderItem: (params, api) => {
            const location = api.coord([api.value(0), api.value(1)])
            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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: linearArr[0],
                      },
                      {
                        offset: 1,
                        color: linearArr[1],
                      },
                    ]),
                  },
                },
                {
                  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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: linearArr[2],
                      },
                      {
                        offset: 1,
                        color: linearArr[3],
                      },
                    ]),
                  },
                },
                {
                  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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: linearArr[4],
                      },
                      {
                        offset: 1,
                        color: linearArr[5],
                      },
                    ]),
                  },
                },
                {
                  type: "CubeBottom",
                  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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: linearArr[6],
                      },
                      {
                        offset: 1,
                        color: linearArr[7],
                      },
                    ]),
                  },
                },
              ],
            }
          },
          data: [],
        },

        {
          type: "bar",
          label: {
            normal: {
              show: true,
              position: "top",
              fontSize: 12,
              color: "#fff",
            },
          },
          itemStyle: {
            color: "transparent",
          },
          data: [],
        },
      ],
    })
    onMounted(() => {
      baseEcharts = echarts.init(echartsRef.value)
      window.addEventListener(
        "resize",
        _.debounce(() => {
          baseEcharts.resize()
        }, 150)
      )
    })
    function setOptions (options, linerArrOptions, barWidth = {}) {
      linearArr = linerArrOptions
      if (barWidth.offsetX && barWidth.sliderWidth) {
        threeDimensionalLine(
          barWidth.offsetX,
          barWidth.sliderWidth,
          (barWidth.offsetX + barWidth.sliderWidth) / 2
        )
      } else {
        threeDimensionalLine()
      }
      baseEcharts.setOption(options, true)
    }
    return {
      echartsRef,
      setOptions,
      defalutOptions,
    }
  },
}
</script>
<style lang="scss" scoped>
#baseBarCharts {
  width: 100%;
  height: 100%;
}
</style>

使用

<template>
  <div style="width:600px;height:500px;background-color: #000;">
    <base-charts ref="baseChartsRef"></base-charts>
  </div>
  <br />
  <div style="width:600px;height:500px;background-color: #000;">
    <base-charts ref="baseChartsRefPlus"></base-charts>
  </div>
</template>

<script>
import baseCharts from "@/components/baseCharts.vue";
import { onMounted, ref } from 'vue';
export default {
  name: "basecharts",
  components: {
    baseCharts,
  },
  setup () {
    const baseChartsRef = ref(null)
    const baseChartsRefPlus = ref(null)
    onMounted(() => {
      let { defalutOptions, setOptions } = baseChartsRef.value
      defalutOptions.xAxis.data = ['深圳', '广州', '汕头']
      defalutOptions.series[0].data = [100, 200, 300]
      defalutOptions.series[1].data = [100, 200, 300]
      setOptions(defalutOptions, [
        "#4582ff", // 正面颜色渐变
        "#582ee8", // 正面颜色渐变
        "#638afa", // 侧面颜色渐变
        "#6757f1", // 侧面颜色渐变
        "#4c70fc", // 顶部颜色渐变
        "#4582ff", // 顶部颜色渐变
        "rgba(90,75,246,0.3)", // 底部颜色渐变
        "rgba(0,31,117,0.3)" // 底部颜色渐变
      ]);
      // 大小可调整
      baseChartsRefPlus.value.defalutOptions.xAxis.data = ['深圳', '广州', '汕头']
      baseChartsRefPlus.value.defalutOptions.series[0].data = [100, 200, 300]
      baseChartsRefPlus.value.defalutOptions.series[0].data = [100, 200, 300]
      baseChartsRefPlus.value.setOptions(baseChartsRefPlus.value.defalutOptions, [
        "#0079e4", // 正面颜色渐变
        "#000af5", // 正面颜色渐变
        "#36c9ff", // 侧面颜色渐变
        "#003efa", // 侧面颜色渐变
        "#0099e4", // 顶部颜色渐变
        "#4a9df7", // 顶部颜色渐变
        "rgba(0,31,117,0.3)", // 底部颜色渐变
        "rgba(0,153,228,0.3)" // 底部颜色渐变
      ], { offsetX: 36, sliderWidth: 16 }) // 正面宽度 , 侧面宽度

    })
    return {
      baseChartsRef,
      baseChartsRefPlus
    };
  },
};
</script>

<style scoped>
.schart-box {
  display: inline-block;
  margin: 20px;
}
.schart {
  width: 600px;
  height: 400px;
}
.content-title {
  clear: both;
  font-weight: 400;
  line-height: 50px;
  margin: 10px 0;
  font-size: 22px;
  color: #1f2f3d;
}
</style>

10.12 更新

横行柱状图

效果

echarts 实现 3d 柱状图

代码

<template>
  <div id="charts" ref="echartsRef"></div>
</template>
<script>
import { onMounted, ref } from "vue"

export default {
  name: "baseLine",
  props: {
    isneedPercent: {
      type: Boolean,
      default: false,
    },
  },
  setup(props) {
    let baseEcharts = null
    let echartsRef = ref(null)
    let linearArr = [
      "#f7763f",
      "#fa2a31",
      "#ff9d61",
      "#fa2a31",
      "#ff9d61",
      "rgba(247,105,109,0.9)",
      "#fa2a31",
      "#ff9d61",
    ]
    let defalutOptions = ref({
      textStyle: {
        fontFamily: "PingFang",
        fontSize: 16,
      },
      tooltip: {
        trigger: "item",
        valueFormatter: value => (props.isneedPercent ? value + "%" : value),
      },
      grid: {
        top: "2%",
        left: "20%",
        bottom: "10%",
      },
      xAxis: {
        axisTick: {
          show: false,
        },
        axisLine: {
          show: true,
        },
        splitLine: {
          show: false,
        },
        axisLabel: {
          interval: 0,
          textStyle: {
            color: "#fff",
            fontSize: 14,
          },
          formatter: props.isneedPercent ? "{value}%" : "{value}",
        },
      },
      yAxis: {
        data: [],
        splitLine: {
          show: false,
        },
        axisTick: {
          show: true,
        },
        axisLine: {
          show: true,
        },
        axisLabel: {
          textStyle: {
            color: "#fff",
            fontSize: 12,
            padding: [0, 5, 0, 0],
          },
        },
      },
      series: [
        // 最左边顶部
        {
          name: "",
          type: "pictorialBar",
          symbolSize: [5, 20],
          symbolOffset: [-2, 0],
          z: 12,
          itemStyle: {
            opacity: 1,
            color: linearArr[0],
          },
          data: [1, 1, 1, 1, 1],
          tooltip: {
            show: false,
          },
        },
        {
          name: "2020",
          type: "bar",
          barWidth: 20,
          barGap: "-100%",
          itemStyle: {
            //lenged文本
            opacity: 1,
            color: new echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1,
              [
                {
                  offset: 0,
                  color: linearArr[1], // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: linearArr[2], // 100% 处的颜色
                },
              ],
              false
            ),
          },
          label: {
            formatter: props.isneedPercent ? "{c} %" : "{c}",
          },
          data: [],
        },
        {
          type: "bar",
          barWidth: 20,
          barGap: "-100%",
          itemStyle: {
            color: "transparent",
          },
          label: {
            formatter: props.isneedPercent ? "{c} %" : "{c}",
          },
          data: [],
        },
        {
          name: "", //头部
          type: "pictorialBar",
          symbolSize: [5, 20],
          symbolOffset: [2, 0],
          z: 12,
          symbolPosition: "end",
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1,
              [
                {
                  offset: 0,
                  color: linearArr[3], // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: linearArr[4], // 100% 处的颜色
                },
              ],
              false
            ),
            opacity: 0.2,
          },
          label: {
            formatter: props.isneedPercent ? "{c} %" : "{c}",
          },
          data: [100, 100, 100, 100, 100, 100],
          tooltip: {
            show: false,
          },
        },
        {
          name: "",
          type: "pictorialBar",
          symbolSize: [5, 20],
          symbolOffset: [2, 0],
          z: 12,
          itemStyle: {
            opacity: 1,
            color: linearArr[5],
          },
          symbolPosition: "end",
          data: [],
          tooltip: {
            show: false,
          },
          label: {
            show: true,
            position: "right",
            fontSize: 12,
            color: "#fff",
            formatter: props.isneedPercent ? "{c} %" : "{c}",
          },
        },
        {
          name: "all",
          type: "bar",
          barWidth: 20,
          barGap: "-100%",
          z: 0,
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1,
              [
                {
                  offset: 0,
                  color: linearArr[6], // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: linearArr[7], // 100% 处的颜色
                },
              ],
              false
            ),
            opacity: 0.2,
          },
          label: {
            formatter: props.isneedPercent ? "{c} %" : "{c}",
          },
          data: [100, 100, 100, 100, 100, 100],
          tooltip: {
            show: false,
          },
        },
      ],
    })

    onMounted(() => {
      baseEcharts = echarts.init(echartsRef.value)
      window.addEventListener(
        "resize",
        _.debounce(() => {
          baseEcharts.resize()
        }, 150)
      )
      // baseEcharts.setOption(defalutOptions.value)
    })
    function setOptions(options, colors) {
      options.series[0].itemStyle.color = colors[0]
      options.series[1].itemStyle.color.colorStops = [
        { offset: 0, color: colors[1] },
        { offset: 1, color: colors[2] },
      ]
      options.series[3].itemStyle.color.colorStops = [
        { offset: 0, color: colors[3] },
        { offset: 1, color: colors[4] },
      ]
      options.series[4].itemStyle.color = colors[5]
      options.series[5].itemStyle.color.colorStops = [
        { offset: 0, color: colors[6] },
        { offset: 1, color: colors[7] },
      ]
      baseEcharts.setOption(options, true)
    }
    return {
      echartsRef,
      setOptions,
      defalutOptions,
    }
  },
}
</script>
<style lang="scss" scoped>
#charts {
  width: 100%;
  height: 100%;
}
</style>

使用文章来源地址https://www.toymoban.com/news/detail-511356.html

<template>
  <div>
    <pipeBar ref="pipeBarRef" style="width: 500px; height: 400px"></pipeBar>
  </div>
</template>
<script>
import { onMounted, ref } from "vue"
import pipeBar from "@/components/echarts/pipeBar"

export default {
  setup() {
    let pipeBarRef = ref(null)
    onMounted(() => {
      let { setOptions, defalutOptions } = pipeBarRef.value
      defalutOptions.series[1].data =
        defalutOptions.series[2].data =
        defalutOptions.series[4].data =
          [20, 60, 10]
      defalutOptions.series[3].data = defalutOptions.series[5].data = [100, 100, 100]
      defalutOptions.yAxis.data = ["本科及以上", "大专", "大专及一下"]
      setOptions(defalutOptions, [
        "#f7763f", // 颜色值
        "#fa2a31",// 颜色值
        "#ff9d61",// 颜色值
        "#fa2a31",// 颜色值
        "#ff9d61",// 颜色值
        "rgba(247,105,109,0.9)",// 颜色值
        "#fa2a31",// 颜色值
        "#ff9d61",// 颜色值
      ])
    })
    return {
      pipeBarRef,
    }
  },
  components: {
    pipeBar,
  },
}
</script>
<style lang="scss"></style>

到了这里,关于echarts 实现 3d 柱状图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • echarts实现3d柱状图的两种方式

    看了不少关于3d柱状图的案例,发现做3d柱状图 常用的两种方式就是 自定义图形和象型柱图, 两种实现方式效果如下: 方法1: echarts.graphic.extendShape 自定义图形 echarts自定义图形的详细用法点这里, 官网点这里, 图中第一个3d柱状图我参考的案例在这里, 看了很多 echarts这种3d案例,

    2024年02月01日
    浏览(43)
  • vue+echarts——实现3D地图+3D柱状图 效果——粗糙代码记录——技能提升

    最近看到同事在弄下面的这个图,这个图是从网上看到的,是某个网站的收费项目: 所以,最后的决定是通过 echarts 中的 3D地图 来写。但是写出来的效果不慎好看。功能是可以实现的。 初版效果图如下: 直接上代码: 我这边是存储到当前文件夹中了。。。 背景颜色是 ec

    2024年02月09日
    浏览(36)
  • echarts 3D 柱状图

    注意: 这里主要就是基于各类图表,更多的使用 Echarts 的各类配置项; 以下代码都可以复制到 Echarts 官网,直接预览; 注意: 以下背景图来源于网络,如果失效请自行替换; echarts 环形图:多层嵌套,自定义 legend 位置、颜色,中间插入数据及文字,颜色渐变; 文字链接:

    2024年02月11日
    浏览(29)
  • Echarts-3D柱状图

    通过Echarts的echarts.graphic.extendShape实现真正的3D柱状图 思路就是通过调整顶部面(CubeTop)、左侧面(CubeLeft)、右侧面(CubeRight)来决定柱状图的宽窄 建议优先调整顶部面,一般c1不需要动 然后在build-bar-option中引用即可 这里主要就是把series中的内容复制过来直接用就行了

    2024年02月08日
    浏览(29)
  • echarts5 3D柱状图

    div ref=\\\"echartsQinggan\\\" style=\\\"width: 100%;height: 100%;\\\"/div     async getData() { ///调用接口获取数据    this.Count1=[1,2,3]  this.Count2=[4,5,6]  this.Count3=[7,null,8] this.$nextTick(() = {         this.qinganFun()       })     },     qinganFun() {       // 获取DOM节点并初始化       const hotTheme = this.$echarts.i

    2024年03月18日
    浏览(40)
  • echart 3d立体颜色渐变柱状图

    如果可以实现记得点赞分享,谢谢老铁~ 1.需求描述 根据业务需求将不同的法律法规,展示不同的3d立体渐变柱状图。 2.先看下效果图 3. 确定三面的颜色,这里我是自定义的颜色 4.然后绘画三个面对应的函数,且注册 5.重点在renderItem 自定义渲染函数上 5.最后看全文吧,这个

    2024年02月12日
    浏览(29)
  • Echarts 3D立体柱状图(源码+例图)

    Echarts 3D立体柱状图,3D长方体柱状图,直接cope源码可用(源码+例图) 废话不多说,直接上代码!!! 

    2024年02月15日
    浏览(35)
  • echarts3d柱状图

     

    2024年02月21日
    浏览(30)
  • echarts3D地图+3D柱状图+3D飞线图

    echarts版本:5.4.0 echarts-gl版本:2.0.8 DataV.GeoAtlas地理小工具系列

    2024年02月07日
    浏览(28)
  • echarts看板效果图:流光折线图、3d柱状图、3d饼图、3d地图

    现在展厅的大看板是越花里胡哨越好,不过真的挺难做的。好在可以百度找到一些大神的作品进行参考。 下面的内容都是基于 echarts 5.3.3 和 vue3 。另外demo都是参考别人的案例。 效果图 代码 本质上是两条线组合在一起的,一条是静态的线条,一条是动态的线条。相关属性都

    2024年02月06日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包