apache / echarts

Apache ECharts is a powerful, interactive charting and data visualization library for browser
https://echarts.apache.org
Apache License 2.0
60.27k stars 19.61k forks source link

[Feature] When the options of a stock chart changed in Vue 3, the chart is broken! #18706

Open suncg-sgla opened 1 year ago

suncg-sgla commented 1 year ago

What problem does this feature solve?

The option of a stock chart changed but the chart can not update accordingly but show like this:

2023-06-02_16-24

After refreshing the webpage the chart shows correctly like this:

2023-06-02_16-24_1 The chart.vue code is:

<template>
  <div ref="chartEl" class="chart" :id="chartID"></div>
</template>

<script setup>
import { onMounted, ref, watch } from "vue";
import * as echarts from "echarts";

const chartEl = ref();
const { chartID, options } = defineProps({
  chartID: {
    type: String,
    default: "chart",
  },
  options: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

function chartInit() {
  const chart = echarts.init(chartEl.value);
  chart.setOption(options, true);
  window.addEventListener("resize", () => {
    chart.resize();
  });
}

onMounted(() => {
  chartInit();
});

watch(
  options,
  (newVal, preVal) => {
    console.log(newVal);
    chartInit();
  },
  {
    deep: true,
  }
);
</script>

<style lang="less" scoped>
.chart {
  flex: 1;
  height: 500px;
  text-align: center;
  border: 1px solid #aaa;
  margin: 0 10px;
}
</style>

What does the proposed API look like?

How can let the chart automatically update correctly?

helgasoft commented 1 year ago

That is a Vue debugging question.