echarts 角度渐变环形图心得

时间:2021-09-05 14:33:53

今天做了一个图,把自己的遇到的问题和体会记录一下,以防忘记

echarts 角度渐变环形图心得

echarts地址

https://gallery.echartsjs.com/editor.html?c=xEPtLLmG4G

参考官网地址: http://echarts.baidu.com/examples/index.html

思路:

首先需要1/4个圆形

在数据对象里面设置75和25 分别表示一个占1份,另一个占4份

 series: [{
"name": '',
"type": 'pie',
"radius": ['50%', '70%'],
"avoidLabelOverlap": false,
"startAngle": 225,
"color": [{
type: 'linear',
x: 0,
y: 0,
x2: 0.4,
y2: 1,
colorStops: [{
offset: 0,
color: color_percent0 // 0% 处的颜色
}, {
offset: 1,
color: color_percent100 // 100% 处的颜色
}],
globalCoord: false // 缺省为 false
}, 'none'],
"hoverAnimation": false,
"legendHoverLink": false,
"label": {
"normal": {
"show": false,
"position": 'center'
},
"emphasis": {
"show": true,
"textStyle": {
"fontSize": '30',
"fontWeight": 'bold'
}
}
},
"labelLine": {
"normal": {
"show": false
}
},
"data": [{
"value": 75,
"name": '1'
}, {
"value": 25,
"name": '2'
}]
}]

还有一部分就是真实数据环形

 series:[{
"name": '',
"type": 'pie',
"radius": ['50%', '70%'],
"avoidLabelOverlap": false,
"startAngle": 315,
"color": ['rgba(34,34,34,.9)', "#ff7a00", "transparent"],
"hoverAnimation": false,
"legendHoverLink": false,
"clockwise": false,
"itemStyle": {
"normal": {
"borderColor": "transparent",
"borderWidth": "20"
},
"emphasis": {
"borderColor": "transparent",
"borderWidth": "20"
}
},
"z": 10,
"label": {
"normal": {
"show": false,
"position": 'center'
}, },
"labelLine": {
"normal": {
"show": false
}
},
"data": [{
"value": (100 - 50) * 270/ 360, "label": {
normal: {
formatter: percent + '%',
position: 'center',
show: true,
textStyle: {
fontSize: '90',
fontWeight: 'normal',
color: '#fff'
}
}
},
"name": ''
}, {
"value": 1,
"name": ''
}, {
"value": 100 - (100 - 50) * 270/ 360,
"name": ''
}]
}]

这个里面需要注意的是一个算法

(100 - 50) * 270/ 360
100 - (100 - 50) * 270/ 360

我们先算出这个公式里面的270 怎么得的

75/100*360 =270

也就是在270的这个圆上进行百分比的配置

如果后台传来的是50%

(100 - 50) * 270/ 360 这样算出来的就是在3/4圆中的一半位置
还有一个地方需要注意,图中有一个颜色很亮的部分,我把这个部分设置占比为1
分为3个部分来显示

分解开是这样的

echarts 角度渐变环形图心得

其他部分用渐变来解决

说到渐变,

 "color": [{
type: 'linear',
x: 0,
y: 0,
x2: 0.4,
y2: 1,
colorStops: [{
offset: 0,
color: 'rgba(12,255,0,1)' // 0% 处的颜色
}, {
offset: 1,
color: 'rgba(12,255,0,.3)'// 100% 处的颜色
}],
globalCoord: false // 缺省为 false
}, 'none'],

我遇到一个问题,就是横轴的坐标,如果不进行设置,坐标上的宽度是页面大小和数据的值变化的,如果页面放大,横轴的坐标就会跟着自适应变大

echarts 角度渐变环形图心得

echarts 角度渐变环形图心得

如果页面放大就会更大,效果就不好了

所以在xAxis对象里设置,根据自己的需要设置值

min: function(value) {
return value.min - 7;
},
max: function(value) {
return value.max + 7;
},

startAngle:230

起始角度,支持范围[0, 360]。