123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <template>
- <div class="hc-bar-chart-box">
- <div ref="echart" class="hc-bar-chart-echarts" :style="`width : ${chart?.clientWidth}px`"/>
- </div>
- </template>
- <script setup>
- import * as echarts from 'echarts'
- import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
- const props = defineProps({
- datas: {
- type: Array,
- default: () => ([])
- },
- })
- //初始变量
- let chart = null;
- const echart = ref(null)
- const datas = ref(props.datas)
- //监听
- watch(() => [
- props.datas
- ], ([data]) => {
- datas.value = data
- setOptions(data)
- })
- //初始化图表
- const initChart = () => {
- chart = echarts.init(echart.value)
- setOptions(props.datas)
- }
- //监听浏览器窗口变化
- const windowResize = () => {
- window.addEventListener("resize", resizeEvent);
- }
- const resizeEvent = () => {
- window.requestAnimationFrame(() => {
- chart.resize();
- })
- }
- //设置图表
- const setOptions = (AxisData) => {
- chart.setOption({
- tooltip: {
- trigger: 'item'
- },
- color: ['#5279AE','#F5CC71','#B8595A','#67B57E'],
- series: [
- {
- type: 'pie',
- radius: '80%',
- center: ["50%", "45%"],
- data: AxisData,
- emphasis: {
- itemStyle: {
- shadowBlur: 10,
- shadowOffsetX: 0,
- shadowColor: 'rgba(0, 0, 0, 0.4)'
- }
- },
- label:{
- formatter: '{b}\ {c}',
- }
- }
- ]
- })
- }
- //渲染完成
- onMounted(() => {
- nextTick(() => {
- initChart()
- windowResize()
- })
- })
- //被卸载
- onUnmounted(() => {
- window.removeEventListener("resize",resizeEvent);
- chart.dispose()
- chart = null
- })
- const onResize = () => {
- nextTick(() => {
- chart.resize();
- })
- }
- // 暴露出去
- defineExpose({
- onResize
- })
- </script>
- <style lang="scss" scoped>
- .hc-bar-chart-box {
- height: 100%;
- overflow: hidden;
- position: relative;
- .hc-bar-chart-echarts {
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- z-index: 2;
- width: 100%;
- height: 100%;
- }
- }
- </style>
|