Echarts餅圖實現顏色漸變

主要代碼:javascript

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
                                        }])                                    
                                      
                                    }
                                }

參考代碼css

<!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數據html

{"list":[{"value":"管理工","name":40},
{"value":"實踐工","name":140},
{"value":"操做工","name":31}]}
5640239-140c7c5c41fbbc14.png
相關文章
相關標籤/搜索