echarts設(shè)置圖例顏色和地圖底色的方法實(shí)例
前言
本來想寫echarts初始化函數(shù)的,但最近因?yàn)橐獙懸粋€(gè)地圖與柱狀圖的混合方式,也就是每個(gè)省的地圖上要有柱狀圖顯示。于是仔細(xì)使用了一下地圖。
1、地圖的一些基本屬性就不介紹了,還是那些style
2、地圖數(shù)據(jù)的獲取以及Series的加載和其他沒有什么大的差異。地圖數(shù)據(jù)都在map.js中,都可以自己看,也可以自己根據(jù)格式獲取響應(yīng)的數(shù)據(jù)。
這里主要想處理的是圖例顏色與地圖底圖顏色怎么設(shè)置的問題。
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是否設(shè)置了itemStyle.normal.color這個(gè)屬性進(jìn)行判斷,如果沒有,則會(huì)按照默認(rèn)的顏色設(shè)置取值。如果設(shè)置了,就按照設(shè)置的顏色取值?,F(xiàn)在想設(shè)置,肯定需要在series中對(duì)應(yīng)的坐標(biāo)系中設(shè)置顏色。
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、于是可能產(chǎn)生了如下一個(gè)坐標(biāo)系設(shè)置代碼
{
name: 'iphone3',
type: 'map',
mapType: 'china',
selectedMode:'single',
roam: true,
showLegendSymbol:true,
itemStyle:{
normal:{
label:{show:true}
,areaStyle:{color:'green'} //設(shè)置地圖背景色的顏色設(shè)置
,color:'rgba(255,0,255,0.8)' //剛才說的圖例顏色設(shè)置
},
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、這么設(shè)置有問題嗎?我設(shè)置了一下發(fā)現(xiàn)有問題。圖例顏色是對(duì)了,但是地圖背景色不對(duì),變成和第一個(gè)設(shè)置color的坐標(biāo)系顏色一致了
于是查看地圖源碼(map.js)發(fā)現(xiàn)有這么一行代碼
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如果區(qū)間拉到最下面也是沒值(可以看到getColor方法返回的是null),然后接著找itemStyle.normal.color,所以兩個(gè)都設(shè)置了,是找不到areaStyle的設(shè)置。背景色就是第一個(gè)坐標(biāo)系的顏色。
4、然后再想怎么解決。
看圖例的顏色設(shè)置機(jī)制,實(shí)際上和坐標(biāo)系的什么圖形,什么類型都沒關(guān)系,只要是坐標(biāo)系的格式就行。那是不是可以欺騙一下。
在series中,設(shè)置成這樣
{
name: 'iphone3',//add by danielinbiti,注意這里名稱必須和坐標(biāo)系的名稱要一致
type:'', //設(shè)置為'',所有圖形都不會(huì)讀取
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)}
]
}
總結(jié):
或許沒有發(fā)現(xiàn)其他隱形設(shè)置,但根據(jù)map中的代碼,似乎也沒有其他途徑。希望echarts能夠修正一下這個(gè)問題。把or的時(shí)候順序換一下就行了。舉手之勞。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
微信小程序?qū)崿F(xiàn)隨機(jī)驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)隨機(jī)驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
純JS實(shí)現(xiàn)表單驗(yàn)證實(shí)例
這篇文章主要介紹了純JS實(shí)現(xiàn)表單驗(yàn)證實(shí)例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
小程序?qū)崿F(xiàn)五星點(diǎn)評(píng)效果
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)五星點(diǎn)評(píng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
js實(shí)現(xiàn)ifram取父窗口URL地址的方法
這篇文章主要介紹了js實(shí)現(xiàn)ifram取父窗口URL地址的方法,是javascript操作window.parent對(duì)象非常典型的應(yīng)用技巧,需要的朋友可以參考下2015-02-02
uniapp H5 https跨域請(qǐng)求實(shí)現(xiàn)
這篇文章主要介紹了uniapp H5 https跨域請(qǐng)求實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
把json格式的字符串轉(zhuǎn)換成javascript對(duì)象或數(shù)組的方法總結(jié)
下面小編就為大家?guī)硪黄裫son格式的字符串轉(zhuǎn)換成javascript對(duì)象或數(shù)組的方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11

