Spaces:
Running
Running
File size: 3,263 Bytes
dda1ac1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
<template>
<div ref="chartContainer" class="echart-mini"></div>
</template>
<script>
import * as echarts from 'echarts/core'
import { LineChart } from 'echarts/charts'
import { GridComponent, TooltipComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
import { nextTick } from 'vue'
echarts.use([LineChart, GridComponent, TooltipComponent, CanvasRenderer])
export default {
name: 'MiniEchart',
props: {
data: { type: Array, default: () => [] },
color: { type: String, default: '#22c55e' }
},
data() {
return {
chart: null,
resizeObserver: null
}
},
mounted() {
console.log('[MiniEchart] Mounted with data:', this.data?.length || 0, 'items')
nextTick(() => this.initChart())
},
beforeUnmount() {
if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
if (this.chart) {
this.chart.dispose()
this.chart = null
}
},
watch: {
data(newVal) {
if (this.chart && Array.isArray(newVal)) {
this.updateChart(newVal)
}
}
},
methods: {
initChart() {
const el = this.$refs.chartContainer
console.log('[MiniEchart] initChart - el:', el)
if (!el) {
console.log('[MiniEchart] No element found')
return
}
const { clientWidth: w, clientHeight: h } = el
console.log('[MiniEchart] Element size:', w, 'x', h)
if (!w || !h) {
console.log('[MiniEchart] Size is 0, will retry on next tick')
setTimeout(() => this.initChart(), 100)
return
}
console.log('[MiniEchart] Initializing ECharts...')
this.chart = echarts.init(el, null, { renderer: 'canvas' })
console.log('[MiniEchart] Chart initialized, rendering data:', this.data?.length || 0)
this.renderChart(this.data)
// Listen for container resize
this.resizeObserver = new ResizeObserver(() => {
if (this.chart) {
this.chart.resize()
}
})
this.resizeObserver.observe(el)
},
renderChart(arr) {
if (!this.chart) return
const d = Array.isArray(arr) ? arr : []
console.log('[MiniEchart] Rendering chart with', d.length, 'data points')
this.chart.setOption({
animation: true,
grid: { left: 0, right: 0, top: 0, bottom: 0 },
xAxis: {
type: 'category',
show: false,
boundaryGap: false,
data: d.map((_, i) => i)
},
yAxis: {
type: 'value',
show: false,
scale: true
},
tooltip: { show: false },
color: [this.color || '#22c55e'],
series: [{
type: 'line',
data: d,
showSymbol: false,
smooth: 0.35,
lineStyle: { width: 2 },
areaStyle: { opacity: 0.15 }
}]
})
},
updateChart(newData) {
if (!this.chart) return
const d = Array.isArray(newData) ? newData : []
this.chart.setOption({
xAxis: { data: d.map((_, i) => i) },
series: [{ data: d }]
})
}
}
}
</script>
<style scoped>
.echart-mini {
width: 100%;
height: 100%;
}
</style>
|