addChart

addChart

BASIC / ADVANCED / PREMIUM

Inserts a chart into the Excel spreadsheet.

Description
public addChart($chart, $position, $options)

This method allows the insertion of a chart into the Excel spreadsheet.

This method also allows adding trendlines, grids, labels and many other chart features.

Chart size is set using rowSize and colSize options. If these values are not set, the chart size is set to 6 columns and 10 rows.

Parameters

chart

Chart type:

  • area
  • area3D
  • bar
  • bar3D, bar3DCone, bar3DCylinder, bar3DPyramid
  • bubble
  • col
  • col3D, col3DCone, col3DCylinder, col3DPyramid
  • doughnut
  • line
  • line3D
  • ofPie
  • pie
  • pie3D
  • radar
  • scatter
  • surface

position

Cell position in the active sheet: A1, C3, AB7...

options

Key Type Description
data array This array contains legends, labels and values of the chart. Individual cell positions and cell range positions can be used.
border int Border width in points.
color int Color scheme.
colOffset array
  • 'from' (int) column size offset from.
  • 'to' (int) column size offset to.
colSize int Number of columns used by the chart.
font string The font to be used. Arial, Calibri...
rowOffset array
  • 'from' (int) row size offset from.
  • 'to' (int) row size offset to.
rowSize int Number of rows used by the chart.
title string The chart title (if any).

Chart display properties

Key Type Description
axPos array Position of the axis (r, l, t, b). Each value of the array for each position (if a value if null avoids adding it).
formatCode string Number format.
formatDataLabels array Possible keys and values are:
  • rotation => (int).
  • position => (string) center, insideEnd, insideBase, outsideEnd.
haxLabel string Label for the horizontal axis.
haxLabelDisplay string How to display the label of the horizontal axis. Possible values are: vertical, horizontal, rotated.
hgrid int Horizontal grid. Possible values are:
  • 0 (no grid)
  • 1 (only major grid lines - default)
  • 2 (only minor grid lines)
  • 3 (both major and minor grid lines)
majorUnit float Major unit value for bar, col and line charts.
minorUnit float Minor unit value for bar, col and line charts.
orientation array Orientation of the axis, from min to max (minMax) or max to min (maxMin). Each value of the array for each axis (if a value if null avoids adding it).
scalingMax float Scaling max value for bar, col and line charts.
scalingMin float Scaling max value for for bar, col and line charts.
stylesTitle array Possible keys and values are:
  • bold => (bool).
  • color => (string) ffffff, ff0000....
  • font => (string) Arial, Times New Roman....
  • fontSize => (int) 1420, 2000... Size as drawing content (10 to 400000). 1420 as default..
  • italic => (bool).
tickLblPos string Tick label position (nextTo, high, low, none). If string, uses default values. If array, sets a value for each position.
vaxLabel string Label for the vertical axis.
vaxLabelDisplay string How to display the label of the horizontal axis. Possible values are: vertical, horizontal, rotated.
vgrid int Vertical grid. Possible values are:
  • 0 (no grid)
  • 1 (only major grid lines - default)
  • 2 (only minor grid lines)
  • 3 (both major and minor grid lines)

The available parameters to display legends and data include:

Key Type Description
legendOverlay bool If true the legend may overlay the chart.
legendPos string The possible values are:
  • b (bottom)
  • l (left)
  • r (right)
  • t (top)
  • none
showCategory bool If true shows the categories inside the chart.
showLegendKey bool If true shows the legend values.
showPercent bool If true shows the percentage values.
showSeries bool If true shows the series values.
showTable bool If true shows the chart data in an associated table.
showValue bool If true shows the chart data values.
trendline array Adds trendlines. Compatible with line, bar, col and area 2D charts. Possible keys and values are:
  • 'color' => (string) 0000FF.
  • 'display_equation' => (bool) display equation on chart.
  • 'display_rSquared' => (bool) display R-squared value on chart.
  • 'intercept' => (float) set intercept.
  • 'line_style' => (string) solid, dot, dash, lgDash, dashDot, lgDashDot, lgDashDotDot, sysDash, sysDot, sysDashDot, sysDashDotDot.
  • 'type' => (string) exp, linear, log, poly, power, movingAvg.
  • 'type_order' => (int) for poly and movingAvg types.

Further configuration options for specific chart types are:

Key Type Description
Bar and column charts
gapWidth int Gap width.
groupBar string Different ways to group data. Possible values are: clustered, stacked, percentStacked and standard.
overlap int Overlap.
Pie and doughnut charts
explosion int Distance between the different values.
holeSize int Size of the inner hole (only doughnut charts).
Line charts
smooth mixed If true it smooths the line. '0' forces disabling it
symbol string The possible symbol values are:
  • Line charts: none, dot, plus, square, star, triangle, x, diamond, circle and dash.
symbolSize int The size of the symbol used. From 1 to 73.

Additional available options for 3D charts:

Key Type Description
perspective int Perspective angle. Angles beyond 25 degrees provoke a big distortion in the 3D chart representation.
rotX int Rotation angle with respect to the vertical axis. Standard values are between 10 and 20 degrees.
rotY int Rotation angle with respect to the horizontal axis. Standard values are between 10 and 20 degrees.

Theme options (Available in Premium licenses):

Key Type Description
chartArea array backgroundColor
gridLines array capType, color, dashType, width
horizontalAxis array textBold, textDirection (horizontal, rotate90, rotate270), textItalic, textSize, textUnderline (DrawingML values such as none, sng, dash)
legendArea array backgroundColor, textBold, textItalic, textSize, textUnderline
plotArea array backgroundColor
serDataLabels array data labels options
serRgbColors array series colors
valueRgbColors array values colors
verticalAxis array textBold, textDirection (horizontal, rotate90, rotate270), textItalic, textSize, textUnderline (DrawingML values such as none, sng, dash)
Exceptions

Data array is not set.

Chart type is not supported.

Code samples

Example #1

The resulting XLSX looks like:

Example #2

The resulting XLSX looks like:

Example #3

The resulting XLSX looks like:

Example #4

The resulting XLSX looks like:

Example #5

The resulting XLSX looks like:

Release notes
  • phpxlsx 4.0:
    • ofPie chart type.
    • apply custom colors to lines.
  • phpxlsx 3.5:
    • bubble and scatter chart types.
  • phpxlsx 3.0:
    • radar and surface chart types.
  • phpxlsx 2.0:
    • bar3DCylinder, bar3DCone, bar3DPyramid, col3DCylinder, col3DCone, col3DPyramid, area and area3D chart types.
  • phpxlsx 1.0:
    • new method.