iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >基于Echarts如何实现绘制立体柱状图
  • 240
分享到

基于Echarts如何实现绘制立体柱状图

2023-07-05 05:07:59 240人浏览 独家记忆
摘要

本篇内容主要讲解“基于Echarts如何实现绘制立体柱状图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Echarts如何实现绘制立体柱状图”吧!实现方法先写一个常规的柱状图在这个基础上进行

本篇内容主要讲解“基于Echarts如何实现绘制立体柱状图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Echarts如何实现绘制立体柱状图”吧!

实现方法

先写一个常规的柱状图

基于Echarts如何实现绘制立体柱状图

在这个基础上进行改进

<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的配置选项

基于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    }}

其中,关于 dataIndexdataIndexInside 的区别:

  • dataIndex 指的 dataItem 在原始数据中的 index。

  • dataIndexInside 指的是 dataItem 在当前数据窗口中的 index。

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

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

所有属性

{[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()  };}

来看下我们的实现效果

基于Echarts如何实现绘制立体柱状图

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

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()      }    ]  };}

再来看下效果

基于Echarts如何实现绘制立体柱状图

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

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如何实现绘制立体柱状图

到此,相信大家对“基于Echarts如何实现绘制立体柱状图”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: 基于Echarts如何实现绘制立体柱状图

本文链接: https://www.lsjlt.com/news/349857.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 基于Echarts如何实现绘制立体柱状图
    本篇内容主要讲解“基于Echarts如何实现绘制立体柱状图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Echarts如何实现绘制立体柱状图”吧!实现方法先写一个常规的柱状图在这个基础上进行...
    99+
    2023-07-05
  • 基于Echarts实现绘制立体柱状图的示例代码
    目录前言实现方法先写一个常规的柱状图echarts的配置选项效果图前言 大家好,我是梁木由。之前在做大屏可视化项目时,UI设计了一个立体形状的柱状图,根据之前做的一些图表的项目没有能...
    99+
    2023-02-23
    Echarts绘制立体柱状图 Echarts立体柱状图 Echarts柱状图
  • vue基于echarts实现立体柱形图
    立体柱形图是由前面、右面、上面三部分组成,绘制时需要先绘制前面为一个图形,右面和上面绘制为一个图形,然后在echats中注册,在option的series中renderItem中渲染...
    99+
    2024-04-02
  • 使用Echart实现绘制立体的柱状图
    效果图: 实现代码: var xData3 = ["6", "7", "8", "9","10","11"]; var data3 = [1209, 1715, 2408,...
    99+
    2024-04-02
  • Vue使用Echarts实现立体柱状图
    本文实例为大家分享了Vue使用Echarts实现立体柱状图的具体代码,供大家参考,具体内容如下 预览: 代码: 页面部分: <template> <div ...
    99+
    2024-04-02
  • vue3.0+echarts实现立体柱图
    前言: vue3.0实现echarts立体柱图 实现效果:   实现步骤: 1、安装echarts cnpm i --save echarts 2、页面定义容器 ...
    99+
    2024-04-02
  • vue+Echart实现立体柱状图
    本文实例为大家分享了Echart+Vue制作立体柱状图,供大家参考,具体内容如下 先来看一下制作完成后的效果: 废话不多说,直接上代码: HTML代码: <div class...
    99+
    2024-04-02
  • python如何绘制柱状图
    1、插件安装 安装两种插件 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlibpip install -i https...
    99+
    2023-10-04
    matplotlib python
  • vue使用echarts实现立体柱形图
    本文实例为大家分享了vue使用echarts实现立体柱形图的具体代码,供大家参考,具体内容如下 立体柱形图是由前面、右面、上面三部分组成,绘制时需要先绘制前面为一个图形,右面和上面绘...
    99+
    2024-04-02
  • React+CSS 实现绘制竖状柱状图
    前言: 页面结构分为两个部分,柱状图 + 文字为一部分,标注为为一部分。 先来看柱状图 + 文字这一部分。 宽度定为 width: 55, height ...
    99+
    2024-04-02
  • 如何在Python中使用ECharts绘制堆叠柱状图
    在数据可视化领域,堆叠柱状图是一种常见的可视化方式。它将多个数据系列绘制成一个条形,每个条形由多个子项组成,每个子项对应一个数据系列,在同一坐标系下进行展示。这种图表可以用于比较不同类别或数据系列的总大小、每个类别或数据系列的组成比例等。在...
    99+
    2023-12-17
    Python echarts 堆叠柱状图
  • C#如何绘制饼状图和柱状图
    这篇文章主要介绍C#如何绘制饼状图和柱状图,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下#代码如下:using System;using System.Collections;usin...
    99+
    2023-06-29
  • PyQt5+QtChart实现柱状图的绘制
    目录柱状图分类实现代码效果图柱状图分类 QBarSeries:竖向柱状图 QPercentBarSeries:竖向百分比柱状图 QStackedBarSeries:竖向堆叠柱状图 Q...
    99+
    2022-12-15
    PyQt5 QtChart绘制柱状图 PyQt5 QtChart 柱状图 PyQt5 QtChart
  • vue怎么使用echarts实现立体柱形图
    今天小编给大家分享一下vue怎么使用echarts实现立体柱形图的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。立体柱形图是由...
    99+
    2023-06-29
  • Java 基于 Apache ECharts 实现:柱状图、折线图、环形图等案例
    Java 基于 Apache ECharts 实现:柱状图、折线图、环形图等案例 柱状图 效果图 源代码 #container{width: 800px;height: 600px;}//04 实例...
    99+
    2023-09-03
    echarts java 前端
  • Python实现动态柱状图的绘制
    目录一.基础柱状图二.基础时间线柱状图三.GDP动态柱状图绘制四.完整代码一.基础柱状图 如图 演示 from pyecharts.charts import Bar from p...
    99+
    2022-12-29
    Python绘制动态柱状图 Python动态柱状图 Python 柱状图
  • React+CSS实现绘制横向柱状图
    前言: 页面一共分为两个结构 文字 + 渐变柱形图为一个部分,下面的标注为一个结构。 我们先看文字 + 渐变柱形图部分。 总体使用 flex 布局,左边文字部分占总体的 50%,右...
    99+
    2024-04-02
  • Python中pyecharts如何绘制柱状图
    这篇文章主要介绍Python中pyecharts如何绘制柱状图,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、pyecharts绘制柱状图语法简介柱状/条形图,通过柱形的高度/条形的宽度来表现数据的大小。Bar.a...
    99+
    2023-06-22
  • C#如何绘制柱状图和折线图
    这篇文章给大家分享的是有关C#如何绘制柱状图和折线图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下运行效果如下图:设计上面的柱状图和折线图其实并没有什么难度,主要是各个坐标的计算,完全是精细活。首先在...
    99+
    2023-06-29
  • Qt如何使用QWT绘制柱状图
    本文将为大家详细介绍“Qt如何使用QWT绘制柱状图”,内容步骤清晰详细,细节处理妥当,而小编每天都会更新不同的知识点,希望这篇“Qt如何使用QWT绘制柱状图”能够给你意想不到的收获,请大家跟着小编的思路慢慢深入,具体内容如下,一起去收获新知...
    99+
    2023-06-28
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作