Dynamic data and asynchronous loading
🎯 Introduction
In real applications, data comes from APIs rather than hard-coded arrays and may refresh in real time. This article covers setOption updates, loading animations, asynchronous rendering, and resizing charts with the window.
🧱 How setOption merges updates
To update a chart, call setOption again instead of initializing it again. By default, it uses merge mode: new options merge with existing ones, updating only the changed parts with animated transitions:
// Initial rendering
chart.setOption({
xAxis: { type: 'category', data: ['Mon', 'Tue'] },
series: [{ type: 'bar', data: [120, 200] }],
});
// After the data changes, pass only the parts to update
chart.setOption({
series: [{ data: [150, 260] }],
});
true as the second argument to replace the configuration without merging:chart.setOption(option, true);
🧱 Loading animations
API requests take time. Showing a spinner while users wait improves the experience:
chart.showLoading(); // Show the loading animation
// Once the data arrives
chart.hideLoading(); // Hide the loading animation
chart.setOption({ series: [{ data: list }] });
🛠 Loading data from an API
Using axios, the flow is: show loading → request data → update the chart → hide loading. If axios is unfamiliar, review the Axios course on this site:
<template>
<div ref="chartRef" class="chart"></div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';
import axios from 'axios';
const chartRef = ref(null);
let chart = null;
const fetchData = async () => {
chart.showLoading();
const res = await axios.get('/api/sales/weekly');
const { days, values } = res.data;
chart.hideLoading();
chart.setOption({
xAxis: { data: days },
series: [{ data: values }],
});
};
onMounted(() => {
chart = echarts.init(chartRef.value);
// Render the chart structure first with empty data
chart.setOption({
title: { text: 'Weekly sales' },
tooltip: { trigger: 'axis' },
xAxis: { type: 'category', data: [] },
yAxis: { type: 'value' },
series: [{ name: 'Sales', type: 'bar', data: [] }],
});
fetchData();
});
</script>
<style scoped>
.chart {
width: 100%;
height: 400px;
}
</style>
The key pattern is to render the chart structure with empty data first, then populate it when the response arrives, so the page does not stay blank while waiting.
fetchData with setInterval. Remember to call clearInterval when the component unmounts.🧱 Resizing with the window
Charts do not automatically resize when the browser window changes. Listen for resize and resize the chart explicitly:
const handleResize = () => {
chart.resize();
};
onMounted(() => {
window.addEventListener('resize', handleResize);
});
onBeforeUnmount(() => {
window.removeEventListener('resize', handleResize);
});
Without this step, resizing the window may cause overflow or leave empty space.
❓ Questions and answers
Q1: Does setOption reinitialize the chart?
No. It updates the existing instance incrementally and efficiently, so frequent calls are fine.
Q2: What if the API response format differs from what ECharts needs?
Transform the response first, for example with map to extract days and values arrays, then pass the result to setOption.
Q3: Can I customize the loading animation?
Yes. showLoading accepts configuration for text, colors, and the overlay. The default appearance is sufficient while learning.
Q4: Why is the chart sized incorrectly in a dialog or drawer?
The container dimensions change when it becomes visible, but the chart has not recalculated its size. Call chart.resize() after showing the container.
🧪 Practice
Simulate an API that returns data after one second and implement the full asynchronous loading flow:
- Render an empty line chart first.
- Use
setTimeoutto return seven days of data after one second. - Show loading while waiting, then hide it and update the chart when the data arrives.
onMounted(() => {
const chart = echarts.init(chartRef.value);
// Write your code here
});
🎉 You can now connect charts to real data! Next, we will turn these patterns into a reusable Vue 3 component and reduce bundle size with selective imports.
