echarts的柱形图增加背景框,背景框比柱形图宽

写项目时,UI设计的原型图上,有背景,一比一还原,没办法写呗

要求的图:

代码:

//数据处理

 getData() {

       let data= [

        {

            "name": "设备1",

            "totalCount": 9

        },

        {

            "name": "设备2",

            "totalCount": 8

        },

        {

            "name": "设备3",

            "totalCount": 6

        },

        {

            "name": "设备4",

            "totalCount": 4

        },

        {

            "name": "设备5",

            "totalCount": 2

        }

    ];

    let xAxis=[];

    let yAxis=[];

    data.forEach((item,index)=>{

      xAxis.push(item.name);

      yAxis.push(item.totalCount);

    })

    this.init(xAxis,yAxis);

    },

//echarts的代码

 init(xAxis,yAxis) {

//背景需要是柱形图的最大值,所以使用math函数

      let maxData=Math.max(...yAxis);      

//因为我的柱形图要求的是渐变效果,所以我在这写了个colorlist,使用graphic时需要引用嗯,要不报错呢

      let colorlist=[new graphic.LinearGradient(

                0, 0, 0, 1,

                [

                  {offset: 0, color: '#FED379'},

                  {offset: 1, color: '#0C1528'}

                ]

              ),new graphic.LinearGradient(

                0, 0, 0, 1,

                [

                  {offset: 0, color: '#FED379'},

                  {offset: 1, color: '#0C1528'}

                ]

              ),new graphic.LinearGradient(

                0, 0, 0, 1,

                [

                  {offset: 0, color: '#FED379'},

                  {offset: 1, color: '#0C1528'}

                ]

              ),new graphic.LinearGradient(

                0, 0, 0, 1,

                [

                  {offset: 0, color: '#71FFCD'},

                  {offset: 1, color: '#0C1528 '}

                ]

              ),new graphic.LinearGradient(

                0, 0, 0, 1,

                [

                  {offset: 0, color: '#71FFCD'},

                  {offset: 1, color: '#0C1528 '}

                ]

              ),];

      let backColorlist=['rgba(254, 211, 121, 0.06)','rgba(254, 211, 121, 0.06)','rgba(254, 211, 121, 0.06)',

      'rgba(113, 255, 205, 0.06)','rgba(113, 255, 205, 0.06)']

      this.option = {  

            grid: {

              top: '15%',

              left: '0%',

              right: '0%',

              bottom: '2%',

              containLabel: true

            },

            xAxis: {

              type: "category",

              data: xAxis,          

              axisLabel: {

                color: '#E7FAFF',

                fontSize: 12,

                fontFamily: 'PingFang SC',

                fontWeight: 'normal',

                rotate: 14,            

              },

              axisLine: {              

                lineStyle: {

                  color: '#6F7E93',

                  width: 1,

                }

              },

              splitLine: {

                show: false,

              },

            },

            yAxis: {

              type: "value",

              name: "(个)",

              nameTextStyle: {

                color: "#A8C7DB",

                fontSize: 12,

                fontFamily: 'PingFang SC',

                fontWeight: 600,

                padding: [0, 0, 0, -35],

              },

              nameGap:5,

              nameLocation: "end",

              splitLine: {

                show: false,            

              },

              axisLabel: {

                color: '#A8C7DB',

                fontSize: 12,

                fontFamily: 'PingFang SC',

                fontWeight: 600,  

                interval:2,            

              },

              max: function(value) {

                return value.max;  // 设置 y 轴的最大值为数据的最大值

              },        

            },

            series: [

            {

              type: 'bar',

              animationDuration: 0,//关键代码  初始动画的时长,支持回调函数,可以通过每个数据返回不同的时长实现更戏剧的初始动画效果:

              animationDurationUpdate: 0,//关键代码  数据更新动画的时长。支持回调函数,可以通过每个数据返回不同的时长实现更戏剧的更新动画效果:

              itemStyle: {

                normal: {

                color: function(params) {

                  return backColorlist[params.dataIndex];

                }                

              }

              },

              data:[maxData+5,maxData+5,maxData+5,maxData+5,maxData+5],

              barWidth: '37',

              z: 1,//层级问题,加上嗯,这个必须加嗯,

            },

          {

            data: yAxis,

            type: 'bar',

            barWidth: 12,

            itemStyle: {

              normal: {

                color: function(params) {

                  return colorlist[params.dataIndex];

                }                

              }

            },

            label: {

              show: true,

              position: 'top',

              color: '#ffffff',

              fontSize: 12,

              fontFamily: 'PingFang SC',

              fontWeight: 600,  

            },

            barGap: '-65%',  //这句话就是为了让柱形图的柱子到达背景图上

          }        

        ]

      };  

    },

  • 31
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Echarts柱形图中添单个背景图案,你需要在 Echarts 中使用图形元素的方式来实现。具体步骤如下: 1. 在 Echarts 的 option 中,使用 graphic 元素定义一个背景图案。 2. 在定义背景图案时,可以使用 image 元素来背景图案文件,并设置其位置和大小。 3. 然后,使用 rect 元素定义一个矩形元素,作为柱形图背景,并设置其大小和位置。 4. 最后,将矩形元素和背景图案元素添Echarts 的 series 中,即可实现柱形图背景图案的添。 下面是一个示例代码: ```javascript option = { graphic: [{ type: 'image', id: 'background', left: 0, top: 0, z: -10, bounding: 'raw', style: { image: 'background.png', width: 600, height: 400 } }], series: [{ type: 'bar', data: [10, 20, 30, 40, 50], barWidth: 20, itemStyle: { normal: { color: '#00bfff' } }, backgroundStyle: { color: 'none', image: 'none' }, markLine: { symbol: 'none', lineStyle: { type: 'dashed' }, data: [{ yAxis: 30 }] }, markPoint: { symbol: 'circle', data: [{ xAxis: 2, yAxis: 30 }] }, graphic: [{ type: 'rect', left: 0, top: 0, z: -9, shape: { width: '100%', height: '100%' }, style: { fill: '#fff' } }, { type: 'image', id: 'background', left: 0, top: 0, z: -10, bounding: 'raw', style: { image: 'background.png', width: 600, height: 400 } }] }] }; ``` 在这个示例代码中,我们首先在 graphic 元素中定义了一个背景图案,然后在 series 中定义了一个矩形元素作为柱形图背景,并将背景图案和矩形元素都添到了柱形图的 graphic 中。 注意,为了使背景图案在柱形图下方,我们需要将其 z 值设置为 -10,而矩形元素的 z 值设置为 -9,这样矩形元素就会在背景图案上方。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值