Echarts升级2:柱状图头部显示百分比、同时内容在弹框显示

目录

需求及技术说明:

效果图: 

根据排污口的排口情况应用echarts的柱状图:

详解1:头部添加百分比

详解2:在弹框不出现百分比

详解3:一种参数同时出现两种值,并且分别展现


 

需求及技术说明:

效果图: 

根据排污口的排口情况应用echarts的柱状图:

  • 头部展示百分比
  • 内容展示排口量
  • 展示多个排口名称及单位
  • 通过官网,展示的弹框内容基本是展示所有数据

所以,要自定义弹框的内容。

对于多个排口数据的展示,需要循环来插入series,对于头部的数据,需要设置样式。

至于,使两数据集合与一点,这里是柱子重合到一起。

最后,浮窗位置一定要设置,否则会有偏差 !

话不多说,上代码,详解见下面

//折线图初始化
      setEchart() {  
        this.myChart = echarts.init(document.getElementById('mainEcharts'));
        var tmp = []
        for (var j in this.headerDataTwo) {  
          this.valueLine = [] 
          this.valueper = []
          for (var i in this.historyDataLine) {
            this.valueLine.push(this.historyDataLine[i].ems[j])
            this.valueper.push(parseFloat(this.historyDataLine[i].per[j]))
          }
          var per = {
            name: this.headerDataTwo[j],
            type: 'bar',
            barWidth: 5,
            barGap:'80%',/*多个并排柱子设置柱子之间的间距*/
            barCategoryGap:'50%',/*多个并排柱子设置柱子之间的间距*/
            stack: this.headerDataTwo[j],
            data: this.valueLine
          };
          tmp.push(per)
          var temp = {
            name: this.headerDataTwo[j],
            type: 'bar',
            barWidth: 5,
            barGap:'80%',/*多个并排柱子设置柱子之间的间距*/
            barCategoryGap:'50%',/*多个并排柱子设置柱子之间的间距*/
            stack: this.headerDataTwo[j],
             label: {
                show: true,
                position: 'top',
                formatter: function(v){
                  if(v.data!='0'){
                     return v.data + '%'
                  }else{
                    return ''
                  }
                  
                },
            },
            data: this.valueper,
          };
          tmp.push(temp)
        }
        // 指定图表的配置项和数据
        this.myChart.setOption({
          tooltip: {
            trigger: 'axis',
						// 浮窗位置
						position: function (point, params, dom, rect, size) {
							var x = 0;
							var y = 0;
							var pointX = point[0];
							var pointY = point[1];
							var boxWidth = size.contentSize[0];
							var boxHeight = size.contentSize[1];
							if (boxWidth > pointX) {
								x = 5;
							} else { // 左边放的下
								x = pointX - boxWidth;
							}
							if (boxHeight > pointY) {
								y = 5;
							} else { // 上边放得下
								y = pointY - boxHeight;
							}
							return [x, y];
						},
						//浮窗内容
						formatter(params) {
              console.log(params)
              var pa = ''
              for(var i in params){
                if(i%2 ==0){
                pa = '<p>'+params[i].marker +params[i].seriesName + ':' + params[i].value+'<br>'+pa
                }
              }
              return pa
						}
          },
          legend: {
            data: this.headerDataTwo
          },
          grid: {
            left: '2%',
            right: '5%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            name: "日期",
            type: "category",
            boundaryGap: true,
            data: this.xAxis.map(function (str) {
              return str.replace(' ', '\n')
            })
          },
          yAxis: {
            name: "值",
            type: 'value'
          },
          series: tmp,
          
        });

      },

 

接口内容: 

详解1:头部添加百分比

