位置: IT常识 - 正文

通过Echarts怎样实现立体柱状图(echarts-gl)

编辑:rootadmin
通过Echarts怎样实现立体柱状图 前言

推荐整理分享通过Echarts怎样实现立体柱状图(echarts-gl),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:echarts方法,echarts bi,echarts快速上手,echarts.,echarts怎么用,echarts怎么样,echart怎么用,echarts怎么用,内容如对您有帮助,希望把文章链接给更多的朋友!

大家好,我是梁木由。之前在做大屏可视化项目时,UI设计了一个立体形状的柱状图,根据之前做的一些图表的项目没有能复用的,没有做过这种立体形状的图表,打开echarts也没看到有相关的demo,看下如何实现

图表样例

来看下UI设计师给到的设计图

上述设计图种柱状图都是立体的样式,那我们来看下如何实现

实现方法先写一个常规的柱状图

在这个基础上进行改进

<div id="main"></div>​#main{  width: 500px;  height: 350px;}​var chartDom = document.getElementById('main');var myChart = echarts.init(chartDom);var option;​option = {  xAxis: {    axisTick: {      show: false   },    nameTextStyle: {      color: '#fff'   },    data: ['春节', '元宵节', '端午节', '中秋节'] },  legend: {    data: ['春节', '元宵节', '端午节', '中秋节'],    right: '25',    top: '18',    icon: 'rect',    itemHeight: 10,    itemWidth: 10,    textStyle: {      color: '#000'   } },  yAxis: {    type: 'value',    axisLabel: {      color: '#000'   },    splitLine: {      show: true,      lineStyle: {        type: 'dashed',        color: ['#ccc']     }   } },  series: [   {      data: [       { name: '春节', value: 24 },       { name: '端午节', value: 44 },       { name: '中秋节', value: 32 },       { name: '春节', value: 50 }     ],      barWidth: 30,      type: 'bar'   } ]};​​option && myChart.setOption(option);echarts的配置选项

首先呢我们看下echarts的配置选项

那我们看所有的type 没有立体柱状图的类型,但是呢我们看最后一项type: custom,什么意思呢,自定义系列,那就是说我们可以选择custom 类型来实现立体柱状图

renderItem

type为custom可以自定义系列图形元素渲染。

根据查看配置项,发现有一个renderItem函数,custom 系列需要开发者自己提供图形渲染的逻辑。这个渲染逻辑一般命名为 renderItem

看下renderItem函数的介绍

renderItem 函数提供了两个参数:

params:包含了当前数据信息和坐标系的信息。

