Search documentation v0.9.0

CSS Pie Chart

Lightweight data visualization with circular charts

Source ↗️ Report Issue ↗️

Use the CSSPieChart component to create responsive and lightweight circular charts to visually represent data proportions. Use them to visualize data like percentages, progress, or distribution, or to illustrate statistics in dashboards and reports. They’re created using CSS which makes them a dependency-free, lightweight solution compared to external libraries.

Preview
Astro (53.75%) Svelte (16.25%) React (30%)
Unlock code
How to use the CSSPieChart component in Astro
---
const pieChartData = [
{
name: 'Astro',
amount: 43,
color: '#4e00c2'
},
{ ... },
{ ... }
]
---
<CSSPieChart data={pieChartData} />

The component only expects a data prop that defines each slice in the pie using an array of objects. Each object can have the following properties:

The chart always takes the full size of its container. If you need to change the size of the chart, specify a width for your container.

Rotation

To customize the visual appearance of the pie, you can rotate it by a number of degrees using the pieRotation prop:

Default
Astro (53.75%) Svelte (16.25%) React (30%)
Rotated by 90°
Astro (53.75%) Svelte (16.25%) React (30%)
Rotated by 120°
Astro (53.75%) Svelte (16.25%) React (30%)
Unlock code
How to customize pie rotation
<CSSPieChart data={pieChartData} pieRotation={90} />
<CSSPieChart data={pieChartData} pieRotation={120} />

Donuts

The component can also be used to create donut charts. Simply set the donut prop to true to turn the pie chart into a donut chart:

Astro (53.75%) Svelte (16.25%) React (30%)
Unlock code
How to turn the pie chart into a donut chart
<CSSPieChart data={pieChartData} donut={true} />

By default, the center of the donut will take the default background color (var(--w-color-primary-70)). If you need to change this, you can specify a custom color using the donutCenterColor prop:

Astro (53.75%) Svelte (16.25%) React (30%)
Unlock code
How to customize donut center color
<CSSPieChart
data={pieChartData}
donut={true}
donutCenterColor="var(--w-color-primary-60)"
/>

You can also customize the size of the donut using the donutSize prop. This is set to 50 by default. The smaller the number, the smaller the hole becomes:

Default (50)
Astro (53.75%) Svelte (16.25%) React (30%)
Set to 40
Astro (53.75%) Svelte (16.25%) React (30%)
Set to 30
Astro (53.75%) Svelte (16.25%) React (30%)
Unlock code
How to customize the donut size
<CSSPieChart data={pieChartData} donut={true} donutSize={40} />
<CSSPieChart data={pieChartData} donut={true} donutSize={30} />

Legends

When using the component, the chart will automatically generates a legend under the chart with the percentages. This can be turned off with the showLegend prop. Percentages can also be disabled separately using the showLegendPercentage prop, or it can be replace with the amount property specified in your data prop using the showLegendAmount.

Without percentage
Astro Svelte React
With amount
Astro (43) Svelte (13) React (24)
With percentage and amount
Astro (53.75%) (43) Svelte (16.25%) (13) React (30%) (24)
Unlock code
How to customize the donut size
<CSSPieChart data={pieChartData} showLegendPercentage={false} />
Unlock code
How to customize the donut size
<CSSPieChart
data={pieChartData}
showLegendAmount={true}
showLegendPercentage={false}
/>
Unlock code
How to customize the donut size
<CSSPieChart data={pieChartData} showLegendAmount={true} />

API

Unlock code
Component API reference
type CSSPieChartProps = {
data: {
name: string
amount: number
color: string
}[]
pieRotation?: number
donut?: boolean
donutSize?: number
donutCenterColor?: string
showLegend?: boolean
showLegendAmount?: boolean
showLegendPercentage?: boolean
className?: string
}
PropDefaultPurpose
data [] Sets the slices for the pie chart.
data.name - Sets the name of the slice, shown in the legend.
data.amount - Sets the size of the slice.
data.color - Sets the color of the slice.
pieRotation 0 Sets the rotation of the pie.
donut false Turns the pie chart into a donut chart.
donutSize 50 Sets the size of the hole inside the donut.
donutCenterColor var(--w-color-primary-70) Sets the center color of the donut.
showLegend true Sets whether to show the legend.
showLegendAmount false Sets whether to show the amount property inside the legend.
showLegendPercentage true Sets whether to show slice percentages inside the legend.
className - Pass additional CSS classes for the chart.