在默认的饼状图里面,图例legend颜色是黑色的,有时候根据ui须要,根据不一样的背景色,须要将图例文字调成白色或者其余颜色,那么,修改这些的仍是一句话,修改某个属性便可。javascript
主要改动的代码在这里:css
legend: { orient: 'vertical', x: 'right', y: 'bottom', textStyle: { //图例文字的样式 color: '#fff', fontSize: 16 }, data: ['在线', '离线'] },
举一个实际的例子:
demohtml
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>五分钟上手之饼状图</title> <!-- 引入 echarts.js --> <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts.min.js" type="text/javascript"></script> </head> <body> <!-- 为ECharts准备一个具有大小(宽高)的Dom --> <div id="main" style="width: 300px;height:300px;"></div> <script type="text/javascript"> $.ajax({ url: "data.json", data: {}, type: 'GET', success: function(data) { /* alert(JSON.stringify(data))*/ hrFun(data); }, }); // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); function hrFun(param) { myChart.setOption({ tooltip: { trigger: 'item', formatter: "{a} <br/>{b}: {c} ({d}%)" }, legend: { orient: 'vertical', x: 'right', y: 'bottom', textStyle: { //图例文字的样式 color: '#0b2b5e', fontSize: 12 }, data: ['在线', '离线'] }, series: [{ name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { normal: { show: false, position: 'center' }, emphasis: { show: true, textStyle: { fontSize: '30', fontWeight: 'bold' } } }, labelLine: { normal: { show: false } }, data: [{ value: param.peopleOutline, name: '在线', itemStyle: { color: '#005eff' } }, { value: param.peopleOnline, name: '离线', itemStyle: { color: '#ff9194' } }, ] }] }); } </script> </body> </html>
json前端
{"peopleTotal":15,"peopleOnline":4,"peopleOutline":12,"ranges":[]}
效果图以下所示:
能够看见,Echarts饼状图修改图例legend文字颜色和字体大小已经改变。java
原文做者:祈澈姑娘
90后前端妹子,爱编程,爱运营,文艺与代码齐飞,魅力与智慧共存的程序媛一枚jquery