Echarts餅狀圖數據交互請求ajax自定義顏色

normal : {
color : function(params) {
//自定義顏色
var colorList = [ '#2059be', '#198577',
'#a8674e' ];
return colorList[params.dataIndex]
}
}

示例:javascript

<!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: {
                            text: '每日睡眠分佈',
                            // subtext:'',
                            x: 'center',
                            y: '7px',
                            textStyle: { //圖例文字的樣式
                                color: '#3A7BD5',
                                fontSize: 16
                            },
                            textAlign: 'left'
                        },
                        legend: {
                            /*orient: 'vertical',
                            x: 'right',
                            y: 'bottom',*/
                            textStyle: { //圖例文字的樣式
                                color: '#fff',
                                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 = ['#2059be', '#198577',
                                            '#a8674e'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            }

                        }]
                    });
                },
                error: function(errorMsg) {
                    //請求失敗時執行該函數
                    alert("圖表請求數據失敗!");
                }
            });
        </script>
    </body>
</html>

jsoncss

{"list":[{"value":"管理","name":40},
{"value":"管理","name":140},
{"value":"操做工","name":31}]}

效果以下,每塊區域都是自定義的顏色了。html


5640239-6bb5a1be1eec8b5e.png
相關文章
相關標籤/搜索