1. 程式人生 > >echart折線圖,柱狀圖,餅圖設定顏色

echart折線圖,柱狀圖,餅圖設定顏色

好記性不如爛筆頭!!!

1、折線圖修改顏色:

 

  1. xAxis: {  
  2.         type: 'category',  
  3.         boundaryGap: false,  
  4.         data: ['年齡','20歲以下','30歲','40歲','50歲','60歲','60歲以上']  
  5.     },  
  6.     yAxis: {  
  7.         type: 'value'  
  8.           
  9.     },  
  10.     series: [  
  11.         {  
  12.             name:'員工數',  
  13.             type:'line',  
  14.             stack: '總量',  
  15.             itemStyle:{  
  16.                 normal:{    
  17.                      lineStyle:{    
  18.                          color:'#b5b5b6'    
  19.                      }    
  20.                  }  
  21.             },  
  22.             data:[]// 注意這裡的這個括號是要保留雖然返回的資料帶著括號!  
  23.         }  
  24.     ]  


其中的series 中的lineStyle中的 color 就是折現的顏色!

 

2、環形圖修改顏色:

 

  1. function queryData2(){  
  2.  var i=0;  
  3.  var colors=['#393939','#f5b031','#fad797','#59ccf7','#c3b4df'];  
  4.  myChart2 = echarts.init(document.getElementById('main2'));  
  5.     option2 = {  
  6.     tooltip : {  
  7.         trigger: 'item',  
  8.         formatter: "{a} <br/>{b} : {c} ({d}%)"  
  9.     },  
  10.     legend: {  
  11.         orient : 'vertical',  
  12.         x : 'left',  
  13.         data:['女','男']  
  14.     },  
  15.     toolbox: {  
  16.         show : true,  
  17.         feature : {  
  18.           
  19.             saveAsImage : {show: true}  
  20.         }  
  21.     },  
  22.     calculable : true,  
  23.     series : [  
  24.         {  
  25.             name:'性別結構',  
  26.             type:'pie',  
  27.             radius : ['30%', '70%'],  
  28.             itemStyle : {  
  29.                 normal : {  
  30.                     color:function(){  
  31.                         return colors[i++];   
  32.                         },  
  33.                     label : {  
  34.                         show : false  
  35.                     },  
  36.                     labelLine : {  
  37.                         show : false  
  38.                     }  
  39.                 },  
  40.                 emphasis : {  
  41.                     label : {  
  42.                         show : true,  
  43.                         position : 'center',  
  44.                         textStyle : {  
  45.                             fontSize : '30',  
  46.                             fontWeight : 'bold'  
  47.                         }  
  48.                     }  
  49.                 }  
  50.             },  
  51.             data:[]  
  52.         }  
  53.     ]  
  54. };  
  55.    
  56. }  

其中 函式開始定義了一個 colors 物件這裡儲存的都是顏色值,而在series中的itemStyle中的normal 中定義了一個color:function(){ return colors[i++]} 函式,這個函式的作用就是隨機獲取顏色值。這樣就修改了

 

3、柱狀圖:

 

  1. yAxis : [  
  2.              {  
  3.                  type : 'value'  
  4.              }  
  5.          ],  
  6.          series : [  
  7.              {  
  8.                  name:'部門人數',  
  9.                  type:'bar',  
  10.                  data:[],  
  11.                  //顏色  
  12.                  itemStyle:{  
  13.                      normal:{  
  14.                        color:'#f5b031',  
  15.                        }  
  16.                 },  
  17.                  markPoint : {  
  18.                      data : [  
  19.                          {type : 'max', name: '最大值'},  
  20.                          {type : 'min', name: '最小值'}  
  21.                      ]  
  22.                  },  
  23.                  markLine : {  
  24.                      data : [  
  25.                          {type : 'average', name: '平均值'}  
  26.                      ]  
  27.                  }  
  28.              }  
  29.          ]  


顏色的修改還是在series 中的itemStyle 中的normal 中的color這個值。

 

4、餅圖顏色修改:

 

  1. var  option = {  
  2.               tooltip: {  
  3.                trigger: 'item',  
  4.                formatter: "{a} <br/>{b}: {c} ({d}%)"  
  5.                  },  
  6.               //設定餅圖的顏色  
  7.              color:['#f6da22','#bbe2e8','#6cacde'],  
  8.              legend: {  
  9.                       orient: 'vertical',  
  10.                       x: 'left',  
  11.                       data:['柴油','汽油','附屬油'],  
  12.                       show:false  
  13.                     },  


餅圖的顏色修改和折線圖 環形圖不同,他是單獨出來的!