OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLChart.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLCHART_HPP
2#define OPENXLSX_XLCHART_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLConstants.hpp"
6#include "XLXmlFile.hpp"
7
8#include <optional>
9#include <string>
10#include <string_view>
11
12namespace OpenXLSX
13{
57
59
61
72 class XLWorksheet;
73 class XLCellRange;
74
75 enum class XLErrorBarDirection { X, Y };
76
77 enum class XLErrorBarType { Both, Minus, Plus };
78
80
82
84
85 enum class XLAxisCrosses { AutoZero, Min, Max };
86
87 class OPENXLSX_EXPORT XLChartSeries
88 {
89 public:
90 XLChartSeries() = default;
91 explicit XLChartSeries(const XMLNode& node);
92
93 XLChartSeries& setTitle(std::string_view title);
94 XLChartSeries& setSmooth(bool smooth);
96
101 XLChartSeries& setColor(std::string_view hexRGB);
102
108 XLChartSeries& setDataPointColor(uint32_t pointIdx, std::string_view hexRGB);
109
116 XLChartSeries& setDataLabels(bool showValue, bool showCategoryName = false, bool showPercent = false);
117
124
128 XLChartSeries& addTrendline(XLTrendlineType type, std::string_view name = "", uint8_t order = 2, uint8_t period = 2);
129
134
135 private:
136 XMLNode m_node;
137 };
138
140 {
141 std::string name;
142 uint32_t row{1};
143 uint32_t col{1};
146
147 XLChartAnchor() = default;
148 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)
149 {}
150 };
151
152 class OPENXLSX_EXPORT XLAxis
153 {
154 public:
155 XLAxis() = default;
156 explicit XLAxis(const XMLNode& node);
157
158 void setTitle(std::string_view title);
159
160 void setMinBounds(double min);
162
163 void setMaxBounds(double max);
165
170 void setMajorUnit(double unit);
171
176 void setMinorUnit(double unit);
177
182 void setLogScale(double base);
183
188 void setDateAxis(bool isDateAxis);
189
195
201
206 void setCrossesAt(double value);
207
208 void setMajorGridlines(bool show);
209 void setMinorGridlines(bool show);
210
216 void setNumberFormat(std::string_view formatCode, bool sourceLinked = false);
217
218 private:
219 XMLNode m_node;
220 };
221
222 class OPENXLSX_EXPORT XLChart final : public XLXmlFile
223 {
224 public:
228 XLChart() : XLXmlFile(nullptr) {}
229
235
240 XLChart(const XLChart& other) = default;
241
245 XLChart(XLChart&& other) noexcept = default;
246
250 ~XLChart() = default;
251
255 XLChart& operator=(const XLChart& other) = default;
256
260 XLChart& operator=(XLChart&& other) noexcept = default;
261
269 const XLCellRange& values,
270 std::string_view title = "",
271 std::optional<XLChartType> targetChartType = std::nullopt,
272 bool useSecondaryAxis = false);
274 const XLCellRange& values,
275 const XLCellRange& categories,
276 std::string_view title = "",
277 std::optional<XLChartType> targetChartType = std::nullopt,
278 bool useSecondaryAxis = false);
279 XLChartSeries addSeries(std::string_view valuesRef,
280 std::string_view title = "",
281 std::string_view categoriesRef = "",
282 std::optional<XLChartType> targetChartType = std::nullopt,
283 bool useSecondaryAxis = false);
284
293 addBubbleSeries(std::string_view xValRef, std::string_view yValRef, std::string_view sizeRef, std::string_view title = "");
294
296 const XLCellRange& xValues,
297 const XLCellRange& yValues,
298 const XLCellRange& sizes,
299 std::string_view title = "");
300
304 void setTitle(std::string_view title);
305
309 void setStyle(uint8_t styleId);
310
315
319 [[nodiscard]] XLAxis xAxis() const;
320
324 [[nodiscard]] XLAxis yAxis() const;
325
326 [[nodiscard]] XLAxis axis(std::string_view position) const;
327
331 void setShowDataLabels(bool showValue, bool showCategory = false, bool showPercent = false);
332
336 void setSeriesSmooth(uint32_t seriesIndex, bool smooth);
337
341 void setSeriesMarker(uint32_t seriesIndex, XLMarkerStyle style);
342
347 void setGapWidth(uint32_t percent);
348
353 void setOverlap(int32_t percent);
354
359 void setHoleSize(uint8_t percent);
360
367 void setRotation(uint16_t x, uint16_t y, uint16_t perspective = 30);
368
373 void setPlotAreaColor(std::string_view hexRGB);
374
379 void setChartAreaColor(std::string_view hexRGB);
380
381 private:
382 friend class XLDocument;
383 void initXml(XLChartType type = XLChartType::Bar);
384 [[nodiscard]] uint32_t seriesCount() const;
385 };
386} // namespace OpenXLSX
387
388#endif // OPENXLSX_XLCHART_HPP
XLXmlData * xmlData
Definition XLDocument.cpp:1422
Definition XLXmlParser.hpp:84
Definition XLChart.hpp:153
void setMinorGridlines(bool show)
void setNumberFormat(std::string_view formatCode, bool sourceLinked=false)
Set the number format for axis tick labels.
void setDateAxis(bool isDateAxis)
Convert this axis to a date axis or back to a category axis.
void setTitle(std::string_view title)
void setMinorUnit(double unit)
Set the minor unit (interval between minor tick marks) for the axis.
XLAxis(const XMLNode &node)
void setOrientation(XLAxisOrientation orientation)
Set the orientation of the axis (normal or reverse).
void setCrosses(XLAxisCrosses crosses)
Set the axis crossing mode.
void setCrossesAt(double value)
Set the axis to cross at a specific value.
void setMaxBounds(double max)
void setMajorGridlines(bool show)
void setMajorUnit(double unit)
Set the major unit (interval between major tick marks) for the axis.
void setLogScale(double base)
Set the axis to use a logarithmic scale.
void setMinBounds(double min)
XLAxis()=default
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:30
Definition XLChart.hpp:88
XLChartSeries & setColor(std::string_view hexRGB)
Set the fill and line color of this data series.
XLChartSeries & setMarkerStyle(XLMarkerStyle style)
XLChartSeries & setDataLabels(bool showValue, bool showCategoryName=false, bool showPercent=false)
Enable and configure data labels for this series.
XLChartSeries & setDataPointColor(uint32_t pointIdx, std::string_view hexRGB)
Override the color of a single data point within this series.
XLChartSeries & setDataLabelsFromRange(const XLWorksheet &wks, const XLCellRange &range)
Set the data labels for this series to come from a worksheet range.
XLChartSeries & setSmooth(bool smooth)
XLChartSeries & addTrendline(XLTrendlineType type, std::string_view name="", uint8_t order=2, uint8_t period=2)
Add a trendline to this series.
XLChartSeries & setTitle(std::string_view title)
XLChartSeries(const XMLNode &node)
XLChartSeries & addErrorBars(XLErrorBarDirection direction, XLErrorBarType type, XLErrorBarValueType valType, double value=0.0)
Add error bars to this series.
Definition XLChart.hpp:223
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.
void setPlotAreaColor(std::string_view hexRGB)
Fill the chart plot area with a solid color.
void setStyle(uint8_t styleId)
Set the built-in chart style ID (1–48).
void setRotation(uint16_t x, uint16_t y, uint16_t perspective=30)
Set the rotation of a 3D chart.
void setShowDataLabels(bool showValue, bool showCategory=false, bool showPercent=false)
Configure the display of data labels on the chart.
XLChartSeries addBubbleSeries(const XLWorksheet &wks, const XLCellRange &xValues, const XLCellRange &yValues, const XLCellRange &sizes, std::string_view title="")
void setHoleSize(uint8_t percent)
Set the size of the hole in a doughnut chart.
XLChartSeries addSeries(std::string_view valuesRef, std::string_view title="", std::string_view categoriesRef="", std::optional< XLChartType > targetChartType=std::nullopt, bool useSecondaryAxis=false)
void setLegendPosition(XLLegendPosition position)
Set the legend position or hide it.
XLChart(XLXmlData *xmlData)
Constructor from XLXmlData.
XLChart & operator=(const XLChart &other)=default
Copy Assignment Operator.
XLChart(const XLChart &other)=default
Copy Constructor.
XLAxis axis(std::string_view position) const
XLChartSeries addSeries(const XLWorksheet &wks, const XLCellRange &values, const XLCellRange &categories, std::string_view title="", std::optional< XLChartType > targetChartType=std::nullopt, bool useSecondaryAxis=false)
XLChart(XLChart &&other) noexcept=default
Move Constructor.
XLAxis xAxis() const
Get the X-axis (typically bottom category axis).
void setChartAreaColor(std::string_view hexRGB)
Fill the outermost chart space background with a solid color.
void setOverlap(int32_t percent)
Set the overlap between bars/columns within a cluster.
void setTitle(std::string_view title)
Set the chart title.
void setGapWidth(uint32_t percent)
Set the gap width between bar/column clusters.
void setSeriesMarker(uint32_t seriesIndex, XLMarkerStyle style)
Set the marker style for a specific series.
~XLChart()=default
Destructor.
XLChart & operator=(XLChart &&other) noexcept=default
Move Assignment Operator.
XLAxis yAxis() const
Get the Y-axis (typically left value axis).
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.
void setSeriesSmooth(uint32_t seriesIndex, bool smooth)
Set whether a specific series should be rendered with a smooth line.
XLChart()
Constructor.
Definition XLChart.hpp:228
Definition XLConstants.hpp:32
static XLDistance Pixels(uint32_t px)
Definition XLConstants.hpp:35
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:82
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:118
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:29
The XLXmlFile class provides an interface for derived classes to use. It functions as an ancestor to ...
Definition XLXmlFile.hpp:42
Definition IZipArchive.hpp:18
XLErrorBarDirection
Definition XLChart.hpp:75
XLAxisOrientation
Definition XLChart.hpp:83
XLMarkerStyle
Definition XLChart.hpp:60
XLTrendlineType
Definition XLChart.hpp:81
XLChartType
Definition XLChart.hpp:14
@ ScatterLineMarker
Scatter with straight lines and markers.
@ ScatterMarker
Scatter with markers only, no lines.
@ ScatterLine
Scatter with straight lines, no markers.
@ ScatterSmoothMarker
Scatter with smooth lines and markers.
@ Bubble
Bubble chart (needs addBubbleSeries)
@ ScatterSmooth
Scatter with smooth lines, no markers.
XLErrorBarValueType
Definition XLChart.hpp:79
XLAxisCrosses
Definition XLChart.hpp:85
XLErrorBarType
Definition XLChart.hpp:77
XLLegendPosition
Definition XLChart.hpp:58
Definition XLChart.hpp:140
uint32_t row
Definition XLChart.hpp:142
std::string name
Definition XLChart.hpp:141
XLChartAnchor(std::string_view n, uint32_t r, uint32_t c, XLDistance w, XLDistance h)
Definition XLChart.hpp:148
XLDistance width
Definition XLChart.hpp:144
XLDistance height
Definition XLChart.hpp:145
uint32_t col
Definition XLChart.hpp:143