echarts自定義legend樣式的詳細(xì)圖文教程
最近要完成顯示一個(gè)餅圖,使用echarts組件,先用官方給定的模板加載出樣式,然后修改為自定義的樣式。如下圖是要自定義legend。
先放上官方加載出的代碼
this.chart.setOption({ title: { text: "Filter request number distribution of different project", textStyle: { color: 'black', fontWeight: 'bold' } }, tooltip: { trigger: 'item' }, legend: { //對(duì)圖形的解釋部分 orient: 'vertical', right: 10, y: 'center' }, series: [ { name: 'Access From', type: 'pie', radius: ['55%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '20', fontWeight: 'bold' } }, labelLine: { show: false }, data: data // 需要加載的數(shù)據(jù) } ] })
對(duì)于需要加載的數(shù)據(jù)如下:
data: [ { value: 1048, name: 'Search Engine' }, { value: 735, name: 'Direct' }, { value: 580, name: 'Email' }, { value: 484, name: 'Union Ads' }, { value: 300, name: 'Video Ads' } ]
然后在此基礎(chǔ)上進(jìn)行修改。
首先可以看到,圖標(biāo)默認(rèn)是長方形,而需求是小圓點(diǎn)。
在此處設(shè)置就可以變?yōu)樾A點(diǎn)
如果需要其它圖標(biāo),可以參看下圖
接著就是右邊一段文字到三段文字的顯示,不止要展示出name,還要展示出百分比和數(shù)量。
這個(gè)就要用到legend.formatter
進(jìn)行設(shè)置,還要用到legend.textStyle. rich
,在 rich 里面,可以自定義富文本樣式,使三列文字的中間那一列展示為灰色,兩邊文字為黑色。
具體官網(wǎng)樣式設(shè)置教程:https://echarts.apache.org/zh/option.html#legend.formatter
具體分析過程如下:
首先把文字分為3段,a表示name,b表示百分比, c表示value數(shù)量。
然后在textStyle里設(shè)置各自的樣式,設(shè)置后的代碼如下,注意備注【添加】的地方是主要更改
this.chart.setOption({ title: { text: 'Filter request number distribution of different project', textStyle: { color: 'black', fontWeight: 'bold' } }, tooltip: { trigger: 'item' }, legend: { // 對(duì)圖形的解釋部分 orient: 'vertical', right: 10, y: 'center', icon: 'circle', // 添加 formatter: function(name) { // 添加 let total = 0 let target for (let i = 0; i < data.length; i++) { total += data[i].value if (data[i].name === name) { target = data[i].value } } var arr = [ '{a|' + name + '}', '{b|' + ((target / total) * 100).toFixed(2) + '%}', '{c|' + target + '}' ] return arr.join(' ') }, textStyle: { // 添加 padding: [8, 0, 0, 0], rich: { a: { fontSize: 15, width: 110 }, b: { fontSize: 15, width: 70, color: '#c1c1c1' }, c: { fontSize: 15 } } } }, series: [ { name: 'Access From', type: 'pie', radius: ['55%', '70%'], center: ['30%', '50%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '20', fontWeight: 'bold' } }, labelLine: { show: false }, data: data } ] })
最后加載出的樣式如圖
大功告成!
總結(jié)
到此這篇關(guān)于echarts自定義legend樣式的文章就介紹到這了,更多相關(guān)echarts自定義legend樣式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解JavaScript中的強(qiáng)制類型轉(zhuǎn)換
這篇文章主要介紹了JavaScript中的強(qiáng)制類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Kendo Grid editing 自定義驗(yàn)證報(bào)錯(cuò)提示的解決方法
Kendo UI是一個(gè)強(qiáng)大的框架用于快速HTML5 UI開發(fā)?;谧钚碌腍TML5、CSS3和JavaScript標(biāo)準(zhǔn)。今天小編通過分享本文給大家介紹Kendo Grid editing 自定義驗(yàn)證報(bào)錯(cuò)提示的解決方法2016-11-11在JavaScript中構(gòu)建ArrayList示例代碼
這篇文章主要介紹了在JavaScript中構(gòu)建ArrayList,很實(shí)用,需要的朋友可以參考下2014-09-09