OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLChart.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLCHART_HPP
12#define OPENXLSX_XLCHART_HPP
13
14#include "OpenXLSX-Exports.hpp"
15#include "XLConstants.hpp"
16#include "XLXmlFile.hpp"
17
18#include <optional>
19#include <string>
20#include <string_view>
21
22namespace OpenXLSX
23{
69
71
73
87
94
98
105 class XLWorksheet;
106 class XLCellRange;
107
108 enum class XLErrorBarDirection { X, Y };
109
110 enum class XLErrorBarType { Both, Minus, Plus };
111
113
115
117
118 enum class XLAxisCrosses { AutoZero, Min, Max };
119
120 class OPENXLSX_EXPORT XLChartSeries
121 {
122 public:
123 XLChartSeries() = default;
124 explicit XLChartSeries(const XMLNode& node);
125
126 XLChartSeries& setTitle(std::string_view title);
127 XLChartSeries& setSmooth(bool smooth);
129
134 XLChartSeries& setColor(std::string_view hexRGB);
135
141 XLChartSeries& setDataPointColor(uint32_t pointIdx, std::string_view hexRGB);
142
149 XLChartSeries& setDataLabels(bool showValue, bool showCategoryName = false, bool showPercent = false);
150
157
161 XLChartSeries& addTrendline(XLTrendlineType type, std::string_view name = "", uint8_t order = 2, uint8_t period = 2);
162
166 XLChartSeries& addErrorBars(XLErrorBarDirection direction, XLErrorBarType type, XLErrorBarValueType valType, double value = 0.0);
167
168 XLChartSeries& setLineWidth(double points);
170
171 private:
172 XMLNode m_node;
173 };
174
176 {
177 std::string name;
178 uint32_t row{1};
179 uint32_t col{1};
182
183 XLChartAnchor() = default;
184 XLChartAnchor(std::string_view n, uint32_t r, uint32_t c, XLDistance w, XLDistance h) : name(n), row(r), col(c), width(w), height(h)
185 {}
186 };
187
188 class OPENXLSX_EXPORT XLAxis
189 {
190 public:
191 XLAxis() = default;
192 explicit XLAxis(const XMLNode& node);
193
194 void setTitle(std::string_view title);
195
196 void setMinBounds(double min);
197 void clearMinBounds();
198
199 void setMaxBounds(double max);
200 void clearMaxBounds();
201
206 void setMajorUnit(double unit);
207
212 void setMinorUnit(double unit);
213
218 void setLogScale(double base);
219
224 void setDateAxis(bool isDateAxis);
225
230 void setOrientation(XLAxisOrientation orientation);
231
236 void setCrosses(XLAxisCrosses crosses);
237
242 void setCrossesAt(double value);
243
244 void setMajorGridlines(bool show);
245 void setMinorGridlines(bool show);
246
252 void setNumberFormat(std::string_view formatCode, bool sourceLinked = false);
253
255
256 private:
257 XMLNode m_node;
258 };
259
260 class OPENXLSX_EXPORT XLChart final : public XLXmlFile
261 {
262 public:
266 XLChart() : XLXmlFile(nullptr) {}
267
272 explicit XLChart(XLXmlData* xmlData);
273
278 XLChart(const XLChart& other) = default;
279
283 XLChart(XLChart&& other) noexcept = default;
284
288 ~XLChart() = default;
289
293 XLChart& operator=(const XLChart& other) = default;
294
298 XLChart& operator=(XLChart&& other) noexcept = default;
299
307 const XLCellRange& values,
308 std::string_view title = "",
309 std::optional<XLChartType> targetChartType = std::nullopt,
310 bool useSecondaryAxis = false);
312 const XLCellRange& values,
313 const XLCellRange& categories,
314 std::string_view title = "",
315 std::optional<XLChartType> targetChartType = std::nullopt,
316 bool useSecondaryAxis = false);
317 XLChartSeries addSeries(std::string_view valuesRef,
318 std::string_view title = "",
319 std::string_view categoriesRef = "",
320 std::optional<XLChartType> targetChartType = std::nullopt,
321 bool useSecondaryAxis = false);
322
331 addBubbleSeries(std::string_view xValRef, std::string_view yValRef, std::string_view sizeRef, std::string_view title = "");
332
334 const XLCellRange& xValues,
335 const XLCellRange& yValues,
336 const XLCellRange& sizes,
337 std::string_view title = "");
338
342 void setTitle(std::string_view title);
343
347 void setStyle(uint8_t styleId);
348
352 void setLegendPosition(XLLegendPosition position);
353
357 [[nodiscard]] XLAxis xAxis() const;
358
362 [[nodiscard]] XLAxis yAxis() const;
363
364 [[nodiscard]] XLAxis axis(std::string_view position) const;
365
369 void setShowDataLabels(bool showValue, bool showCategory = false, bool showPercent = false);
370
374 void setSeriesSmooth(uint32_t seriesIndex, bool smooth);
375
379 void setSeriesMarker(uint32_t seriesIndex, XLMarkerStyle style);
380
385 void setGapWidth(uint32_t percent);
386
391 void setOverlap(int32_t percent);
392
397 void setHoleSize(uint8_t percent);
398
405 void setRotation(uint16_t x, uint16_t y, uint16_t perspective = 30);
406
411 void setPlotAreaColor(std::string_view hexRGB);
412
417 void setChartAreaColor(std::string_view hexRGB);
418
419 void setShowDataTable(bool showTable, bool showKeys = false);
420
421 private:
422 friend class XLDocument;
423 void initXml(XLChartType type = XLChartType::Bar);
424 [[nodiscard]] uint32_t seriesCount() const;
425 };
426} // namespace OpenXLSX
427
428#endif // OPENXLSX_XLCHART_HPP
Definition XLChart.hpp:189
void setLogScale(double base)
Set the axis to use a logarithmic scale.
Definition XLChart.cpp:288
void clearMaxBounds()
Definition XLChart.cpp:268
void setMinorGridlines(bool show)
Definition XLChart.cpp:368
void setCrossesAt(double value)
Set the axis to cross at a specific value.
Definition XLChart.cpp:349
void setOrientation(XLAxisOrientation orientation)
Set the orientation of the axis (normal or reverse).
Definition XLChart.cpp:318
void setMinBounds(double min)
Definition XLChart.cpp:244
void setMaxBounds(double max)
Definition XLChart.cpp:259
void setDateAxis(bool isDateAxis)
Convert this axis to a date axis or back to a category axis.
Definition XLChart.cpp:303
void clearMinBounds()
Definition XLChart.cpp:253
void setMajorUnit(double unit)
Set the major unit (interval between major tick marks) for the axis.
Definition XLChart.cpp:274
void setTickLabelPosition(XLAxisTickLabelPosition position)
Definition XLChart.cpp:1115
void setTitle(std::string_view title)
Definition XLChart.cpp:227
void setMinorUnit(double unit)
Set the minor unit (interval between minor tick marks) for the axis.
Definition XLChart.cpp:281
void setCrosses(XLAxisCrosses crosses)
Set the axis crossing mode.
Definition XLChart.cpp:328
void setNumberFormat(std::string_view formatCode, bool sourceLinked=false)
Set the number format for axis tick labels.
Definition XLChart.cpp:838
void setMajorGridlines(bool show)
Definition XLChart.cpp:357
XLAxis()=default
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:40
Definition XLChart.hpp:121
XLChartSeries & setDataLabelsFromRange(const XLWorksheet &wks, const XLCellRange &range)
Set the data labels for this series to come from a worksheet range.
Definition XLChart.cpp:675
XLChartSeries & setMarkerStyle(XLMarkerStyle style)
Definition XLChart.cpp:500
XLChartSeries & addTrendline(XLTrendlineType type, std::string_view name="", uint8_t order=2, uint8_t period=2)
Add a trendline to this series.
Definition XLChart.cpp:572
XLChartSeries & setTitle(std::string_view title)
Definition XLChart.cpp:475
XLChartSeries & setSmooth(bool smooth)
Definition XLChart.cpp:492
XLChartSeries & setDataPointColor(uint32_t pointIdx, std::string_view hexRGB)
Override the color of a single data point within this series.
Definition XLChart.cpp:798
XLChartSeries & setColor(std::string_view hexRGB)
Set the fill and line color of this data series.
Definition XLChart.cpp:788
XLChartSeries & addErrorBars(XLErrorBarDirection direction, XLErrorBarType type, XLErrorBarValueType valType, double value=0.0)
Add error bars to this series.
Definition XLChart.cpp:607
XLChartSeries & setLineDash(XLLineDashType dashType)
Definition XLChart.cpp:1068
XLChartSeries & setDataLabels(bool showValue, bool showCategoryName=false, bool showPercent=false)
Enable and configure data labels for this series.
Definition XLChart.cpp:554
XLChartSeries & setLineWidth(double points)
Definition XLChart.cpp:1040
Definition XLChart.hpp:261
XLChartSeries addBubbleSeries(std::string_view xValRef, std::string_view yValRef, std::string_view sizeRef, std::string_view title="")
Add a bubble chart series with explicit X, Y, and size ranges.
Definition XLChart.cpp:968
void setShowDataTable(bool showTable, bool showKeys=false)
Definition XLChart.cpp:1148
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
void setStyle(uint8_t styleId)
Set the built-in chart style ID (1–48).
Definition XLChart.cpp:129
void setSeriesSmooth(uint32_t seriesIndex, bool smooth)
Set whether a specific series should be rendered with a smooth line.
Definition XLChart.cpp:411
void setRotation(uint16_t x, uint16_t y, uint16_t perspective=30)
Set the rotation of a 3D chart.
Definition XLChart.cpp:925
void setGapWidth(uint32_t percent)
Set the gap width between bar/column clusters.
Definition XLChart.cpp:864
void setOverlap(int32_t percent)
Set the overlap between bars/columns within a cluster.
Definition XLChart.cpp:885
XLAxis axis(std::string_view position) const
Definition XLChart.cpp:379
void setLegendPosition(XLLegendPosition position)
Set the legend position or hide it.
Definition XLChart.cpp:138
void setShowDataLabels(bool showValue, bool showCategory=false, bool showPercent=false)
Configure the display of data labels on the chart.
Definition XLChart.cpp:179
XLChart & operator=(const XLChart &other)=default
Copy Assignment Operator.
void setHoleSize(uint8_t percent)
Set the size of the hole in a doughnut chart.
Definition XLChart.cpp:908
XLChart(const XLChart &other)=default
Copy Constructor.
XLAxis yAxis() const
Get the Y-axis (typically left value axis).
Definition XLChart.cpp:392
XLChart(XLChart &&other) noexcept=default
Move Constructor.
XLAxis xAxis() const
Get the X-axis (typically bottom category axis).
Definition XLChart.cpp:391
void setPlotAreaColor(std::string_view hexRGB)
Fill the chart plot area with a solid color.
Definition XLChart.cpp:950
friend class XLDocument
Definition XLChart.hpp:422
void setTitle(std::string_view title)
Set the chart title.
Definition XLChart.cpp:106
~XLChart()=default
Destructor.
XLChart & operator=(XLChart &&other) noexcept=default
Move Assignment Operator.
void setChartAreaColor(std::string_view hexRGB)
Fill the outermost chart space background with a solid color.
Definition XLChart.cpp:957
void setSeriesMarker(uint32_t seriesIndex, XLMarkerStyle style)
Set the marker style for a specific series.
Definition XLChart.cpp:420
XLChart()
Constructor.
Definition XLChart.hpp:266
Definition XLConstants.hpp:42
static XLDistance Pixels(uint32_t px)
Definition XLConstants.hpp:45
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
XLXmlFile()=default
Default constructor.
Definition IXLCellProvider.hpp:24
XLErrorBarDirection
Definition XLChart.hpp:108
@ Y
Definition XLChart.hpp:108
XLAxisOrientation
Definition XLChart.hpp:116
@ MinMax
Definition XLChart.hpp:116
@ MaxMin
Definition XLChart.hpp:116
XLAxisTickLabelPosition
Definition XLChart.hpp:88
@ Low
Definition XLChart.hpp:91
@ High
Definition XLChart.hpp:90
@ NextToAxis
Definition XLChart.hpp:89
XLMarkerStyle
Definition XLChart.hpp:72
@ X
Definition XLChart.hpp:72
@ Star
Definition XLChart.hpp:72
@ Circle
Definition XLChart.hpp:72
@ Plus
Definition XLChart.hpp:72
@ Dash
Definition XLChart.hpp:72
@ Triangle
Definition XLChart.hpp:72
@ None
Definition XLChart.hpp:72
@ Default
Definition XLChart.hpp:72
@ Picture
Definition XLChart.hpp:72
@ Diamond
Definition XLChart.hpp:72
@ Dot
Definition XLChart.hpp:72
@ Square
Definition XLChart.hpp:72
XLLineDashType
Definition XLChart.hpp:74
@ SysDashDot
Definition XLChart.hpp:85
@ SysDot
Definition XLChart.hpp:84
@ LgDashDot
Definition XLChart.hpp:81
@ LgDash
Definition XLChart.hpp:79
@ LgDashDotDot
Definition XLChart.hpp:82
@ Unset
Definition XLChart.hpp:75
@ SysDash
Definition XLChart.hpp:83
@ Solid
Definition XLChart.hpp:76
@ DashDot
Definition XLChart.hpp:80
XLTrendlineType
Definition XLChart.hpp:114
@ Polynomial
Definition XLChart.hpp:114
@ Logarithmic
Definition XLChart.hpp:114
@ Linear
Definition XLChart.hpp:114
@ MovingAverage
Definition XLChart.hpp:114
@ Exponential
Definition XLChart.hpp:114
@ Power
Definition XLChart.hpp:114
XLChartType
Definition XLChart.hpp:24
@ Scatter
Definition XLChart.hpp:43
@ Bar3D
Definition XLChart.hpp:28
@ Column
Definition XLChart.hpp:31
@ Bar3DStacked
Definition XLChart.hpp:29
@ StockOHLC
Definition XLChart.hpp:51
@ Line3D
Definition XLChart.hpp:40
@ ScatterLineMarker
Scatter with straight lines and markers.
Definition XLChart.hpp:45
@ Doughnut
Definition XLChart.hpp:62
@ BarOfPie
Definition XLChart.hpp:67
@ ScatterMarker
Scatter with markers only, no lines.
Definition XLChart.hpp:48
@ Column3D
Definition XLChart.hpp:34
@ Line
Definition XLChart.hpp:37
@ BarPercentStacked
Definition XLChart.hpp:27
@ Pie3D
Definition XLChart.hpp:42
@ Bar3DPercentStacked
Definition XLChart.hpp:30
@ ScatterLine
Scatter with straight lines, no markers.
Definition XLChart.hpp:44
@ LinePercentStacked
Definition XLChart.hpp:39
@ ScatterSmoothMarker
Scatter with smooth lines and markers.
Definition XLChart.hpp:47
@ RadarFilled
Definition XLChart.hpp:64
@ StockHLC
Definition XLChart.hpp:50
@ Pie
Definition XLChart.hpp:41
@ ColumnStacked
Definition XLChart.hpp:32
@ AreaPercentStacked
Definition XLChart.hpp:58
@ Surface
Definition XLChart.hpp:52
@ Area3DStacked
Definition XLChart.hpp:60
@ LineStacked
Definition XLChart.hpp:38
@ Column3DPercentStacked
Definition XLChart.hpp:36
@ Bubble
Bubble chart (needs addBubbleSeries).
Definition XLChart.hpp:49
@ ColumnPercentStacked
Definition XLChart.hpp:33
@ PieOfPie
Definition XLChart.hpp:66
@ Area3DPercentStacked
Definition XLChart.hpp:61
@ SurfaceWireframe
Definition XLChart.hpp:54
@ ScatterSmooth
Scatter with smooth lines, no markers.
Definition XLChart.hpp:46
@ Area3D
Definition XLChart.hpp:59
@ Radar
Definition XLChart.hpp:63
@ Bar
Definition XLChart.hpp:25
@ Area
Definition XLChart.hpp:56
@ Column3DStacked
Definition XLChart.hpp:35
@ Surface3D
Definition XLChart.hpp:53
@ RadarMarkers
Definition XLChart.hpp:65
@ AreaStacked
Definition XLChart.hpp:57
@ Surface3DWireframe
Definition XLChart.hpp:55
@ BarStacked
Definition XLChart.hpp:26
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
XLErrorBarValueType
Definition XLChart.hpp:112
@ Percentage
Definition XLChart.hpp:112
@ StandardDeviation
Definition XLChart.hpp:112
@ Custom
Definition XLChart.hpp:112
@ FixedValue
Definition XLChart.hpp:112
@ StandardError
Definition XLChart.hpp:112
XLAxisCrosses
Definition XLChart.hpp:118
@ AutoZero
Definition XLChart.hpp:118
@ Max
Definition XLChart.hpp:118
@ Min
Definition XLChart.hpp:118
XLErrorBarType
Definition XLChart.hpp:110
@ Both
Definition XLChart.hpp:110
@ Minus
Definition XLChart.hpp:110
XLLegendPosition
Definition XLChart.hpp:70
@ TopRight
Definition XLChart.hpp:70
@ Bottom
Definition XLChart.hpp:70
@ Hidden
Definition XLChart.hpp:70
@ Right
Definition XLChart.hpp:70
@ Left
Definition XLChart.hpp:70
@ Top
Definition XLChart.hpp:70
uint32_t row
Definition XLChart.hpp:178
std::string name
Definition XLChart.hpp:177
XLChartAnchor(std::string_view n, uint32_t r, uint32_t c, XLDistance w, XLDistance h)
Definition XLChart.hpp:184
XLDistance width
Definition XLChart.hpp:180
XLDistance height
Definition XLChart.hpp:181
uint32_t col
Definition XLChart.hpp:179