Introduction
OpenXLSX allows you to programmatically generate and insert a wide variety of native Excel charts directly into your worksheets or as standalone chartsheets.
These charts are fully dynamic—meaning they are linked to cell ranges in your workbook and will automatically update in Excel if the underlying data changes.
This tutorial covers:
- Creating a basic Column or Bar chart.
- Binding data series (values, labels, and names) to cell ranges.
- Creating advanced chart types (Line, Scatter, Pie, Area).
- Advanced financial and scientific charts (Stock OHLC, 3D Surface).
- Generating dedicated Chartsheet pages.
1. Preparing the Data
Before adding a chart, you need data in your worksheet. A chart series typically consists of:
- Values: The numerical data points to plot.
- Categories (X-Axis): The labels for each data point (e.g., Dates, Names).
- Series Name: The legend name for the data.
#include <OpenXLSX.hpp>
int main() {
wks.cell("A1").value() = "Month";
wks.cell("B1").value() = "Sales";
wks.cell("C1").value() = "Profit";
wks.cell("A2").value() = "Jan"; wks.cell("B2").value() = 150; wks.cell("C2").value() = 45;
wks.cell("A3").value() = "Feb"; wks.cell("B3").value() = 180; wks.cell("C3").value() = 60;
wks.cell("A4").value() = "Mar"; wks.cell("B4").value() = 220; wks.cell("C4").value() = 90;
wks.cell("A5").value() = "Apr"; wks.cell("B5").value() = 280; wks.cell("C5").value() = 120;
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
void create(std::string_view fileName, bool forceOverwrite=XLForceOverwrite)
Initialize a new .xlsx package from a built-in template.
Definition XLDocument.cpp:372
XLWorkbook workbook() const
Get the underlying workbook object, containing worksheets and global data state. [[nodiscard]] preven...
Definition XLDocument.cpp:828
void setName(const std::string &sheetName)
Method for renaming the sheet.
Definition XLSheetBase.hpp:212
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
Definition IXLCellProvider.hpp:24
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
2. Adding a Basic Chart
You can insert a chart into a worksheet using wks.addChart(). You must specify:
- XLChartType (e.g., Column, Bar, Line)
- Title of the chart
- Row and Column anchor (top-left position of the chart object)
- Width and Height in pixels
colChart.addSeries("Data!$B$2:$B$5", "Sales", "Data!$A$2:$A$5");
colChart.addSeries("Data!$C$2:$C$5", "Profit", "Data!$A$2:$A$5");
@ Column
Definition XLChart.hpp:31
3. Common Chart Types
OpenXLSX supports dozens of chart types via the XLChartType enumeration.
Line and Area Charts
Great for showing trends over time.
lineChart.addSeries("Data!$C$2:$C$5", "Profit", "Data!$A$2:$A$5");
areaChart.addSeries("Data!$B$2:$B$5", "Sales", "Data!$A$2:$A$5");
@ Line
Definition XLChart.hpp:37
@ Area
Definition XLChart.hpp:56
Pie and Doughnut Charts
Used for showing distributions or proportions.
auto pieChart = wks.addChart(
XLChartType::Pie,
"Sales Distribution", 34, 5, 450, 300);
pieChart.addSeries("Data!$B$2:$B$5", "Sales", "Data!$A$2:$A$5");
@ Pie
Definition XLChart.hpp:41
Stacked Charts
Used to show parts of a whole over time. Types include BarStacked, ColumnStacked, and BarPercentStacked (100% Stacked).
stackedChart.addSeries("Data!$B$2:$B$5", "Sales", "Data!$A$2:$A$5");
stackedChart.addSeries("Data!$C$2:$C$5", "Profit", "Data!$A$2:$A$5");
@ BarStacked
Definition XLChart.hpp:26
4. Advanced Chart Types
Scatter Charts (XY)
Scatter charts map numerical X values to numerical Y values, rather than using categorical text labels. Use ScatterSmoothMarker for curved lines with dots, or ScatterLine for straight lines.
scatterChart.addSeries("Data!$C$2:$C$5", "Profit vs Sales", "Data!$B$2:$B$5");
@ ScatterSmoothMarker
Scatter with smooth lines and markers.
Definition XLChart.hpp:47
Bubble Charts
Bubble charts are a variation of scatter charts where the size of each marker represents a third dimension of data. Because bubble charts require three data ranges (X values, Y values, and Bubble Sizes), you must use the specialized addBubbleSeries() method.
bubbleChart.addBubbleSeries("Data!$A$2:$A$5", "Data!$B$2:$B$5", "Data!$C$2:$C$5", "Market Share");
@ Bubble
Bubble chart (needs addBubbleSeries).
Definition XLChart.hpp:49
Stock (OHLC) Financial Charts
Stock charts require data to be added in a very specific order to represent Open, High, Low, and Close values.
stockChart.addSeries("Data!$G$2:$G$6", "Open", "Data!$F$2:$F$6");
stockChart.addSeries("Data!$H$2:$H$6", "High", "Data!$F$2:$F$6");
stockChart.addSeries("Data!$I$2:$I$6", "Low", "Data!$F$2:$F$6");
stockChart.addSeries("Data!$J$2:$J$6", "Close", "Data!$F$2:$F$6");
@ StockOHLC
Definition XLChart.hpp:51
3D Surface Charts
Surface charts are useful for finding optimum combinations between two sets of data. As in a topographic map, colors and patterns indicate areas that are in the same range of values. Surface charts require three-dimensional data (X, Y, and Z):
- Series Names (Y-axis / Depth): Represent the different column categories.
- Categories (X-axis / Width): Represent the row labels.
- Values (Z-axis / Height): The actual data points plotted on the surface.
surfChart.addSeries("Data!$B$2:$B$4", "Data!$B$1", "Data!$A$2:$A$4");
surfChart.addSeries("Data!$C$2:$C$4", "Data!$C$1", "Data!$A$2:$A$4");
surfChart.addSeries("Data!$D$2:$D$4", "Data!$D$1", "Data!$A$2:$A$4");
@ Surface3D
Definition XLChart.hpp:53
5. Dedicated Chartsheets
If you want a chart to occupy an entire tab by itself without any worksheet grid lines, you can create a Chartsheet.
csChart.
addSeries(
"Data!$B$2:$B$5",
"Sales",
"Data!$A$2:$A$5");
csChart.addSeries("Data!$C$2:$C$5", "Profit", "Data!$A$2:$A$5");
return 0;
}
XLChartSeries addSeries(const XLWorksheet &wks, const XLCellRange &values, std::string_view title="", std::optional< XLChartType > targetChartType=std::nullopt, bool useSecondaryAxis=false)
Add a data series to the chart.
Definition XLChart.cpp:711
XLChart addChart(XLChartType type, std::string_view name)
Add or replace the chart in this chartsheet.
Definition XLChartsheet.cpp:91
void close()
Close the document and release underlying ZIP resources, ensuring file handles are closed properly.
Definition XLDocument.cpp:412
void save()
Save changes to the original file path.
Definition XLDocument.cpp:460
XLChartsheet chartsheet(std::string_view sheetName)
Get the chartsheet with the given name.
Definition XLWorkbook.cpp:207
void addChartsheet(std::string_view sheetName)
Definition XLWorkbook.cpp:272
@ Column3D
Definition XLChart.hpp:34
This generates a professional workbook where the data drives multiple visualizations across embedded worksheets and full-page chart tabs.