Core configuration options explained
🎯 Introduction
In the previous article, we rendered a bar chart with a configuration object without examining each field. Here, you will learn the overall structure of option and its five core parts: title, tooltip, legend, axes, and series. This will make configuration references much easier to follow.
🧱 What is option?
The object passed to setOption() is called option. It is the chart's blueprint, describing its appearance, data, and interactions.
Think of it as a restaurant order:
title: the dish name (chart title)legend: the list of accompaniments (legend)xAxis/yAxis: the horizontal and vertical layout guides (axes)series: the ingredients (data and chart types)tooltip: the server's explanation (hover information)
🧱 Titles and tooltips
title controls the chart title. The main title and subtitle are commonly used:
title: {
text: 'Weekly sales',
subtext: 'Source: backend analytics',
left: 'center',
},
tooltip displays information on hover, making the chart interactive:
tooltip: {
trigger: 'axis',
},
There are two common trigger values. Be sure to distinguish them:
'item': triggers when hovering over an individual graphic element, suitable for pie and scatter charts.'axis': triggers when hovering at an axis position, showing data for all series at that position, suitable for line and bar charts.
🧱 The legend option
When a chart has multiple series, such as “Sales” and “Profit”, legend tells readers what each color represents:
legend: {
data: ['Sales', 'Profit'],
},
legend.data must exactly match the corresponding series name for the legend to display and toggle that series correctly. Beginners often overlook this relationship.🧱 The xAxis and yAxis options
Cartesian charts, such as line and bar charts, need two axes:
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
},
yAxis: {
type: 'value',
},
Two common type values are:
'category': a category axis showing discrete category names, supplied indata.'value': a value axis with ticks calculated automatically from series data, without adataarray.
xAxis to 'value' and yAxis to 'category', moving the category data to the other axis. The axis arrangement determines the bar direction.🧱 Series: the data at the heart of the chart
series is an array in which each entry represents a dataset + a chart type:
series: [
{
name: 'Sales',
type: 'bar',
data: [120, 200, 150, 80, 70],
},
{
name: 'Profit',
type: 'line',
data: [20, 50, 30, 10, 15],
},
],
An option object can contain multiple series. The code above creates a mixed chart combining bars and a line.
💡 Putting the example together
Combine the five core options:
chart.setOption({
title: {
text: 'Weekly sales and profit',
},
tooltip: {
trigger: 'axis',
},
legend: {
data: ['Sales', 'Profit'],
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
},
yAxis: {
type: 'value',
},
series: [
{
name: 'Sales',
type: 'bar',
data: [120, 200, 150, 80, 70],
},
{
name: 'Profit',
type: 'line',
data: [20, 50, 30, 10, 15],
},
],
});
❓ Questions and answers
Q1: Why is the legend missing?
First, check that legend.data exactly matches the series name values, including capitalization and every character.
Q2: What happens if I omit tooltip?
The chart still renders, but hovering will not show values. Tooltips are generally recommended in real applications.
Q3: Why does yAxis have no data?
ECharts calculates value-axis ticks from the series data automatically; manually supplying data would constrain that behavior. Only category axes need a manually supplied data array.
Q4: How many different type values can series contain?
There is no fixed limit. A common combination uses bars for totals and a line for trends.
🧪 Practice
Complete the configuration below to compare scores in three subjects:
- Show Chinese, Mathematics, and English on
xAxis. - Use a bar series for Xiaoming's scores: 80, 90, 85.
- Use another bar series for Xiaohong's scores: 85, 88, 92.
- Add a title, legend, and axis-triggered tooltip.
chart.setOption({
// Write your code here
});
🎉 You can now read and write a complete option object! Next, we will explore common chart types to expand your charting toolkit.
