echarts自定義legend樣式的詳細圖文教程
最近要完成顯示一個餅圖,使用echarts組件,先用官方給定的模板加載出樣式,然后修改為自定義的樣式。如下圖是要自定義legend。

先放上官方加載出的代碼
this.chart.setOption({
title: {
text: "Filter request number distribution of different project",
textStyle: {
color: 'black',
fontWeight: 'bold'
}
},
tooltip: {
trigger: 'item'
},
legend: { //對圖形的解釋部分
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ù)
}
]
})
對于需要加載的數(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' }
]
然后在此基礎上進行修改。
首先可以看到,圖標默認是長方形,而需求是小圓點。

在此處設置就可以變?yōu)樾A點

如果需要其它圖標,可以參看下圖

接著就是右邊一段文字到三段文字的顯示,不止要展示出name,還要展示出百分比和數(shù)量。
這個就要用到legend.formatter進行設置,還要用到legend.textStyle. rich,在 rich 里面,可以自定義富文本樣式,使三列文字的中間那一列展示為灰色,兩邊文字為黑色。
具體官網(wǎng)樣式設置教程:https://echarts.apache.org/zh/option.html#legend.formatter
具體分析過程如下:
首先把文字分為3段,a表示name,b表示百分比, c表示value數(shù)量。
然后在textStyle里設置各自的樣式,設置后的代碼如下,注意備注【添加】的地方是主要更改
this.chart.setOption({
title: {
text: 'Filter request number distribution of different project',
textStyle: {
color: 'black',
fontWeight: 'bold'
}
},
tooltip: {
trigger: 'item'
},
legend: { // 對圖形的解釋部分
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
}
]
})
最后加載出的樣式如圖

大功告成!
總結
到此這篇關于echarts自定義legend樣式的文章就介紹到這了,更多相關echarts自定義legend樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Kendo Grid editing 自定義驗證報錯提示的解決方法
Kendo UI是一個強大的框架用于快速HTML5 UI開發(fā)?;谧钚碌腍TML5、CSS3和JavaScript標準。今天小編通過分享本文給大家介紹Kendo Grid editing 自定義驗證報錯提示的解決方法2016-11-11

