Charts - Styling
This page groups topics about charts customization.
Colors
Series color
Series accepts a property color
which is the base color used to render its components.
<LineChart series={[{ ..., color: '#fdb462'}]} />
- Example
Color palette
Charts come with built-in color palettes to automatically assign colors to series. If a particular series lacks a color prop, the chart will default to assigning a color based on the series' index.
You can set a custom color palette by using the prop colors
on chart components (or <ChartContainer />
if you are using composition).
This prop takes an array of colors, or callback whose input is the theme's mode ('dark'
or 'light'
) and returns the array of colors.
Provided palettes
The library includes three palettes.
- Series 1
- Series 2
- Series 3
- Series 4
- Series 5
- Series 6
- Series 7
- Series 8
- Series 9
- Series 10
- Series 11
- Series 12
- Series 13
blueberryTwilight
Custom palettes
Those palettes can also be generated by using d3-scale-chromatic. Or any color manipulation library you like.
Here is an example of the d3 Categorical color palette.
- Series 1
- Series 2
- Series 3
- Series 4
- Series 5
- Series 6
- Series 7
- Series 8
- Series 9
- Series 10
- Series 11
- Series 12
- Series 13
Category10
Values color
Colors can also be set according to item values using the colorMap
property of the corresponding axis.
Learn more about how to use this feature with each chart component in their dedicated docs section:
The colorMap
property can accept three kinds of objects defined below.
Piecewise color map
The piecewise configuration takes an array of n thresholds
values and n+1 colors
.
{
type: 'piecewise';
thresholds: Value[];
colors: string[];
}
Continuous color map
The continuous configuration lets you map values from min
to max
properties to their corresponding colors.
The color
property can either be an array of two colors to interpolate, or an interpolation function that returns a color corresponding to a number t with a value between 0 and 1.
The d3-scale-chromatic offers a lot of those functions.
Values lower than the min
get the color of the min
value; similarly, values higher than the max
get the color of the max
value.
By default, the min
/max
range is set to 0 / 100.
{
type: 'continuous';
min?: Value;
max?: Value;
color: [string, string] | ((t: number) => string);
}
Ordinal color map
This configuration takes two properties—values
and colors
—and maps those values to their respective colors.
If a value is not defined, it will fall back to the unknownColor
, and if this is also undefined, then it falls back on the series color.
This configuration can be used in Bar Charts to set colors according to string categories.
{
type: 'ordinal';
values: Value[];
colors: string[];
unknownColor?: string;
}
Overlay
Charts have a loading and noData overlays that appear if:
loading
prop is set totrue
.- There is no data to display.
Axis display
You can provide the axes data to display them while loading the data.
Custom overlay
To modify the overlay message, you can use the message
props as follows:
<BarChart
slotProps={{
// Custom loading message
loadingOverlay: { message: 'Data should be available soon.' },
// Custom message for empty chart
noDataOverlay: { message: 'Select some data to display.' },
}}
/>
For more advanced customization, use the loadingOverlay
and noDataOverlay
slots link in the following demo.
Styling
Size
By default, charts adapt their sizing to fill their parent element.
However, you can modify this behavior by providing height
and/or width
props.
Those will fix the chart's size to the given value (in px).
Placement
There are two concepts to consider when defining the placement of a chart:
margin
: The space between the SVG border and the axis or drawing area.axis size
: The space taken by the axis. Each axis has its own size.
The axes have a default size.
To update it, use the xAxis
and yAxis
configuration as follows:
x-axis
: Uses theheight
prop to define the space taken by the axis.y-axis
: Uses thewidth
prop instead.
Axes only take up space in the side they are positioned.
If the axis is not be displayed (position: 'none'
), they will not take up any space, regardless of their size.
import { BarChart } from '@mui/x-charts/BarChart';
<BarChart
// ...
margin={{
left: 40,
right: 40,
top: 40,
bottom: 40,
}}
xAxis={[{
height: 30,
position: 'top'
}]}
yAxis={[{
width: 30
position: 'right'
}]}
/>
Playground
CSS
Since the library relies on SVG for rendering, you can customize them as you do with other MUI System components with CSS overriding.
Chart components accept the sx
props.
From here, you can target any subcomponents with its class name.
- l
- r
Gradients and patterns
It is possible to use gradients and patterns to fill the charts. This can be done by passing your gradient or pattern definition as children of the chart component.
Note that the gradient or pattern defined that way is only usable for SVG.
So a direct definition like color: "url(#Pattern)'
would cause undefined colors in HTML elements.
- series A
- series B