{    context: // {Object} 一个可供开发者暂存东西的对象。生命周期只为:当前次的渲染。    seriesId: // {string} 本系列 id。    seriesName: // {string} 本系列 name。    seriesIndex: // {number} 本系列 index。    dataIndex: // {number} 数据项的 index。    dataIndexInside: // {number} 数据项在当前坐标系中可见的数据的 index(即 dataZoom 当前窗口中的数据的 index)。    dataInsideLength: // {number} 当前坐标系中可见的数据长度(即 dataZoom 当前窗口中的数据数量)。    actionType: // {string} 触发此次重绘的 action 的 type。    coordSys: // 不同的坐标系中,coordSys 里的信息不一样,含有如下这些可能:    coordSys: {        type: 'cartesian2d',        x: // {number} grid rect 的 x        y: // {number} grid rect 的 y        width: // {number} grid rect 的 width        height: // {number} grid rect 的 height   },    coordSys: {        type: 'calendar',        x: // {number} calendar rect 的 x        y: // {number} calendar rect 的 y        width: // {number} calendar rect 的 width        height: // {number} calendar rect 的 height        cellWidth: // {number} calendar cellWidth        cellHeight: // {number} calendar cellHeight        rangeInfo: {            start: // calendar 日期开端            end: // calendar 日期结尾            weeks: // calendar 周数            dayCount: // calendar 日数       }   },    coordSys: {        type: 'geo',        x: // {number} geo rect 的 x        y: // {number} geo rect 的 y        width: // {number} geo rect 的 width        height: // {number} geo rect 的 height        zoom: // {number} 缩放的比率。如果没有缩放,则值为 1。例如 0.5 表示缩小了一半。   },    coordSys: {        type: 'polar',        cx: // {number} polar 的中心坐标        cy: // {number} polar 的中心坐标        r: // {number} polar 的外半径        r0: // {number} polar 的内半径   },    coordSys: {        type: 'singleAxis',        x: // {number} singleAxis rect 的 x        y: // {number} singleAxis rect 的 y        width: // {number} singleAxis rect 的 width        height: // {number} singleAxis rect 的 height   }}

其中,关于 dataIndex 和 dataIndexInside 的区别:

dataIndex 指的 dataItem 在原始数据中的 index。dataIndexInside 指的是 dataItem 在当前数据窗口中的 index。

[renderItem.arguments.api] 中使用的参数都是 dataIndexInside 而非 dataIndex,因为从 dataIndex 转换成 dataIndexInside 需要时间开销。

api:是一些开发者可调用的方法集合。

所有属性通过Echarts怎样实现立体柱状图(echarts-gl)

{[value], [coord] , [size] , [style] , [styleEmphasis] , [visual] , [barLayout] , [currentSeriesIndices] , [font], [getWidth] , [getHeight], [getZr], [getDevicePixelRatio]}

我们使用renderItem来自定义元素会使用到renderItem.api的三个方法,先来介绍下这三个方法

[api.value(…)],意思是取出 dataItem 中的数值。例如 api.value(0) 表示取出当前 dataItem 中第一个维度的数值。[api.coord(…)],意思是进行坐标转换计算。例如 var point = api.coord([api.value(0), api.value(1)]) 表示 dataItem 中的数值转换成坐标系上的点。[api.size(…)] ,表示得到坐标系上一段数值范围对应的长度。

看下代码实现

series:  getSeriesData()​function getSeriesData() {  const data = [];  seriesData.forEach((item, index) => {    data.push(     {        type: 'custom',        name: item.label,        renderItem: function (params, api) {          return getRenderItem(params, api);       },        data: item.value,     }   ) })  return data}​function getRenderItem(params, api) {  // params.seriesIndex表示本系列 index  const index = params.seriesIndex;  // api.coord() 坐标转换计算  // api.value() 取出当前项中的数值  let location = api.coord([api.value(0) + index, api.value(1)]);  // api.size() 坐标系数值范围对应的长度  var extent = api.size([0, api.value(1)]);  return {    type: 'rect',    shape: {      x: location[0] - 20 / 2,      y: location[1],      width: 20,      height: extent[1]   },    style: api.style() };}

来看下我们的实现效果

柱状图效果出来了,那来看下怎么将柱状图改为立体图

return_group

我看到renderItem可以返回一个return_group类型,来看看这个类型的介绍

group 是唯一的可以有子节点的容器。group 可以用来整体定位一组图形元素。

那就是说我们可以将设定一组图形元素然后组合到一起形成立体柱状图

那么问题又来了怎么设定一组图形元素呢?

graphic

这个呢是关于图形相关的方法,再来了解两个API

graphic.extendShape

创建一个新的图形元素

graphic.registerShape

注册一个开发者定义的图形元素

创建图形元素

那我们先来创建一个新的图形元素

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);

再来修改一下return_group

function getRenderItem(params, api) {  const index = params.seriesIndex;  let location = api.coord([api.value(0) + index, api.value(1)]);  var extent = api.size([0, 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: api.style()     },     {        type: 'rightRect',        shape: {          api,          xValue: api.value(0) + index,          yValue: api.value(1),          x: location[0],          y: location[1]       },        style: api.style()     }   ] };}

再来看下效果

可以看到立体形状的柱状图已经实现了,那还有个缺点就是颜色需要再按照设计图来改改

