官網文檔:https://echarts.apache.org/zh/option.html#series-pie.type
我的環形圖表效果圖:
配置:
1 //常用顏色列表 2 colors: string[] = ['#FF767C', '#FFA262', '#9FE11F', '#3ED389', '#71C8FF', '#9EA2FF']; 3 //獲取數據及渲染樣式 4 getOption = () => { 5 let assembledDatas = this.getAssembledDataList(); 6 let option = { 7 //數據加載時間為0,規避初始化時加載動畫偏移的問題 8 animationDurationUpdate: 0, 9 title: { 10 text: this.props.commentCount, 11 textStyle: { 12 color: '#333333', 13 fontFamily: 'Microsoft YaHei', 14 fontSize: 24, 15 lineHeight: 26, 16 fontWeight: 'normal', 17 }, 18 x: 'center', 19 y: 'center', 20 }, 21 //hover提示 22 tooltip: { 23 formatter: '{b}({d}%)', 24 }, 25 //標簽文本 26 label: { 27 color: '#666666', 28 fontFamily: 'Microsoft YaHei', 29 fontSize: 14, 30 lineHeight: 16, 31 formatter: '{b} {c}', 32 }, 33 //標簽線條 34 labelLine: { 35 lineStyle: { 36 color: '#666666', 37 }, 38 }, 39 //定義全局顏色盤 40 color: this.colors, 41 series: [ 42 { 43 name: '系列名', 44 type: 'pie', 45 center: ['50%', '50%'], // 餅圖的圓心坐標 46 radius: ['24%', '34%'], //內外圓圈 47 data: assembledDatas, 48 hoverOffset: 4, 49 }, 50 ], 51 }; 52 return option; 53 }
數據:
1 //獲取標簽數據列表(如果需要在指定data項中添加定制樣式,可以往這里加) 2 getAssembledDataList() { 3 let datas: any[] = []; 4 const sourceDatas = this.props.datas; 5 if (sourceDatas) { 6 for (let index = 0; index < sourceDatas.length; index++) { 7 const dataItem = sourceDatas[index]; 8 datas.push({ 9 value: dataItem.cupCount, 10 name: dataItem.typeName, 11 }); 12 } 13 } 14 return datas; 15 }
添加圖表:
1 render() { 2 return ( 3 <div className="pieChart"> 4 <ReactEcharts option={this.getOption()} /> 5 </div> 6 ); 7 }
一些引用:
1 import React, { Component } from 'react'; 2 //導入餅圖 3 import 'echarts/lib/chart/pie'; 4 import 'echarts/lib/component/tooltip'; 5 import 'echarts/lib/component/title'; 6 import 'echarts/lib/component/legend'; 7 import 'echarts/lib/component/markPoint'; 8 import ReactEcharts from 'echarts-for-react';
遇到的幾個問題:
1. 數據初始加載時,有一個更新動畫,但是是從顯示區域的左側彈出來的。
原因:默認是從左側出來的。暫時未找到直接設置初始動畫位置的屬性。如果直接關閉Animation=false,Hover動畫也會被禁用(即使設置hoverAnimation也加不回來)
規避:設置數據加載耗時為0,即數據加載時,不設置動畫。
2. 圖表有個最小高寬,如果小於這個最小高亮,圖表會加載不出來。
原因:他們的設計如此,說是需要一個加載的空間。
規避:可以等數據加載完成后,resize圖表。或者調整圖表的margin,隱藏冗余的空白部分。