主要代码:
normal: { color: function(params) { var colorList = [ { c1: ' #fce5ca', //管理 c2: '#FF9D62' }, { c1: ' #508DFF', //实践 c2: '#26C5FE' }, { c1: '#63E587',//操作 c2: '#5FE2E4' }] return new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上 offset: 0, color: colorList[params.dataIndex].c1 }, { offset: 1, color: colorList[params.dataIndex].c2 }]) } }
参考代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.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" class="col-md-12 col-sm-12 col-xs-12" style="height: 400px;"></div> <script> //基于准备好的dom,初始化echarts实例 var cChart = echarts.init(document.getElementById('main')); var names = []; //类别数组(用于存放饼图的类别) var brower = []; $.ajax({ type: 'get', url: "data.json", dataType: "json", //返回数据形式为json success: function(data) { //请求成功时执行该函数内容,result即为服务器返回的json对象 $.each(data.list, function(index, item) { names.push(item.value); //挨个取出类别并填入类别数组 brower.push({ name: item.value, value: item.name }); }); cChart.setOption({ //加载数据图表 title: { }, legend: { textStyle: { //图例文字的样式 color: '#000', fontSize: 12 }, type: 'scroll', orient: 'vertical', right: 10, top: 20, bottom: 20, data: names }, series: [{ name: '姓名', type: 'pie', radius: '55%', center: ['40%', '50%'], data: brower, itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' }, normal: { color: function(params) { var colorList = [ { c1: ' #fce5ca', //管理 c2: '#FF9D62' }, { c1: ' #508DFF', //实践 c2: '#26C5FE' }, { c1: '#63E587',//操作 c2: '#5FE2E4' }] return new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上 offset: 0, color: colorList[params.dataIndex].c1 }, { offset: 1, color: colorList[params.dataIndex].c2 }]) /* return colorList[params.dataIndex]*/ } } } }] }); }, error: function(errorMsg) { //请求失败时执行该函数 alert("图表请求数据失败!"); } }); </script> </body> </html>
json数据
{"list":[{"value":"管理工","name":40}, {"value":"实践工","name":140}, {"value":"操作工","name":31}]}