ECharts圖表實戰經驗1:如何設置圖表同序列不同數據點的獨立顏色值


最近有不少朋友在追問這樣一個問題:我單序列的柱狀圖,我想讓每一個根柱子的顏色都不一樣,應該如何做?

針對這個問題,其實我只想說你壓根沒有認真看完或者查找ECharts官方的示例,官方能夠找到的示例有:

1、http://echarts.baidu.com/doc/example/bar14.html

2、http://echarts.baidu.com/doc/example/bar15.html

那么你看完過后是否能夠明白其中的道理呢?數據點的屬性首先是通過itemStyle節點進行控制的,我們要控制數據點的顏色,自然我們就需要設置color,另外根據ECharts的API介紹,color是支持Function函數的。

我們首先來看看這樣一個例子:

示例一:根據數據點所在序號從一個顏色數組內拿拿取對應顏色值

我們首先設置一個顏色數組,最好比序列內的數據點個數要大或者相等,結合itemStylecolor的函數根據當前數據點在當前序列內所處的順序序號去顏色數組內自動匹配顏色。

代碼如下所示:

option = {
    title : {
        text: '某地區蒸發量和降水量',
        subtext: '純屬虛構'
    },
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['蒸發量']
    },
    toolbox: {
        show : true,
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            magicType : {show: true, type: ['line', 'bar']},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    calculable : true,
    xAxis : [
        {
            type : 'category',
            data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'蒸發量',
            type:'bar',
            data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
           itemStyle: {
                normal: {
                    color: function(params) {
                        // build a color map as your need.
                        var colorList = [
                          '#C1232B','#B5C334','#FCCE10','#E87C25','#27727B',
                           '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
                           '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0'
                        ];
                        return colorList[params.dataIndex]
                    }
                }
            }
        }
    ]
};                  

 

效果是不是很炫?!

 

示例二:通過配置數據點的顏色擴展屬性來達到控制不同數據點的顏色

我們最想實現的應該是我們自己能夠設置每一個數據點的顏色值,而非通過設置顏色數組的形式,那么我們應該如何做呢?

1、我們需要改寫series的data格式,之前是一個一維數據類型的數組,先走我們需要將至變成一個對象類型的一維數組,如下所示;

 
         
data:[{
              value:2.0,
              color:"red"
            }, 4.9, 7.0, {
              value:23.2,
              color:"green"
            }, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
 

某些數據點我設置了其color屬性,也就是我要用所配置的顏色來渲染柱子。

2、為了沒有配置顏色屬性的數據點的顏色顯示有所歸屬(因為我們通過params找不到當前序列的顏色,所以我們最好自己給其series設置一個顏色屬性。如下所示:

{
            name:'蒸發量',
            type:'bar',
            color:"#ff7f50",
            data:[{
              value:2.0,
              color:"red"
            }, 4.9, 7.0, {
              value:23.2,
              color:"green"
            }, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
            itemStyle: {
                normal: {
                  color: function(params) {                    
                    if(params.series.data[params.dataIndex] != null && params.series.data[params.dataIndex].color != undefined)
                    {
                       return params.series.data[params.dataIndex].color;
                    }else
                    {
                      return params.series.color;
                    }
                  }
                }
            }
        }

最后我們就可以通過改寫itemStyle內color的function函數規則來返回數據點對象所配置的顏色以及序列所配置的顏色。

當數據點對象尚未配置顏色color屬性時,我們就返回當前序列所配置的color即可。

示例代碼的option配置如下所示:

option = {
    title : {
        text: '某地區蒸發量和降水量',
        subtext: '純屬虛構'
    },
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['蒸發量']
    },
    toolbox: {
        show : true,
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            magicType : {show: true, type: ['line', 'bar']},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    calculable : true,
    xAxis : [
        {
            type : 'category',
            data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'蒸發量',
            type:'bar',
            color:"#ff7f50",
            data:[{
              value:2.0,
              color:"red"
            }, 4.9, 7.0, {
              value:23.2,
              color:"green"
            }, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
            itemStyle: {
                normal: {
                  color: function(params) {                    
                    if(params.series.data[params.dataIndex] != null && params.series.data[params.dataIndex].color != undefined)
                    {
                       return params.series.data[params.dataIndex].color;
                    }else
                    {
                      return params.series.color;
                    }
                  }
                }
            }
        }
    ]
};
                    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM