echarts餅圖顯示設(shè)置效果及代碼示例
上圖

這是最終顯示效果
代碼
<template>
<div class="w-full h-full py-[10px] px-[20px] flex flex-col gap-[10px]">
<!-- 上 -->
<div class="flex flex-col flex-1 bg-white rounded-[6px] px-[18px] py-[20px] shadow-[0px_0px_8px_rgba(0,0,0,0.16)]">
<div class="flex items-center">
<img src="@/assets/images/layout/yunhe/dashboard_title.png" class="w-[4px] h-[16px] mr-[10px]" alt="" />
<h2 class="text-[18px] font-bold">單位匯總</h2>
</div>
<div class="flex-1 flex">
<!-- 左 -->
<div class="flex-1 flex flex-col items-center justify-center" style="border-right: 2px dashed #dfdfdf">
<div
class="flex items-end text-[20px] h-[51px] text-[#1e1e1e] w-[510px] bg-[url('@/assets/images/layout/yunhe/dashboard_top_bg.png')] bg-no-repeat bg-bottom bg-size-[510px_24px]"
>
<img
src="@/assets/images/layout/yunhe/dashboard_top_left.png"
class="w-[77px] h-[51px] ml-[67px] mr-[27px]"
alt=""
/>
<div class="mb-[5px]">
<span>頻繁停電用戶數(shù)</span>
<span class="font-bold ml-[31px] mr-[27px] text-[#08a279] text-[24px]">6000</span>
<span>戶</span>
</div>
</div>
<div ref="leftPieChartRef" class="flex-1 w-full flex items-center justify-center"></div>
</div>
<!-- 右 -->
<div class="flex-1 flex flex-col items-center justify-center">
<div
class="flex items-end text-[20px] h-[51px] text-[#1e1e1e] w-[510px] bg-[url('@/assets/images/layout/yunhe/dashboard_top_bg.png')] bg-no-repeat bg-bottom bg-size-[510px_24px]"
>
<img
src="@/assets/images/layout/yunhe/dashboard_top_right.png"
class="w-[77px] h-[51px] ml-[67px] mr-[27px]"
alt=""
/>
<div class="mb-[5px]">
<span>昨日發(fā)生停電戶數(shù)</span>
<span class="font-bold ml-[31px] mr-[27px] text-[#08a279] text-[24px]">6000</span>
<span>戶</span>
</div>
</div>
<div ref="rightPieChartRef" class="flex-1 w-full flex items-center justify-center">1</div>
</div>
</div>
</div>
</div>
<!-- -->
</div>
</template>
<script lang="ts" setup>
import { useEcharts, EchartsOption } from '@/hooks/useEcharts'
import { useEchartsOption } from '@/hooks/useEchartsOption'
import dashboardPieBg from '@/assets/images/layout/yunhe/dashboard_pie_bg.png'
import * as echarts from 'echarts'
const tableData = ref<any[]>([])
const chartData = ref<any[]>([])
const loading = ref(false)
const barChartRef = ref()
const leftPieChartRef = ref()
const rightPieChartRef = ref()
const getLeftPieOption = (): EchartsOption => {
return {
color: ['#08a279', '#ff6600', '#50b9ef'],
tooltip: {
trigger: 'item',
},
graphic: [
{
type: 'image',
left: 'center', //調(diào)整圖片位置
top: 'center', //調(diào)整圖片位置
z: -10, //確保圖片在餅圖下方
//設(shè)置圖片樣式
style: {
image: dashboardPieBg,
width: 280 * 1.3, // 圖片寬度
height: 226 * 1.3, // 圖片高度
},
},
],
series: [
{
type: 'pie',
radius: ['55%', '60%'],
center: ['50%', '50%'],
avoidLabelOverlap: false,
itemStyle: {
borderRadius: 50,
borderColor: '#fff',
borderWidth: 1,
},
label: {
show: true,
fontSize: 16,
color: '#1e1e1e',
verticalAlign: 'middle',
// 啟用富文本(關(guān)鍵配置)
rich: {
// 定義名稱樣式
text: {
fontSize: 14,
color: '#1e1e1e',
padding: [5, 5, 5, 0], // 上右下左內(nèi)邊距
align: 'left', // 富文本內(nèi)部左對(duì)齊
},
textRight: {
fontSize: 14,
color: '#1e1e1e',
padding: [5, 5, 5, 0], // 上右下左內(nèi)邊距
align: 'right', // 富文本內(nèi)部右對(duì)齊
},
// 2次
count2: {
fontSize: 17,
fontWeight: 'bold',
color: '#08a279',
align: 'left', // 統(tǒng)一左對(duì)齊
},
// 3-8次
count3to8: {
fontSize: 17,
fontWeight: 'bold',
color: '#ff6600',
align: 'left', // 補(bǔ)充左對(duì)齊,避免默認(rèn)居中
},
// 8次以上
count8More: {
fontSize: 17,
fontWeight: 'bold',
color: '#50b9ef',
align: 'left', // 補(bǔ)充左對(duì)齊
},
},
// 重構(gòu) formatter:移除無效空標(biāo)簽,簡(jiǎn)化邏輯
formatter: (params) => {
// 統(tǒng)一獲取樣式名,簡(jiǎn)化邏輯
let styleName = ''
if (params.name == '2次') styleName = 'count2'
else if (params.name == '3-8次') styleName = 'count3to8'
else if (params.name == '8次以上') styleName = 'count8More'
const content = `{${styleName}|${params.value}} {text|戶} {${styleName}|${params.percent}} {text|%}`
// 最終返回:名稱換行 + 數(shù)值內(nèi)容,無多余空標(biāo)簽
return `{${params.name == '2次' ? 'textRight' : 'text'}|${params.name}}\n${content}`
},
},
labelLine: {
show: true,
length: 20,
length2: 130,
// smooth: true,
lineStyle: {
color: '#dfdfdf',
width: 1.5,
},
},
labelLayout: function (params: any) {
const isLeft = params.labelRect.x < leftPieChartInstance.value.getWidth() / 2
const points = params.labelLinePoints
// Update the end point.
points[2][0] = isLeft ? params.labelRect.x : params.labelRect.x + params.labelRect.width
return {
labelLinePoints: points,
}
},
data: [
{ value: 3000, name: '2次' },
{ value: 2000, name: '3-8次' },
{ value: 1000, name: '8次以上' },
],
},
],
}
}
const { instance: leftPieChartInstance } = useEcharts(leftPieChartRef, getLeftPieOption())
const getList = () => {
loading.value = true
tableData.value = Array.from({ length: 40 }, (_, index) => ({
gdsName: `供電所${index + 1}`,
twoTimes: `${index * 100}`,
threeToEightTimes: `${index * 200}`,
eightTimesMore: `${index * 300}`,
threeTimesMoreRatio: `${index * 400}`,
}))
loading.value = false
}
const getChartData = () => {
chartData.value = [
{ value: 16, name: '一級(jí)預(yù)警' },
{ value: 27, name: '二級(jí)預(yù)警' },
{ value: 38, name: '三級(jí)預(yù)警' },
{ value: 22, name: '四級(jí)預(yù)警' },
]
barChartInstance.value?.setOption(getBarOption())
}
const getLeftPieChartData = () => {}
onMounted(() => {
getList()
getLeftPieChartData()
})
</script>
<style lang="scss" scoped></style>
記錄一下
總結(jié)
到此這篇關(guān)于echarts餅圖顯示設(shè)置效果及代碼示例的文章就介紹到這了,更多相關(guān)echarts餅圖顯示設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript高級(jí)程序設(shè)計(jì)閱讀筆記(五) ECMAScript中的運(yùn)算符(一)
ECMAScript中的運(yùn)算符,學(xué)習(xí)js的朋友可以看看2012-02-02
layui: layer.open加載窗體時(shí)出現(xiàn)遮罩層的解決方法
今天小編就為大家分享一篇layui: layer.open加載窗體時(shí)出現(xiàn)遮罩層的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
微信小程序?qū)崿F(xiàn)指定顯示行數(shù)多余文字去掉用省略號(hào)代替
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)指定顯示行數(shù)多余文字去掉用省略號(hào)代替的方法,需要的朋友可以參考下2018-07-07
js中的document.querySelector()方法舉例詳解
這篇文章主要給大家介紹了關(guān)于js中document.querySelector()方法的相關(guān)資料,document.querySelector是JavaScript中的一個(gè)內(nèi)置方法,用于通過CSS選擇器選擇文檔中的第一個(gè)匹配元素,需要的朋友可以參考下2024-01-01
javascript 解析后的xml對(duì)象的讀取方法細(xì)解
2009-07-07
mui開發(fā)中獲取單選按鈕、復(fù)選框的值(實(shí)例講解)
下面小編就為大家?guī)硪黄猰ui開發(fā)中獲取單選按鈕、復(fù)選框的值(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

