This commit is contained in:
2026-02-02 20:36:54 +08:00
parent 21f4a0fa96
commit f4af5dded9
7 changed files with 727 additions and 74 deletions

View File

@@ -0,0 +1,115 @@
<template>
<view class="chart-wrap" :style="{ height: heightPx }">
<EChartsView :option="chartOption" class="chart" />
</view>
</template>
<script lang="uts">
import EChartsView from '@/uni_modules/charts/EChartsView.vue'
export default {
components: {
EChartsView
},
props: {
items: { type: Array, default: () => [] },
height: { type: Number, default: 300 }
},
data() {
return {
heightPx: '300px',
chartOption: {} as any
}
},
watch: {
items: { handler() { this.updateOption() }, deep: true },
height: {
handler() {
this.heightPx = `${this.height}px`
}
}
},
mounted() {
this.heightPx = `${this.height}px`
setTimeout(() => {
this.updateOption()
}, 200)
},
methods: {
toPlainObject(obj: any): any {
if (obj == null) return null
if (typeof obj !== 'object') return obj
if (Array.isArray(obj)) {
return obj.map((item) => this.toPlainObject(item))
}
const plain: any = {}
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key]
if (typeof value === 'function' || key.startsWith('_') || key === 'toJSON') {
continue
}
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
let isSimple = true
for (const k in value) {
if (typeof value[k] === 'object' && value[k] !== null) {
isSimple = false
break
}
}
plain[key] = isSimple ? { ...value } : this.toPlainObject(value)
} else {
plain[key] = value
}
}
}
return plain
},
updateOption() {
if (!this.items || this.items.length === 0) return
const data = (this.items as Array<any>).map((it) => {
return {
name: String(it.name),
value: Number(it.value),
itemStyle: this.toPlainObject(it.itemStyle)
}
})
const option = {
tooltip: { trigger: 'item', formatter: '{b}: {c} ({d}%)' },
legend: {
orient: 'vertical',
right: 10,
top: 'center',
itemWidth: 10,
itemHeight: 10,
textStyle: { fontSize: 11, color: '#666' }
},
series: [{
type: 'pie',
radius: ['45%', '70%'],
center: ['35%', '50%'],
data: data,
avoidLabelOverlap: true,
label: {
show: false
},
labelLine: { show: false },
itemStyle: {
borderWidth: 2,
borderColor: '#fff'
}
}]
}
this.chartOption = this.toPlainObject(option)
}
}
}
</script>
<style scoped>
.chart-wrap { width: 100%; position: relative; overflow: hidden; }
.chart { width: 100%; height: 100%; position: absolute; top: 0; left: 0; }
</style>