其实这三种图我都认为是一种图,都是饼图的变形,需求长这样子
我的效果图这样子
想要一模一样自己改改颜色就行了
我们先生成完整的蚊香图,众所周知,我们正常的饼图只是把seriesData配置一
次,然后把数据全部塞进去,就好,如官网示例
那想要生成很多条数据,我们把series里面的每一条数据都按比例分
(比如100处长啥样,40分长啥样,60分长啥样),搞成很多条就好了,循环数据,把每一个数据都搞成一个seriesData数据条
,就变成一圈一圈的了
const obj = {//定义要用到的变量:series: [],maxRadius: 90,barWidth: 6,barGap: 2,// sumValue: 0,showValue: true,showPercent: true,option: {},//图形数据PieDatas: [{"value": 95,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 85,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 35,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 65,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 100,"name": "XXXXXXXXXXXXXXXX完整率识别准确率"},{"value": 55,"name": "XXXXXXX完整率时钟准确率"},{"value": 75,"name": "车XXXXXXXXXX数据XXXXXXXXXX完整率上传率"}, {"value": 95,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率上传率"}, {"value": 15,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率可用率"}, {"value": 25,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率用率"},],//图形颜色BarColor: [{"color1": "#4E9DFF","color2": ""},{"color1": "#36C4F7","color2": ""},{"color1": "#65D4AB","color2": ""},{"color1": "#9FFF8D","color2": ""},{"color1": "#FFE154","color2": ""},{"color1": "#FFB854","color2": ""}, {"color1": "#FF9254","color2": ""}, {"color1": "#FF8181","color2": ""}, {"color1": "#FF81BA","color2": ""}, {"color1": "#DC81FF","color2": ""},],
}
我们制作两个 series数据条,直接把数据条显示成有颜色
的,不足100%的用灰色
显示,以下是series中的data写法
data: [{value: item.value,name: item.name,itemStyle: {color: obj.BarColor[i].color1}}, {value: 100 - item.value,name: '',itemStyle: {color: "transparent",},tooltip: {show: false},hoverAnimation: false}]
完整版循环代码
obj.PieDatas.forEach((item, i) => {obj.series.push({center: ['50%', '60%'],type: 'pie',// clockWise: false, //顺时加载hoverAnimation: false, //鼠标移入变大radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],label: {show: false},itemStyle: {label: {show: false,},labelLine: {show: false},borderWidth: 5,},data: [{value: item.value,name: item.name,itemStyle: {color: obj.BarColor[i].color1}}, {value: 100 - item.value,name: '',itemStyle: {color: "transparent",},tooltip: {show: false},hoverAnimation: false}]})})
想制作带底色
的图,我们再写一个series
, 底色改成灰色就行,蚊香图就出来了
obj.PieDatas.forEach((item, i) => {obj.series.push({// startAngle: 0,// endAngle: 180,// radius: ['40%', '70%'],center: ['50%', '60%'],name: 'blank',type: 'pie',silent: true,z: 0,// clockWise: false, //顺时加载hoverAnimation: false, //鼠标移入变大radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],// center: ["30%", "50%"],label: {show: false},itemStyle: {label: {show: false,},labelLine: {show: false},borderWidth: 5,},data: [{value: 1,itemStyle: {color: "#f7f7f7",//圆圈另一半的颜色// color: "transparent",borderWidth: 0},tooltip: {show: false},hoverAnimation: false}]});})
最后就是制作成半圆形,制作半圆的精髓就是咱们把刚才设置的第2个series设置成透明色,
或者把直接把第2个series数据删除
,然后第一个series数据条的底色设置成想要的颜色就ok,然后再设置`起始角度结束角度等,我们先来看这个图
咱们设置startAngle和endAngle,然后根据自己的需求看是否要设置顺时加载colockWise等
属性,要设置彩虹底色可以自定义颜色等,直接放setOption完整代码吧
obj.PieDatas.forEach((item, i) => {obj.series.push({startAngle: 0,endAngle: 180,// radius: ['40%', '70%'],center: ['50%', '60%'],type: 'pie',// clockWise: false, //顺时加载hoverAnimation: false, //鼠标移入变大radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],// center: ["30%", "50%"],label: {show: false},itemStyle: {label: {show: false,},labelLine: {show: false},borderWidth: 5,},data: [{value: 100,itemStyle: {normal: {color: "transparent" // 透明色}}, name: ''}, {value: item.value,name: item.name,itemStyle: {color: obj.BarColor[i].color1}}, {value: 100 - item.value,name: '',itemStyle: {color: "#f7f7f7",},tooltip: {show: false},hoverAnimation: false}]})// 制作半圆图时这段要不要都可// obj.series.push({// startAngle: 0,// endAngle: 180,// // radius: ['40%', '70%'],// center: ['50%', '60%'],// name: 'blank',// type: 'pie',// silent: true,// z: 0,// // clockWise: false, //顺时加载// hoverAnimation: false, //鼠标移入变大// radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],// // center: ["30%", "50%"],// label: {// show: false// },// itemStyle: {// label: {// show: false,// },// labelLine: {// show: false// },// borderWidth: 5,// },// data: [{// value: 1,// itemStyle: {// // color: "#f7f7f7",//圆圈另一半的颜色// color: "transparent",// borderWidth: 0// },// tooltip: {// show: false// },// hoverAnimation: false// }]// });})obj.option = {// startAngle: 180,// endAngle: 360,grid: {left: 0,right: 0,top: 0,bottom: 0,},backgroundColor: '#fff',tooltip: {show: true,trigger: "item",},legend: {//右侧文字show: true,left: '20%',top: 'bottom',icon: "circle",itemWidth: 6,itemHeight: 8,itemGap: 5,textStyle: {//右侧字体样式rich: {bothNameValue: {width: 300, //给文字设置统一长度,保证右侧的百分比对齐},title: {fontSize: 14,lineHeight: 16,color: '#999999',width: 260,},value: {color: '#489DF7',fontSize: 18,fontFimely: 'DIN Alternate'}}},formatter: (name) => {var datas = obj.PieDatas;let total = 0;datas.map(item => {total += (item.value - 0)})let valueIndex = datas.map(item => item.name).indexOf(name);let htmlValue = obj.showPercent ? datas[valueIndex].value + "%" : ''return "{title|" + name + "} {value|" + htmlValue + "}"// return name + " " + (obj.showPercent ? ((datas[valueIndex].value / total) * 100).toFixed(2) + "%" : '');},},series: obj.series,}
react demo完整版代码,可直接复制粘贴
import React, { useEffect, useState } from 'react';const obj = {//定义要用到的变量:series: [],maxRadius: 90,barWidth: 6,barGap: 2,// sumValue: 0,showValue: true,showPercent: true,option: {},//图形数据PieDatas: [{"value": 95,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 85,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 35,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 65,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率"},{"value": 100,"name": "XXXXXXXXXXXXXXXX完整率识别准确率"},{"value": 55,"name": "XXXXXXX完整率时钟准确率"},{"value": 75,"name": "车XXXXXXXXXX数据XXXXXXXXXX完整率上传率"}, {"value": 95,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率上传率"}, {"value": 15,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率可用率"}, {"value": 25,"name": "XXXXXXXXXX数据XXXXXXXXXX完整率用率"},],//图形颜色BarColor: [{"color1": "#4E9DFF","color2": ""},{"color1": "#36C4F7","color2": ""},{"color1": "#65D4AB","color2": ""},{"color1": "#9FFF8D","color2": ""},{"color1": "#FFE154","color2": ""},{"color1": "#FFB854","color2": ""}, {"color1": "#FF9254","color2": ""}, {"color1": "#FF8181","color2": ""}, {"color1": "#FF81BA","color2": ""}, {"color1": "#DC81FF","color2": ""},],
}const PieChart = (props) => {const {// data,// isEmpty,width = '500px',height = '500px',// radius = [fitChartSize(50), fitChartSize(60)], // 饼图的半径// title = '',// subTitle = '达成率',// gradient = false, // 渐变// showLable = false,// centerTextFontSize = fitChartSize(16)// color,// value,// value2,// value3,// value4,// tooltipShowPercent,// unit = '',} = props;const [option, setOption] = useState(null);useEffect(() => {setOptionData();}, []);const setOptionData = () => {// 指定图表的配置项和数据console.log(' obj.pieDatas', obj)obj.PieDatas.forEach((item, i) => {obj.series.push({startAngle: 0,endAngle: 180,// radius: ['40%', '70%'],center: ['50%', '60%'],type: 'pie',// clockWise: false, //顺时加载hoverAnimation: false, //鼠标移入变大radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],// center: ["30%", "50%"],label: {show: false},itemStyle: {label: {show: false,},labelLine: {show: false},borderWidth: 5,},data: [{value: 100,itemStyle: {normal: {color: "transparent" // 透明色}}, name: ''}, {value: item.value,name: item.name,itemStyle: {color: obj.BarColor[i].color1}}, {value: 100 - item.value,name: '',itemStyle: {color: "#f7f7f7",},tooltip: {show: false},hoverAnimation: false}]})// obj.series.push({// startAngle: 0,// endAngle: 180,// // radius: ['40%', '70%'],// center: ['50%', '60%'],// name: 'blank',// type: 'pie',// silent: true,// z: 0,// // clockWise: false, //顺时加载// hoverAnimation: false, //鼠标移入变大// radius: [(obj.maxRadius - i * (obj.barGap + obj.barWidth)) + '%', (obj.maxRadius - (i + 1) * obj.barWidth - i * obj.barGap) + '%'],// // center: ["30%", "50%"],// label: {// show: false// },// itemStyle: {// label: {// show: false,// },// labelLine: {// show: false// },// borderWidth: 5,// },// data: [{// value: 1,// itemStyle: {// // color: "#f7f7f7",//圆圈另一半的颜色// color: "transparent",// borderWidth: 0// },// tooltip: {// show: false// },// hoverAnimation: false// }]// });})obj.option = {// startAngle: 180,// endAngle: 360,grid: {left: 0,right: 0,top: 0,bottom: 0,},backgroundColor: '#fff',tooltip: {show: true,trigger: "item",},legend: {//右侧文字show: true,left: '20%',top: 'bottom',icon: "circle",itemWidth: 6,itemHeight: 8,itemGap: 5,textStyle: {//右侧字体样式rich: {bothNameValue: {width: 300, //给文字设置统一长度,保证右侧的百分比对齐},title: {fontSize: 14,lineHeight: 16,color: '#999999',width: 260,},value: {color: '#489DF7',fontSize: 18,fontFimely: 'DIN Alternate'}}},formatter: (name) => {var datas = obj.PieDatas;let total = 0;datas.map(item => {total += (item.value - 0)})let valueIndex = datas.map(item => item.name).indexOf(name);let htmlValue = obj.showPercent ? datas[valueIndex].value + "%" : ''return "{title|" + name + "} {value|" + htmlValue + "}"// return name + " " + (obj.showPercent ? ((datas[valueIndex].value / total) * 100).toFixed(2) + "%" : '');},},series: obj.series,}setOption(obj.option)}return <MyChart chartOption={option} width={width} height={height} />;
}export default PieChart;
MyChart 就是把echarts init封装了一下,自己按官网的示例,把数据塞进去就行了