| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <template>
- <div class="item-distribution">
- <div class="chart-container" ref="itemChart"></div>
- <div class="legend-grid">
- <div v-for="(item, index) in chartsData.items" :key="index" class="legend-item">
- <div class="legend-dot" :style="{ backgroundColor: item.color }"></div>
- <div class="legend-text">{{ item.name }}: {{ item.value }}</div>
- </div>
- </div>
- </div>
- </template>
- <script>
- import * as echarts from 'echarts'
- export default {
- name: 'ItemDistribution',
- props: {
- chartsData: {
- type: Object,
- default: () => ({
- items: [
-
- ]
- })
- }
- },
- data() {
- return {
- chart: null
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.initChart()
- })
- },
- watch: {
- chartsData: {
- deep: true,
- handler() {
- this.$nextTick(() => {
- if (this.chart) this.chart.dispose()
- this.initChart()
- })
- }
- }
- },
- methods: {
- initChart() {
- if (!this.$refs.itemChart) return
- this.chart = echarts.init(this.$refs.itemChart)
- const option = {
- responsive: true,
- maintainAspectRatio: false,
- series: [{
- type: 'pie',
- radius: ['50%', '70%'],
- data: this.chartsData.items.map(item => ({
- name: item.name,
- value: item.value,
- color: item.color
- })),
- itemStyle: {
- color: function(params) {
- return params.data.color || params.color
- }
- },
- label: {
- show: true,
- color: 'rgba(255, 255, 255, 0.7)',
- fontSize: 10,
- formatter: '{b}\n{d}%'
- }
- }]
- }
- this.chart.setOption(option)
- }
- },
- beforeDestroy() {
- if (this.chart) this.chart.dispose()
- }
- }
- </script>
- <style lang="scss" scoped>
- .item-distribution {
- .chart-container {
- height: 300rpx;
- }
-
- .legend-grid {
- display: grid;
- grid-template-columns: repeat(2, 1fr);
- gap: 8rpx;
- margin-top: 16rpx;
- }
-
- .legend-item {
- display: flex;
- align-items: center;
- font-size: 24rpx;
-
- .legend-dot {
- width: 12rpx;
- height: 12rpx;
- border-radius: 50%;
- margin-right: 8rpx;
- }
-
- .legend-text {
- color: rgba(255, 255, 255, 0.7);
- }
- }
- }
- </style>
|