這篇文章給大家介紹使用echarts怎么設置圖例顏色,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1、圖例的顏色代碼
refresh: function (newOption) {
if (newOption) {
this.option = newOption || this.option;
this.option.legend = this.reformOption(this.option.legend);
this.legendOption = this.option.legend;
var data = this.legendOption.data || [];
var itemName;
var something;
var color;
var queryTarget;
if (this.legendOption.selected) {
for (var k in this.legendOption.selected) {
this._selectedMap[k] = typeof this._selectedMap[k] != 'undefined' ? this._selectedMap[k] : this.legendOption.selected[k];
}
}
for (var i = 0, dataLength = data.length; i < dataLength; i++) {
itemName = this._getName(data[i]);
if (itemName === '') {
continue;
}
something = this._getSomethingByName(itemName);
if (!something.series) {
this._hasDataMap[itemName] = false;
} else {
this._hasDataMap[itemName] = true;
if (something.data && (something.type === ecConfig.CHART_TYPE_PIE || something.type === ecConfig.CHART_TYPE_FORCE || something.type === ecConfig.CHART_TYPE_FUNNEL)) {
queryTarget = [
something.data,
something.series
];
} else {
queryTarget = [something.series];
}//可以看到下面這一句commend by danielinbiti,圖例顏色先查找series是否設置了itemStyle.normal.color這個屬性進行判斷,如果沒有,則會按照默認的顏色設置取值。如果設置了,就按照設置的顏色取值?,F在想設置,肯定需要在series中對應的坐標系中設置顏色。
color = this.getItemStyleColor(this.deepQuery(queryTarget, 'itemStyle.normal.color'), something.seriesIndex, something.dataIndex, something.data); if (color && something.type != ecConfig.CHART_TYPE_K) { this.setColor(itemName, color); } this._selectedMap[itemName] = this._selectedMap[itemName] != null ? this._selectedMap[itemName] : true; } } } this.clear(); this._buildShape(); },2、于是可能產生了如下一個坐標系設置代碼
{
name: 'iphone3',
type: 'map',
mapType: 'china',
selectedMode:'single',
roam: true,
showLegendSymbol:true,
itemStyle:{
normal:{
label:{show:true}
,areaStyle:{color:'green'} //設置地圖背景色的顏色設置
,color:'rgba(255,0,255,0.8)' //剛才說的圖例顏色設置
},
emphasis:{label:{show:true}}
},
data:[
{name: '北京',value: Math.round(Math.random()*1000)},
{name: '天津',value: Math.round(Math.random()*1000)},
{name: '上海',value: Math.round(Math.random()*1000)}
]
}3、這么設置有問題嗎?我設置了一下發現有問題。圖例顏色是對了,但是地圖背景色不對,變成和第一個設置color的坐標系顏色一致了
于是查看地圖源碼(map.js)發現有這么一行代碼
color = dataRange && !isNaN(value) ? dataRange.getColor(value) : null; style.color = style.color || color || this.getItemStyleColor(this.deepQuery(queryTarget, 'itemStyle.normal.color'), data.seriesIndex, -1, data)|| this.deepQuery(queryTarget, 'itemStyle.normal.areaStyle.color');
如果按照地圖是china的話,這里的style可以理解成地圖省份,style.color沒值,color如果區間拉到最下面也是沒值(可以看到getColor方法返回的是null),然后接著找itemStyle.normal.color,所以兩個都設置了,是找不到areaStyle的設置。背景色就是第一個坐標系的顏色。
4、然后再想怎么解決。
看圖例的顏色設置機制,實際上和坐標系的什么圖形,什么類型都沒關系,只要是坐標系的格式就行。那是不是可以欺騙一下。
在series中,設置成這樣
{
name: 'iphone3',//add by danielinbiti,注意這里名稱必須和坐標系的名稱要一致
type:'', //設置為'',所有圖形都不會讀取
itemStyle:{
normal:{
color:'rgba(255,0,255,0.8)'
}
},
mapType:'none',
data:[]
},
{
name: 'iphone3',
type: 'map',
mapType: 'china',
selectedMode:'single',
roam: true,
showLegendSymbol:true,
itemStyle:{
normal:{
label:{show:true}
,areaStyle:{color:'green'}
},
emphasis:{label:{show:true}}
},
data:[
{name: '北京',value: Math.round(Math.random()*1000)},
{name: '天津',value: Math.round(Math.random()*1000)},
{name: '上海',value: Math.round(Math.random()*1000)}
]
}關于使用echarts怎么設置圖例顏色就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。