js echarts使用百分比顯示數據 echarts使用配置
這個是最終效果,其實這個還是比較簡單的,貼一下代碼,然后把最主要的兩個地方說一下:
let dom = document.getElementById("main"); let myChart = echarts.init(dom); let option = { title: { text: '折線圖堆疊' }, tooltip: { trigger: 'axis', formatter: '{b0}<br/>{a0}: {c0}%<br />{a1}: {c1}%<br />{a2}: {c2}%<br />{a3}: {c3}%' }, legend: { data:['1PD0','2PD0','3PD0','4PD0'] }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'category', boundaryGap: false, data: ['Jan-18','Feb-18','Mar-18','Apr-18','May-18','Jun-18','Jul-18','Aug-18'] }, yAxis: { type: 'value', axisLabel: { show: true, interval: 'auto', formatter: '{value}%' }, show: true }, series: [ { name:'1PD0', type:'line', data:[37.39,33.33,44.29,39.27,54.01,68.16,78.33], }, { name:'2PD0', type:'line', data:[30.43,29.41,52.86,69.86,72.09,50], }, { name:'3PD0', type:'line', data:[26.09,42.86,60.94,71.63,69.41], }, { name:'4PD0', type:'line', data:[30.43,40.82,56.25,76.92,58.97], } ] }; myChart.setOption(option, true);
主要代碼,大部分都是echart官網上扒的,其中tooltip的formatter是設置那個划過提示框的顯示內容的,formatter有幾項配置,給大家從官網粘一下方便看:
模板變量有 {a}
, {b}
,{c}
,{d}
,{e}
,分別表示系列名,數據名,數據值等。 在 trigger 為 'axis'
的時候,會有多個系列的數據,此時可以通過 {a0}
, {a1}
, {a2}
這種后面加索引的方式表示系列的索引。 不同圖表類型下的 {a}
,{b}
,{c}
,{d}
含義不一樣。 其中變量{a}
, {b}
, {c}
, {d}
在不同圖表類型下代表數據含義為:
-
折線(區域)圖、柱狀(條形)圖、K線圖 :
{a}
(系列名稱),{b}
(類目值),{c}
(數值),{d}
(無) -
散點圖(氣泡)圖 :
{a}
(系列名稱),{b}
(數據名稱),{c}
(數值數組),{d}
(無) -
地圖 :
{a}
(系列名稱),{b}
(區域名稱),{c}
(合並數值),{d}
(無) -
餅圖、儀表盤、漏斗圖:
{a}
(系列名稱),{b}
(數據項名稱),{c}
(數值),{d}
(百分比)
更多其它圖表模板變量的含義可以見相應的圖表的 label.formatter 配置項。
示例:
formatter: '{b0}: {c0}<br />{b1}: {c1}'
相信看到這里就知道我寫的是啥意思了,這個是設置顯示的時候的百分比,還有設置左面label的百分比提示:
yAxis: { type: 'value', axisLabel: { show: true, interval: 'auto', formatter: '{value}%' }, show: true } 就是這段,
解釋還是拷貝官網:
[ default: null ]
刻度標簽的內容格式器,支持字符串模板和回調函數兩種形式。
示例:
// 使用字符串模板,模板變量為刻度默認標簽 {value}
formatter: '{value} kg'
// 使用函數模板,函數參數分別為刻度數值(類目),刻度的索引
formatter: function (value, index) {
// 格式化成月/日,只在第一個刻度顯示年份
var date = new Date(value);
var texts = [(date.getMonth() + 1), date.getDate()];
if (index === 0) {
texts.unshift(date.getYear());
}
return texts.join('/');
}
常用的主要是:formatter: '{value} kg'
配置詳見:http://echarts.baidu.com/option.html#yAxis.axisLabel.formatter