CSS Pie Chart
Lightweight data visualization with circular charts
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.
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:
name
: The name that identifies the slice. It’ll show up in the legend.amount
: The size of the slice. Can be an arbitrary number or percentage. Percentages will be automatically calculated.color
: The color of the slice.
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:
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:
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:
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:
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
.
showLegend
: Sets whether the legend should be shown.showLegendAmount
: Sets whether theamount
property should be shown in the legend.showLegendPercentage
: sets whether the calculated percentages should be shown in the legend.
API
Prop | Default | Purpose |
---|---|---|
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. |