Introduction
PhpSpreadsheet allows you to create and embed various types of charts in Excel worksheets. Charts are visual representations of worksheet data that can be added programmatically.Chart support is primarily for Xlsx files. Chart rendering capabilities vary by writer format.
Chart Components
Every chart consists of these key components:- DataSeries: Defines the chart type and data
- DataSeriesValues: Specifies the data ranges
- PlotArea: Contains the data series
- Legend: Displays series labels
- Title: Chart and axis titles
- Chart: The main chart object
Creating a Basic Chart
Column Chart Example
Chart Types
Available Chart Types
PhpSpreadsheet supports these chart types:| Type | Constant |
|---|---|
| Area | DataSeries::TYPE_AREACHART |
| Bar | DataSeries::TYPE_BARCHART |
| Line | DataSeries::TYPE_LINECHART |
| Pie | DataSeries::TYPE_PIECHART |
| Doughnut | DataSeries::TYPE_DOUGHNUTCHART |
| Scatter | DataSeries::TYPE_SCATTERCHART |
| Bubble | DataSeries::TYPE_BUBBLECHART |
| Radar | DataSeries::TYPE_RADARCHART |
| Surface | DataSeries::TYPE_SURFACECHART |
| Stock | DataSeries::TYPE_STOCKCHART |
Bar Chart Example
Pie Chart Example
Scatter Chart Example
Chart Grouping
Grouping Types
DataSeries::GROUPING_STANDARD- Default groupingDataSeries::GROUPING_STACKED- StackedDataSeries::GROUPING_PERCENT_STACKED- 100% stackedDataSeries::GROUPING_CLUSTERED- Clustered
Stacked Bar Chart
Customizing Charts
Legend Position
Chart Titles
Empty Cell Handling
DataSeriesValues
Data Types
Creating DataSeriesValues
Chart Positioning
Setting Chart Position
Multiple Series
Creating a Chart with Multiple Series
Reading Charts
Reading Charts from Files
Chart Rendering
Rendering to Image
Charts can be rendered to images using chart renderers:Best Practices
Use Descriptive Chart Names
Use Descriptive Chart Names
Give charts meaningful names to easily identify them when reading files or debugging.
Validate Data Ranges
Validate Data Ranges
Ensure data ranges are correct and contain valid data before creating charts.
Position Charts Carefully
Position Charts Carefully
Position charts to avoid overlapping with data or other charts.
Choose Appropriate Chart Types
Choose Appropriate Chart Types
Select chart types that best represent your data (e.g., line charts for trends, pie charts for proportions).
Limitations
- Chart support is primarily for Xlsx format
- Not all Excel chart features are supported
- Chart rendering has limited functionality
- Some advanced chart types may not be fully supported

