echarts
echarts copied to clipboard
[Pie] How to Get percent values from echart instance
First of all, Thank you for creating this library. it is awesome!
Version & Environment [版本及环境]
- ECharts version [ECharts 版本]: 4.0.4
- Browser version [浏览器类型和版本]: Chrome
- OS Version [操作系统类型和版本]: Window 10
One-line summary [问题简述]
How to get "percent' value from echart instance with a data name
jsFiddle
http://jsfiddle.net/coolbean/2wkd4bab/8/
Description
From the jsFiddle above, there are 3 data ('aaa', 'bbb', 'ccc' with different values) you can see that when you hover over each section of the Pie chart, you are able to view both its value and its percent. This is done via "formatter" fields of the echart's tooltip.
I am wondering if there is way to access this same "percent" value directly from the echart instance.
var myChart = echarts.init(document.getElementById('main'));
var option = { ... } // please see option below
myChart.setOption(option);
// This is what I want - accessing params of a data directly from echart instance
myChart.getData('aaa').getPercent()
ECharts option [ECharts配置项]
option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {
show: true,
formatter: function(params) {
let res = "";
res += "Value : " + params.value.toLocaleString() + '</br>';
res += "Percent : " + params.percent + '</br>';
return res;
}
},
series: [{
name: '销量',
type: 'pie',
data: [{
name: "aaa",
value: 74900
},
{
name: "bbb",
value: 112350
},
{
name: "ccc",
value: 187250
}
]
}]
};
Solution Solved by calculating percent from data's value and store percent field in it to make sure it is the single point of truth.