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

在这里插入图片描述

<!--
 * @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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue2 使用 ECharts 实现 x 轴 y 轴气泡图,你需要先安装 ECharts。 可以通过 npm 或 yarn 安装 ECharts: ```bash npm install echarts --save # 或者 yarn add echarts ``` 安装完成后,在 Vue 组件引入 ECharts: ```javascript import echarts from 'echarts' ``` 然后在组件的 mounted 钩子函数初始化 ECharts 实例: ```javascript mounted () { // 初始化 ECharts 实例 const chart = echarts.init(this.$refs.chart) // 具体的 ECharts 配置 const option = { // 配置项 } // 使用刚指定的配置项和数据显示图表 chart.setOption(option) } ``` 对于气泡图,可以使用 scatter 和 visualMap 组件实现。以下是一个简单的示例: ```javascript mounted () { // 初始化 ECharts 实例 const chart = echarts.init(this.$refs.chart) // ECharts 配置 const option = { xAxis: { type: 'value', scale: true }, yAxis: { type: 'value', scale: true }, visualMap: { min: 0, max: 100, dimension: 2, calculable: true, orient: 'horizontal', left: 'center', bottom: '15%' }, series: [{ type: 'scatter', data: [ [10.0, 8.04, 10], [8.0, 6.95, 30], [13.0, 7.58, 20], [9.0, 8.81, 50], [11.0, 8.33, 80], [14.0, 9.96, 40], [6.0, 7.24, 70], [4.0, 4.26, 90], [12.0, 10.84, 60], [7.0, 4.82, 100], [5.0, 5.68, 10] ], symbolSize: function (data) { return Math.sqrt(data[2]) }, label: { emphasis: { show: true, formatter: function (param) { return param.data[2] }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(120, 36, 50, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(251, 118, 123)' }, { offset: 1, color: 'rgb(204, 46, 72)' }]) } } }] } // 使用刚指定的配置项和数据显示图表 chart.setOption(option) } ``` 在模板添加一个 div 占位符,并设置 ref 属性为 chart: ```html <template> <div ref="chart" style="width: 100%; height: 400px;"></div> </template> ``` 这样就可以在 Vue2 使用 ECharts 实现 x 轴 y 轴气泡图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值