const colors = [   [     { offset: 0, color: 'rgba(26, 132, 191, 1)' },     { offset: 1, color: 'rgba(52, 163, 224, 0.08)' },   ],   [     { offset: 0, color: 'rgba(137, 163, 164, 1)' },     { offset: 1, color: 'rgba(137, 163, 164, 0.08)' },   ],   [     { offset: 0, color: 'rgba(44, 166, 166, 1)' },     { offset: 1, color: 'rgba(44, 166, 166, 0.08)' },   ],   [     { offset: 0, color: 'rgba(34, 66, 186, 1)' },     { offset: 1, color: 'rgba(34, 66, 186, 0.08)' },   ], ];​//在getSeriesData添加itemStyleitemStyle: {       color: () => {              return new echarts.graphic.LinearGradient(0, 0, 0, 1, colors[index]);       },},效果图

结尾

Echarts实现立体柱状图——码上掘金

如果文章对你有帮助,期望点一下赞支持一下,🙏🙏🙏

本文链接地址:https://www.jiuchutong.com/zhishi/296171.html 转载请保留说明!

上一篇:初学者安装Sklearn详细步骤(有详细步骤截图,亲测完成)(初学者安装visual studio)

下一篇:vue-devtools的安装与使用(devtools vue)

  • vivox70pro+充电多少瓦(vivox70pro充电多久)

    vivox70pro+充电多少瓦(vivox70pro充电多久)

  • 宽带错误711是什么意思(宽带显示错误711什么意思)

    宽带错误711是什么意思(宽带显示错误711什么意思)

  • 手机知乎为什么不能复制(手机知乎为什么要下载)

    手机知乎为什么不能复制(手机知乎为什么要下载)

  • soul被别人拉黑了 还可以发信息吗(soul被别人拉黑后怎么看对方瞬间)

    soul被别人拉黑了 还可以发信息吗(soul被别人拉黑后怎么看对方瞬间)

  • vivox27熄屏显示怎么设置(vivox27灭屏显示时间)

    vivox27熄屏显示怎么设置(vivox27灭屏显示时间)

  • qq音乐怎么取消dj模式(QQ音乐怎么取消到期续费)

    qq音乐怎么取消dj模式(QQ音乐怎么取消到期续费)

  • 傲腾增强型ssd什么意思(傲腾增强型ssd什么牌子好)

    傲腾增强型ssd什么意思(傲腾增强型ssd什么牌子好)

  • 微信最多加多少人加满(微信最多加多少个人)

    微信最多加多少人加满(微信最多加多少个人)

  • 文件下载打开找不到应用程序(下载的文件点击打开没有反应)

    文件下载打开找不到应用程序(下载的文件点击打开没有反应)

  • 苹果x用12系统还是13系统(苹果x升级苹果12)

    苹果x用12系统还是13系统(苹果x升级苹果12)

  • 3dmax切换视图快捷键(3dmax切换视角)

    3dmax切换视图快捷键(3dmax切换视角)

  • 笔记本的无线网卡在哪个位置(笔记本的无线网卡是什么)

    笔记本的无线网卡在哪个位置(笔记本的无线网卡是什么)

  • 上网方式cmnet是什么意思(cmwap上网是什么意思)

    上网方式cmnet是什么意思(cmwap上网是什么意思)

  • 微信正在使用移动数据(微信正在使用移动网络)

    微信正在使用移动数据(微信正在使用移动网络)

  • 快手作品下载到本地去哪找(快手作品下载到相册没有音乐)

    快手作品下载到本地去哪找(快手作品下载到相册没有音乐)

  • 苹果11待机耗电快是什么原因(苹果11待机耗电多少正常)

    苹果11待机耗电快是什么原因(苹果11待机耗电多少正常)

  • 手机怎么打开wim文件(手机怎么打开wim文件视频)

    手机怎么打开wim文件(手机怎么打开wim文件视频)

  • 快手频道分类怎么弄(快手频道分类怎么删除)

    快手频道分类怎么弄(快手频道分类怎么删除)

  • ps怎么让图片融入背景(ps怎么让图片融入)

    ps怎么让图片融入背景(ps怎么让图片融入)

  • 哔哩哔哩免流量卡注销(哔哩哔哩免流量播放什么意思)

    哔哩哔哩免流量卡注销(哔哩哔哩免流量播放什么意思)

  • ivox27微信怎么设置美颜(手机微信如何设置)

    ivox27微信怎么设置美颜(手机微信如何设置)

  • 主网和配网的区别(什么叫主网什么叫配网)

    主网和配网的区别(什么叫主网什么叫配网)

  • 表格里字太多怎么调整(表格里字太多怎么换行)

    表格里字太多怎么调整(表格里字太多怎么换行)

  • 美团怎么取消自动续费(美团怎么取消自动扣款)

    美团怎么取消自动续费(美团怎么取消自动扣款)

  • 【经验分享】使用了6年的实时操作系统,是时候梳理一下它的知识点了 | 文末赠书4本

    【经验分享】使用了6年的实时操作系统,是时候梳理一下它的知识点了 | 文末赠书4本

  • 帝国cms模板文章列表分页的下划线如何修改(帝国cms模板文件放在哪里)

    帝国cms模板文章列表分页的下划线如何修改(帝国cms模板文件放在哪里)

  • 税控系统怎么登录
  • 受托加工开票如何选名称
  • 个人独资企业需要监事吗
  • 什么公司不可以上市
  • 会计和税法折旧年限不同如何计算终结期现金净流量
  • 牛奶公司饲养奶牛生产牛奶
  • 免征土地增值税
  • 给国外汇款交增值税吗
  • 公司职员聚餐取个名字
  • 2021 上海房产税
  • 职工教育经费进项
  • 临时工工资个税怎么算
  • 客运服务费怎么开票
  • 关于劳务派遣服务外包的案例
  • 发票备注有法律效力吗
  • 研发支出资本化支出在报表哪里
  • 发票密码区出格了怎么调整
  • 研发失败的项目可以加计扣除吗
  • 个体户查账征收没有成本票怎么办
  • 美元兑人民币分时走势图
  • 固定资产改造更新是否需要计提折旧
  • 开启共享文件夹
  • 公交车的乘车凭证能报销吗
  • 微信聊天记录备份和恢复
  • 如果工地老板拖欠工资怎么办
  • php中substr_replace
  • 商场充值卡发票在哪开
  • 拆迁货币什么意思
  • 主营业务收入明细账
  • 房地产企业开发成本结转
  • 公司主营业务有哪些类型
  • 高新技术企业研发费加计扣除政策
  • 发票抵税是怎么申报的
  • imx6ul开源项目
  • numpy的简单例子
  • 股票收益缴纳个人所得税吗
  • 小微企业免征增值税优惠
  • 个人出租房可开发票吗
  • 企业增值税申报流程
  • 自然人专项扣除填报
  • python中字典的键有何要求
  • 编制科目余额表的方法
  • 小规模纳税人工资薪金怎么申报
  • 支出金额是什么
  • 小企业会计准则和一般企业会计准则的区别
  • 小规模当月开普票作废流程
  • 小企业会计准则和企业会计准则的区别
  • 个人独资企业需要缴纳哪些税种
  • 长期待摊费用摊销会计分录
  • 预付账款如何结账
  • 扣除工程款说明
  • 直接减免税款的例子
  • 设置资产处置损益的依据
  • mysql的性能调优
  • mysql5.7安装教程详细
  • macos怎么操作
  • win10显示请勿关闭电脑
  • 安装win7旗舰版用户名和密码是多少
  • windows xp设置屏保密码
  • centos7 cp
  • ubuntu sudo not found
  • linux检测硬盘故障
  • win7系统出现蓝屏
  • 微软首席科学家薪水
  • 安装win8正在安装应用要多久
  • windows7如何取消锁屏密码
  • win10如何使用usb无线网卡
  • linux如何绑定域名
  • easyui给下拉框赋值
  • unity判断点击ui
  • perl pi
  • Node.js中的construct构造函数
  • Node.js中的事件循环是什么样的
  • linux多线程调试手段
  • jq倒计时代码
  • unity3d移动代码
  • jqueryfor
  • 国税系统升级后怎么添加办税人
  • 打单子的打印机能否打a4的纸
  • 深圳城管局 局长
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设