Overview
TheChart class represents a visual chart that can be embedded in a worksheet. Charts transform worksheet data into visual representations like bar charts, line charts, pie charts, and more.
Namespace: PhpOffice\PhpSpreadsheet\Chart
Creating a Chart
A chart requires several components:- DataSeriesValues: Define the data labels, categories, and values
- DataSeries: Configure the chart type, grouping, and data
- PlotArea: Container for the data series
- Legend: Optional legend for the chart
- Title: Chart title and axis labels
Basic Example
Constructor
Unique name identifier for the chart
Chart title object
Chart legend configuration
Plot area containing data series
Whether to plot only visible cells
How to handle blank cells:
gap, zero, or spanX-axis label
Y-axis label
X-axis configuration object
Y-axis configuration object
Key Methods
getName()
Get the chart name.setName()
Set the chart name.getTitle() / setTitle()
Get or set the chart title.getLegend() / setLegend()
Get or set the chart legend.getPlotArea() / setPlotArea()
Get or set the plot area.setTopLeftPosition()
Set the top-left anchor position for the chart.Cell address (e.g., ‘A7’)
Horizontal offset in pixels
Vertical offset in pixels
setBottomRightPosition()
Set the bottom-right anchor position for the chart.getTopLeftPosition()
Get the top-left position.cell, xOffset, yOffset
getBottomRightPosition()
Get the bottom-right position.cell, xOffset, yOffset
render()
Render the chart to a file or stream.File path to save the rendered chart, or null for default output
refresh()
Refresh chart data from the worksheet.getChartAxisX() / setChartAxisX()
Get or set the X-axis configuration.getChartAxisY() / setChartAxisY()
Get or set the Y-axis configuration.Chart Types
The chart type is defined in theDataSeries object. Available types include:
DataSeries::TYPE_BARCHART- Bar/Column chartDataSeries::TYPE_LINECHART- Line chartDataSeries::TYPE_AREACHART- Area chartDataSeries::TYPE_PIECHART- Pie chartDataSeries::TYPE_DOUGHNUTCHART- Doughnut chartDataSeries::TYPE_SCATTERCHART- Scatter chartDataSeries::TYPE_RADARCHART- Radar chartDataSeries::TYPE_BUBBLECHART- Bubble chartDataSeries::TYPE_STOCKCHART- Stock/Candle chartDataSeries::TYPE_SURFACECHART- Surface chart
3D Charts
For 3D chart rendering, use methods to control perspective:setRotX() / getRotX()
Set/get X-axis rotation (0-360 degrees).setRotY() / getRotY()
Set/get Y-axis rotation (0-360 degrees).setPerspective() / getPerspective()
Set/get perspective value (0-100).Styling Methods
setRoundedCorners()
Enable/disable rounded corners for the chart border.setNoFill() / getNoFill()
Set/get whether the chart has no fill.setNoBorder() / getNoBorder()
Set/get whether the chart has no border.getFillColor()
Get the chart fill color object.getBorderLines() / setBorderLines()
Get or set chart border line styling.Multiple Charts Example
Related Classes
- DataSeries - Define chart data and type
- DataSeriesValues - Data values, labels, and categories
- Title - Chart and axis titles
- Legend - Chart legend configuration
- PlotArea - Container for data series
- Axis - Axis configuration

