首页 前端知识 vue3中使用echarts,动态更新X、Y轴数据

vue3中使用echarts,动态更新X、Y轴数据

2024-08-15 22:08:42 前端知识 前端哥 935 165 我要收藏

在这里插入图片描述

<!--
 * @Description: 
 * @Version: 1.0
 * @Author: 
 * @Date: 2022-09-15 14:56:15
-->
<template>
  <div :id="chartRef" :style="{ width: '100%', height: '100%' }"></div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, watch, computed, toRefs } from "vue";
import * as echarts from "echarts";
import { uuid } from "vue-uuid";
const props = defineProps({
  xaxisData: {
    type: Array,
  },
  yaxisData: {
    type: Array,
  },
});
const chartRef = ref<any>(1);
chartRef.value = uuid.v1().replace(/-/g, "");
const setOptions = computed(() => {
  let option = {
    tooltip: {
      trigger: "axis",
    },
    yAxis: {
      type: "value",
    },
    xAxis: {
      type: "category",
      data: props.xaxisData,
    },
    series: props.yaxisData,
  };
  return option;
});
var BarChart: echarts.ECharts;
function initChart() {
  BarChart.dispose();
  BarChart = echarts.init(
    document.getElementById(chartRef.value) as HTMLElement
  );
  BarChart.setOption(setOptions.value);
  document.getElementById(chartRef.value).onresize = function () {
    BarChart.resize();
  };
}
onMounted(() => {
  BarChart = echarts.init(
    document.getElementById(chartRef.value) as HTMLElement
  );
  initChart();
});
watch([props], () => {
  initChart();
});
</script>

<style scoped></style>
转载请注明出处或者链接地址:https://www.qianduange.cn//article/15747.html
标签
评论
发布的文章

HTML5学习记录

2024-04-29 12:04:01

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!