|
@@ -0,0 +1,132 @@
|
|
|
+<template>
|
|
|
+ <div class="chart-main" ref="ref_main">
|
|
|
+ <div class="chart-ref" ref="ref_chart"/>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang="ts">
|
|
|
+import {getCurrentInstance, reactive, ref, onMounted, watch, nextTick} from "vue";
|
|
|
+import * as echarts from 'echarts';
|
|
|
+
|
|
|
+const props = defineProps({
|
|
|
+ data: <any>{},
|
|
|
+ color: {},
|
|
|
+ unit: {}
|
|
|
+})
|
|
|
+
|
|
|
+const {proxy} = getCurrentInstance()
|
|
|
+const state = reactive({
|
|
|
+ resizeObserver: <any>null,
|
|
|
+ chart: <any>null
|
|
|
+})
|
|
|
+const ref_chart = ref();
|
|
|
+const ref_main = ref();
|
|
|
+const initChart = () => {
|
|
|
+ echarts.dispose(ref_chart.value)
|
|
|
+ const chart = echarts.init(ref_chart.value);
|
|
|
+ const option = {
|
|
|
+ grid: {
|
|
|
+ bottom: 20,
|
|
|
+ right: 20,
|
|
|
+ left: 40,
|
|
|
+ top: 20,
|
|
|
+ },
|
|
|
+ tooltip: {
|
|
|
+ trigger: 'axis',
|
|
|
+ formatter: (p) => {
|
|
|
+ let str = ''
|
|
|
+ str += p[0].name + '<br/>'
|
|
|
+ str += p[0].marker + '\r' + p[0].value + (props.unit || '')
|
|
|
+ return str
|
|
|
+ },
|
|
|
+ },
|
|
|
+ xAxis: {
|
|
|
+ type: 'category',
|
|
|
+ data: props.data.map(v => v.name),
|
|
|
+ axisLine: {show: false},
|
|
|
+ axisTick: {show: false},
|
|
|
+ splitLine: {show: false},
|
|
|
+ axisLabel: {
|
|
|
+ fontSize: 10,
|
|
|
+ interval: 0,
|
|
|
+ color: '#606266'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ yAxis: {
|
|
|
+ type: 'value',
|
|
|
+ axisLine: {show: false},
|
|
|
+ axisTick: {show: false},
|
|
|
+ splitNumber: 3,
|
|
|
+ splitLine: {
|
|
|
+ lineStyle: {
|
|
|
+ type: 'dashed',
|
|
|
+ color: 'rgba(239, 241, 248, 1)'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ axisLabel: {
|
|
|
+ fontSize: 10,
|
|
|
+ color: '#606266',
|
|
|
+ formatter: `{value}`
|
|
|
+ },
|
|
|
+ },
|
|
|
+ series: [
|
|
|
+ {
|
|
|
+ type: 'line',
|
|
|
+ data: props.data,
|
|
|
+ smooth: true,
|
|
|
+ showSymbol: false,
|
|
|
+ itemStyle: {
|
|
|
+ color: '#45B1FF'
|
|
|
+ },
|
|
|
+ // areaStyle: {
|
|
|
+ // color: {
|
|
|
+ // type: 'linear',
|
|
|
+ // x: 0,
|
|
|
+ // y: 1,
|
|
|
+ // x2: 0,
|
|
|
+ // y2: 0,
|
|
|
+ // colorStops: [{
|
|
|
+ // offset: 0, color: 'red' // 0% 处的颜色
|
|
|
+ // }, {
|
|
|
+ // offset: 1, color: 'blue' // 100% 处的颜色
|
|
|
+ // }],
|
|
|
+ // global: false // 缺省为 false
|
|
|
+ // },
|
|
|
+ // },
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ chart.setOption(option);
|
|
|
+ state.resizeObserver = new ResizeObserver((entries) => {
|
|
|
+ for (const entry of entries) {
|
|
|
+ chart && chart.resize()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ state.resizeObserver.observe(ref_main.value);
|
|
|
+ return chart
|
|
|
+}
|
|
|
+watch(() => props.data, () => {
|
|
|
+ state.chart = initChart()
|
|
|
+})
|
|
|
+onMounted(() => {
|
|
|
+ nextTick(() => {
|
|
|
+ state.chart = initChart()
|
|
|
+ })
|
|
|
+ return () => {
|
|
|
+ state.resizeObserver?.unobserve(ref_main?.value)
|
|
|
+ state.resizeObserver?.disconnect()
|
|
|
+ }
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+.chart-main {
|
|
|
+ flex: 1;
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+ .chart-ref {
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+ }
|
|
|
+}
|
|
|
+</style>
|