Custom styles and themes
🎯 Introduction
Your charts render correctly, but you may want something beyond the default colors. This article covers palettes, gradients, and labels, along with the built-in dark theme and custom themes for consistent styling across your site.
🧱 The color palette
ECharts assigns built-in colors to series by default. ECharts 6 introduced a new default theme with a more modern palette. Override the top-level color array to change these colors; series use them in order:
chart.setOption({
color: ['#5470c6', '#91cc75', '#fac858'],
// ...
});
color array as your brand palette. Using the same colors across charts gives the site a consistent appearance.✨ Gradients and line styles
Combine itemStyle with LinearGradient to add gradients, a common technique for bar charts:
series: [
{
name: 'Sales',
type: 'bar',
data: [120, 200, 150, 80, 70],
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#a5b4fc' },
{ offset: 1, color: '#6366f1' },
]),
},
},
],
The first four LinearGradient arguments define its direction, (x1, y1, x2, y2). The values 0, 0, 0, 1 create a top-to-bottom gradient.
Here are the rendered gradient bars:
That configuration styles bars. A line chart consists of a line, data points, and a filled area, each controlled by a separate style property:
lineStyle: the line color and width.itemStyle: the color of the circular data-point symbols.areaStyle: the fill color below the line, with gradient support.
series: [
{
name: 'Visits',
type: 'line',
data: [820, 932, 901, 934, 1290, 1330, 1320],
lineStyle: {
color: '#5470c6',
width: 3,
},
itemStyle: {
color: '#5470c6',
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'rgba(84, 112, 198, 0.4)' },
{ offset: 1, color: 'rgba(84, 112, 198, 0)' },
]),
},
},
],
Here is a live chart combining a styled line, data points, and a gradient area:
itemStyle has different roles in these charts: it styles entire bars in a bar chart, but only the data-point symbols in a line chart.🧱 Data labels with label
Use label to display values directly on chart elements:
series: [
{
name: 'Sales',
type: 'bar',
data: [120, 200, 150, 80, 70],
label: {
show: true,
position: 'top',
},
},
],
show: truedisplays labels.positioncontrols placement. Common values are'top'for bars and'outside'for pies.
Here are values displayed above each bar:
🧱 The built-in dark theme
ECharts includes a dark theme. Pass 'dark' when initializing the chart:
const chart = echarts.init(chartRef.value, 'dark');
🧰 Custom themes with registerTheme
If built-in themes do not meet your branding needs, register your own:
import * as echarts from 'echarts';
echarts.registerTheme('myTheme', {
color: ['#2ec7c9', '#b6a2de', '#5ab1ef'],
backgroundColor: '#ffffff',
textStyle: {
fontFamily: 'PingFang SC, sans-serif',
},
});
Use the theme name during initialization:
const chart = echarts.init(chartRef.value, 'myTheme');
A theme object can define the palette, background color, text styles, and more. These settings apply consistently to all charts initialized with that theme.
❓ Questions and answers
Q1: Why does setting color in a series override the theme color?
This is expected: series-level settings take precedence over theme and palette colors.
Q2: What format does LinearGradient use for its colors?
An array of { offset, color } entries. Each offset ranges from 0 to 1 and specifies the color's position along the gradient.
Q3: Can I change themes after initialization?
Traditionally, you dispose of the instance and call init again with the new theme. ECharts 6 improves theme support for smoother switching; consult the official handbook for details.
Q4: What if labels are too crowded?
Enable label only for key series, or adjust position and fontSize. For bar charts, you can also adjust barWidth to create more space.
🧪 Practice
Style the “Weekly sales” bar chart from the previous article:
- Add a top-to-bottom gradient with
LinearGradient, using colors of your choice. - Display values above the bars with
label. - Register a theme named
'brand'with three colors you like, then use it to initialize the chart.
// Write your code here
🎉 You can now customize chart appearance! Next, we will add event handling, data zoom, and annotations.