var temp = {
            name: this.headerDataTwo[j],
            type: 'bar',
            barWidth: 5,
            barGap:'80%',/*多个并排柱子设置柱子之间的间距*/
            barCategoryGap:'50%',/*多个并排柱子设置柱子之间的间距*/
            stack: this.headerDataTwo[j],
             label: {
                show: true,
                position: 'top',
                formatter: function(v){
                  if(v.data!='0'){
                     return v.data + '%'
                  }else{
                    return ''
                  }
                  
                },
            },
            data: this.valueper,
          };
          tmp.push(temp)

循环出来push进数组,设置样式lable,(注意:后台数据之间给%是不可以的,只能自己加%)

详解2:在弹框不出现百分比

//浮窗内容
						formatter(params) {
              console.log(params)
              var pa = ''
              for(var i in params){
                if(i%2 ==0){
                pa = '<p>'+params[i].marker +params[i].seriesName + ':' + params[i].value+'<br>'+pa
                }
              }
              return pa
						}

判断一下当奇数的时候不显示!(注意:一定要手动加上颜色的色块params[i].marker

详解3:一种参数同时出现两种值,并且分别展现

这里是把柱子进行的重合,然后把弹框的内容进行自定义!

扩展1   柱状图颜色设置:

series: [{
        data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar',
        itemStyle: {
            //配置样式
            itemStyle: {   
                //通常情况下:
                normal:{  
            //每个柱子的颜色即为colorList数组里的每一项,如果柱子数目超出,则柱子颜色循环使用该数组
                    color: function (params){
                        var colorList = ['#5EC2DF','#EDAC6D','#CC6633','#F9E8DC','#5BD27C'];
                        return colorList[params.dataIndex];
                    }
                },
            },
        }
    }]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
function getGzMap(_data) { if (_chinaMap == undefined) { var dom = document.getElementById("container"); _chinaMap = echarts.init(dom); _chinaMap.on('click', function(params) { console.log(params); var _type = params.seriesType; if (_type == "map") { //window.parent.aaa('aa') //调用父页面方法 } else if (_type == "effectScatter") { window.parent.showMap(); } }) } var option = { backgroundColor: 'rgba(0,0,0,0)', visualMap: { type: 'piecewise', show: false, min: 0, max: 300, splitNumber: 3, itemWidth: 10, itemHeight: 10, itemGap: 5, seriesIndex: [1], pieces: [ { min: 0, max: 100, label: '优' }, { min: 101, max: 200, label: '良' }, { min: 201, max: 300, label: '高风险' } ], //color: ['#FA4D08', '#4BD94F', '#FBD32B'], //红、绿、黄 color: ['#F8DAE6', '#FEF9B5', '#B0D8B3'], //红、黄、绿 textStyle: { color: '#9EA8B1', fontSize: 10 } }, tooltip: { formatter: '{b}' }, geo: { map: 'guangdong', roam: true, aspectScale: 1, zoom: 1.5, layoutCenter: ['55%', '40%'], layoutSize: 500, label: { normal: { show: true }, emphasis: { show: true } }, itemStyle: { normal: { areaColor: '#323c48', borderColor: '#111', borderColor: '#3BB4DF', shadowColor: '#25A3FC', shadowBlur: 10 }, emphasis: { areaColor: '#ddb926' } } }, series: [{ type: 'effectScatter', coordinateSystem: 'geo', data: unitData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/fire.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, { name: '', type: 'map', geoIndex: 0, mapType: 'guangdong', // 自定义扩展图表类型 label: { normal: { show: true, } }, itemStyle: { normal: { label: { show: true, fontSize: 10, color: '#111' }, shadowColor: '#ddb926', shadowBlur: 5, }, emphasis: { label: { show: true }, shadowColor: 'rgba(0, 0, 0, 0.5)', shadowBlur: 10 } }, data: _data }, { type: 'effectScatter', coordinateSystem: 'geo', data: windData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/wind.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, ] }; $.getJSON('../../MapCN/guangdong.json', function(chinaJson) { echarts.registerMap('guangdong', chinaJson); _chinaMap.setOption(option, true); }); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BMG-Princess

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值