function getHexOpacityColor({ color = '#000', opacity = 0.9 }) { opacity = Math.max(opacity, 0); opacity = Math.min(opacity, 1); color = color.replace(/\#/g, '').toUpperCase(); if (color.length === 3) { let arr = color.split(''); color = ''; for (let i = 0; i < arr.length; i++) { color += (arr[i] + arr[i]); //将简写的3位字符补全到6位字符 } } let num = Math.round(255 * opacity); //四舍五入 let str = ''; let arrHex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]; //十六进制数组 while (num > 0) { let mod = num % 16; num = (num - mod) / 16; str = arrHex[mod] + str; } if (str.length == 1) str = '0' + str; if (str.length == 0) str = '00'; return `#${color+str}`; } var dom = document.getElementById("container"); var myChart = echarts.init(dom); var app = {}; var datas = [{ value: 44, name: '小客车' }, { value: 40, name: '中大型客车' }, { value: 32, name: '大货车' }, { value: 28, name: '小货车' }, { value: 38, name: '超长车' } ] var color = ['#04e4fc', '#103669', '#4e186f', '#d99a15', '#473a40'] var option; option = { legend: { align: 'left', right: '4%', itemWidth: 10, itemHeight: 10, orient: 'vertical', top: '20%', icon: 'circle', textStyle: { color: '#91badb' }, formatter: function(name) { for (let i = 0; i < datas.length; i++) { if (name === datas[i].name) { return `${name}${datas[i].value}位` } } } }, toolbox: { show: false, }, series: [{ type: 'pie', radius: [0, 70], center: ['30%', '50%'], roseType: 'area', itemStyle: { color: function(val) { let _color = color[val.dataIndex] return getHexOpacityColor({ color: _color, opacity: 0.4 }) }, borderColor: 'rgba(255,255,255,0.6)', borderWidth: 2, borderRadius: 2 }, label: { color: '#779bbe' }, labelLine: { normal: { length: 1 } }, data: datas }] }; if (option && typeof option === 'object') { myChart.setOption(option); }