echarts餅圖labelLine線的長度自適應設置
基本思路:首先求出中心點的位置,然后判斷一下當前l(fā)abel的位置是左邊還是右邊,如果是左邊的話,中心點的位置 減去 label的寬度,如果是右邊的話 中心點的位置加上 label的寬度。 因為圖的大小不一樣 可根據(jù)實際情況添加 一個數(shù)值,我這邊添加的是50
labelLayout:標簽的統(tǒng)一布局配置。該配置項是在每個系列默認的標簽布局基礎上,統(tǒng)一調(diào)整標簽的(x, y)位置,標簽對齊等屬性以實現(xiàn)想要的標簽布局效果。
該配置項也可以是一個有如下參數(shù)的回調(diào)函數(shù)
// 標簽對應數(shù)據(jù)的 dataIndex dataIndex: number // 標簽對應的數(shù)據(jù)類型,只在關系圖中會有 node 和 edge 數(shù)據(jù)類型的區(qū)分 dataType?: string // 標簽對應的系列的 index seriesIndex: number // 標簽顯示的文本 text: string // 默認的標簽的包圍盒,由系列默認的標簽布局決定 labelRect: {x: number, y: number, width: number, height: number} // 默認的標簽水平對齊 align: 'left' | 'center' | 'right' // 默認的標簽垂直對齊 verticalAlign: 'top' | 'middle' | 'bottom' // 標簽所對應的數(shù)據(jù)圖形的包圍盒,可用于定位標簽位置 rect: {x: number, y: number, width: number, height: number} // 默認引導線的位置,目前只有餅圖(pie)和漏斗圖(funnel)有默認標簽位置 // 如果沒有該值則為 null labelLinePoints?: number[][]
代碼如下:
labelLayout: (params) => { const isLeft = params.labelRect.x < this.curEChartObj.getWidth() / 2 const centerX = this.curEChartObj.getWidth() / 2 let newX = 0 if (isLeft) { newX = centerX - 50 - params.labelRect.width } else { newX = centerX + 50 + params.labelRect.width } const points = params.labelLinePoints points[2][0] = isLeft ? newX + 10 : newX return { x: newX, labelLinePoints: points } },
完整代碼如下:
const option = { color: ['#94b6f3', '#5087ec', '#b9cff7'], legend: { show: false }, grid: { top: '1px', right: '1px', bottom: '1px', left: '2px' }, series: { type: 'pie', radius: ['45%', '60%'], center: ['50%', '50%'], itemStyle: { borderColor: '#fff', borderWidth: 1 }, label: { alignTo: 'edge', formatter: '{name|}\n{time|{c}人}', minMargin: 5, edgeDistance: 10, lineHeight: 15, width: 58, padding: [0, 6, 0, 10], // 關鍵代碼!關鍵代碼!關鍵代碼! rich: { name: { align: 'left' }, time: { fontSize: 12, color: '#333', align: 'left' } } }, labelLine: { length: 15, length2: 0, maxSurfaceAngle: 80 }, labelLayout: (params) => { const isLeft = params.labelRect.x < this.curEChartObj.getWidth() / 2 const centerX = this.curEChartObj.getWidth() / 2 let newX = 0 if (isLeft) { newX = centerX - 50 - params.labelRect.width } else { newX = centerX + 50 + params.labelRect.width } const points = params.labelLinePoints points[2][0] = isLeft ? newX + 10 : newX return { x: newX, labelLinePoints: points } }, data: pieData } }
總結(jié)
到此這篇關于echarts餅圖labelLine線的長度自適應設置的文章就介紹到這了,更多相關echarts labelLine線長度自適應內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript json字符串到json對象轉(zhuǎn)義問題
今天小編就為大家分享一篇關于javascript json字符串到json對象轉(zhuǎn)義問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01javascript對HTML字符轉(zhuǎn)義與反轉(zhuǎn)義
這篇文章主要介紹了javascript對HTML字符轉(zhuǎn)義與反轉(zhuǎn)義,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12JS forEach跳出循環(huán)2種實現(xiàn)方法
這篇文章主要介紹了JS forEach跳出循環(huán)2種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06將json當數(shù)據(jù)庫一樣操作的javascript lib
使用javascript操作JSON的類庫TAFFY DB,具體介紹了:查詢數(shù)據(jù)、添加數(shù)據(jù)、刪除數(shù)據(jù)、修改數(shù)據(jù)。2013-10-10Bootstrap導航條可點擊和鼠標懸停顯示下拉菜單的實現(xiàn)代碼
這篇文章主要介紹了Bootstrap導航條可點擊和鼠標懸停顯示下拉菜單的實現(xiàn)代碼的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06html中通過JS獲取JSON數(shù)據(jù)并加載的方法
本篇內(nèi)容主要給大家講了如何通過javascript解析JSON并得到數(shù)據(jù)后添加到HTML中的方法,需要的朋友參考下。2017-11-11