//组件
<script setup>
import { provide, ref, watch, onMounted } from 'vue';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { BarChart, LineChart, PieChart, RadarChart, TreeChart } from 'echarts/charts';
import {
TitleComponent,
ToolboxComponent,
TooltipComponent,
GridComponent,
LegendComponent,
} from 'echarts/components';
import { UniversalTransition } from 'echarts/features';
import VChart, { THEME_KEY } from 'vue-echarts';
const props = defineProps({
option: Object,
updateOptions: Object,
height: {
type: [Number, String],
default: 400,
},
width: {
type: [Number, String],
default: '',
},
loading: Boolean,
manualUpdate: {
type: Boolean,
default: false,
},
});
provide(THEME_KEY, 'light');
use([
CanvasRenderer,
PieChart,
LineChart,
BarChart,
RadarChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
TreeChart,
ToolboxComponent,
UniversalTransition,
]);
const vchart = ref(null);
const setOption = (option) => {
vchart.value.setOption({
...option,
series: [
{
data: option.series[0].data,
},
],
});
};
const getChartRef = () => {
return vchart.value;
};
watch(
() => props.option,
() => {
setOption(props.option);
},
);
defineExpose({
setOption,
getChartRef,
});
</script>
<template>
<VChart
ref="vchart"
:option="option"
:update-options="updateOptions"
:style="{ height: `${height}px`, width: width ? `${width}px` : '100%' }"
autoresize
:loading="loading"
:manual-update="manualUpdate"
/>
</template>
<style lang="scss" scoped></style>
//调用
const initHeight = () => {
let myChart = chartsRef.value.getChartRef().chart;
var container = document.getElementById('personnelStructure-wrap');
var allNode = 0;
var nodes = myChart._chartsViews[0]._data._graphicEls;
if (nodes.length < 20) return;
for (var i = 0, count = nodes.length; i < count; i++) {
var node = nodes[i];
if (node === undefined) continue;
allNode++;
}
var height = window.innerHeight;
var currentHeight = 35 * allNode;
var newWidth = Math.max(currentHeight, height);
// container.style.width = window.innerWidth + 'px';
container.style.height = newWidth + 'px';
myChart.resize();
};