QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsplot.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsplot.h
3 ---------------
4 begin : March 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#ifndef QGSPLOT_H
18#define QGSPLOT_H
19
20#include <memory>
21
22#include "qgis_core.h"
23#include "qgis_sip.h"
24#include "qgsmargins.h"
26#include "qgstextformat.h"
27
28#include <QSizeF>
29
30class QgsColorRamp;
31class QgsMarkerSymbol;
32class QgsLineSymbol;
33class QgsFillSymbol;
35class QgsSymbol;
37
47class CORE_EXPORT QgsPlot
48{
49 //SIP_TYPEHEADER_INCLUDE( "qgsbarchartplot.h" );
50 //SIP_TYPEHEADER_INCLUDE( "qgslinechartplot.h" );
51 //SIP_TYPEHEADER_INCLUDE( "qgspiechartplot.h" );
52
53#ifdef SIP_RUN
55 if ( QgsPlot *item = dynamic_cast< QgsPlot * >( sipCpp ) )
56 {
57 if ( dynamic_cast<QgsBarChartPlot *>( item ) != NULL )
58 {
59 sipType = sipType_QgsBarChartPlot;
60 }
61 else if ( dynamic_cast<QgsLineChartPlot *>( item ) != NULL )
62 {
63 sipType = sipType_QgsLineChartPlot;
64 }
65 else if ( dynamic_cast<QgsPieChartPlot *>( item ) != NULL )
66 {
67 sipType = sipType_QgsPieChartPlot;
68 }
69 else if ( dynamic_cast<Qgs2DXyPlot *>( item ) != NULL )
70 {
71 sipType = sipType_Qgs2DXyPlot;
72 }
73 else if ( dynamic_cast<Qgs2DPlot *>( item ) != NULL )
74 {
75 sipType = sipType_Qgs2DPlot;
76 }
77 else
78 {
79 sipType = sipType_QgsPlot;
80 }
81 }
82 else
83 {
84 sipType = NULL;
85 }
87#endif
88
89 public:
90
96 {
97 MarginLeft,
98 MarginTop,
99 MarginRight,
100 MarginBottom,
101 XAxisMajorInterval,
102 XAxisMinorInterval,
103 XAxisLabelInterval,
104 YAxisMajorInterval,
105 YAxisMinorInterval,
106 YAxisLabelInterval,
107 XAxisMinimum,
108 XAxisMaximum,
109 YAxisMinimum,
110 YAxisMaximum,
111 };
112
113 QgsPlot() = default;
114
115 virtual ~QgsPlot();
116
121 virtual QString type() const { return QString(); }
122
129 void setDataDefinedProperty( DataDefinedProperty key, const QgsProperty &property ) { mDataDefinedProperties.setProperty( key, property ); }
130
137
144
152
156 virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
157
161 virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context );
162
166 static const QgsPropertiesDefinition &propertyDefinitions();
167
168 protected:
170
171 private:
172 static void initPropertyDefinitions();
173 static QgsPropertiesDefinition sPropertyDefinitions;
174};
175
176
183class CORE_EXPORT QgsPlotRenderContext
184{
185 public:
186
192};
193
194
203class CORE_EXPORT QgsAbstractPlotSeries
204{
205#ifdef SIP_RUN
207 if ( QgsAbstractPlotSeries *item = dynamic_cast< QgsAbstractPlotSeries * >( sipCpp ) )
208 {
209 if ( dynamic_cast<QgsXyPlotSeries *>( item ) != NULL )
210 {
211 sipType = sipType_QgsXyPlotSeries;
212 }
213 else
214 {
215 sipType = sipType_QgsAbstractPlotSeries;
216 }
217 }
218 else
219 {
220 sipType = NULL;
221 }
222 SIP_END
223#endif
224 public:
225
227 virtual ~QgsAbstractPlotSeries() = default;
228
232 QString name() const;
233
237 void setName( const QString &name );
238
243
244 private:
245
246 QString mName;
247};
248
257class CORE_EXPORT QgsXyPlotSeries : public QgsAbstractPlotSeries
258{
259 public:
260
261 QgsXyPlotSeries() = default;
262 ~QgsXyPlotSeries() override = default;
263
267 QList<std::pair<double, double>> data() const;
268
272 void setData( const QList<std::pair<double, double>> &data );
273
277 void append( double x, double y );
278
282 void clear();
283
284 QgsAbstractPlotSeries *clone() const override SIP_FACTORY;
285
286 private:
287
288 QList<std::pair<double, double>> mData;
289};
290
299class CORE_EXPORT QgsPlotData
300{
301 public:
302
303 QgsPlotData() = default;
304 ~QgsPlotData();
305
306 QgsPlotData( const QgsPlotData &other );
308 QgsPlotData &operator=( const QgsPlotData &other );
310
315 QList<QgsAbstractPlotSeries *> series() const;
316
322
326 void clearSeries();
327
331 QStringList categories() const;
332
336 void setCategories( const QStringList &categories );
337
338 private:
339
340 QList<QgsAbstractPlotSeries *> mSeries;
341 QStringList mCategories;
342};
343
352class CORE_EXPORT QgsPlotAxis
353{
354 public:
355
356 QgsPlotAxis();
358
359 QgsPlotAxis( const QgsPlotAxis &other ) = delete;
360 QgsPlotAxis &operator=( const QgsPlotAxis &other ) = delete;
361
365 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
366
370 bool readXml( const QDomElement &element, const QgsReadWriteContext &context );
371
376 Qgis::PlotAxisType type() const;
377
383
389 double gridIntervalMinor() const { return mGridIntervalMinor; }
390
396 void setGridIntervalMinor( double interval ) { mGridIntervalMinor = interval; }
397
403 double gridIntervalMajor() const { return mGridIntervalMajor; }
404
410 void setGridIntervalMajor( double interval ) { mGridIntervalMajor = interval; }
411
417 double labelInterval() const { return mLabelInterval; }
418
424 void setLabelInterval( double interval ) { mLabelInterval = interval; }
425
431 QgsLineSymbol *gridMajorSymbol();
432
440 void setGridMajorSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
441
447 QgsLineSymbol *gridMinorSymbol();
448
456 void setGridMinorSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
457
463 QgsTextFormat textFormat() const;
464
470 void setTextFormat( const QgsTextFormat &format );
471
477 QgsNumericFormat *numericFormat() const;
478
486 void setNumericFormat( QgsNumericFormat *format SIP_TRANSFER );
487
496 QString labelSuffix() const;
497
506 void setLabelSuffix( const QString &suffix );
507
516 Qgis::PlotAxisSuffixPlacement labelSuffixPlacement() const;
517
526 void setLabelSuffixPlacement( Qgis::PlotAxisSuffixPlacement placement );
527
528 private:
529
530#ifdef SIP_RUN
531 QgsPlotAxis( const QgsPlotAxis &other );
532#endif
533
535
536 double mGridIntervalMinor = 1;
537 double mGridIntervalMajor = 5;
538
539 double mLabelInterval = 1;
540
541 QString mLabelSuffix;
543
544 std::unique_ptr< QgsNumericFormat > mNumericFormat;
545
546 std::unique_ptr< QgsLineSymbol > mGridMajorSymbol;
547 std::unique_ptr< QgsLineSymbol > mGridMinorSymbol;
548
549 QgsTextFormat mLabelTextFormat;
550
551};
552
561class CORE_EXPORT Qgs2DPlot : public QgsPlot
562{
563 public:
564
568 Qgs2DPlot();
569
570 ~Qgs2DPlot() override;
571
572 Qgs2DPlot( const Qgs2DPlot &other ) = delete;
573 Qgs2DPlot &operator=( const Qgs2DPlot &other ) = delete;
574
575 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
576 bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
577
581 virtual void render( QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QgsPlotData &plotData = QgsPlotData() );
582
594 virtual void renderContent( QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QRectF &plotArea, const QgsPlotData &plotData = QgsPlotData() );
595
601 QSizeF size() const;
602
608 void setSize( QSizeF size );
609
614 virtual QRectF interiorPlotArea( QgsRenderContext &context, QgsPlotRenderContext &plotContext ) const;
615
621 const QgsMargins &margins() const;
622
628 void setMargins( const QgsMargins &margins );
629
630 protected:
631
634
635 private:
636
637#ifdef SIP_RUN
638 Qgs2DPlot( const Qgs2DPlot &other );
639#endif
640
641 QSizeF mSize;
642 QgsMargins mMargins;
643
644 friend class Qgs2DXyPlot;
645};
646
658class CORE_EXPORT Qgs2DXyPlot : public Qgs2DPlot
659{
660 public:
661
665 Qgs2DXyPlot();
666
667 ~Qgs2DXyPlot() override;
668
669 Qgs2DXyPlot( const Qgs2DXyPlot &other ) = delete;
670 Qgs2DXyPlot &operator=( const Qgs2DXyPlot &other ) = delete;
671
672 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
673 bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
674
678 void render( QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QgsPlotData &plotData = QgsPlotData() ) override;
679
684 QRectF interiorPlotArea( QgsRenderContext &context, QgsPlotRenderContext &plotContext ) const override;
685
694
700 double xMinimum() const { return mMinX; }
701
707 void setXMinimum( double minimum ) { mMinX = minimum; }
708
714 double yMinimum() const { return mMinY; }
715
721 void setYMinimum( double minimum ) { mMinY = minimum; }
722
728 double xMaximum() const { return mMaxX; }
729
735 void setXMaximum( double maximum ) { mMaxX = maximum; }
736
742 double yMaximum() const { return mMaxY; }
743
749 void setYMaximum( double maximum ) { mMaxY = maximum; }
750
756 QgsPlotAxis &xAxis() { return mXAxis; }
757
763 const QgsPlotAxis &xAxis() const SIP_SKIP { return mXAxis; }
764
770 QgsPlotAxis &yAxis() { return mYAxis; }
771
777 const QgsPlotAxis &yAxis() const SIP_SKIP { return mYAxis; }
778
784 QgsFillSymbol *chartBackgroundSymbol();
785
793 void setChartBackgroundSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
794
800 QgsFillSymbol *chartBorderSymbol();
801
809 void setChartBorderSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
810
811 protected:
812
814 void applyDataDefinedProperties( QgsRenderContext &context, double &minX, double &maxX, double &minY, double &maxY, double &majorIntervalX, double &minorIntervalX, double &labelIntervalX, double &majorIntervalY, double &minorIntervalY, double &labelIntervalY ) const;
815
816 private:
817
818#ifdef SIP_RUN
819 Qgs2DXyPlot( const Qgs2DXyPlot &other );
820#endif
821
822 double mMinX = 0;
823 double mMinY = 0;
824 double mMaxX = 10;
825 double mMaxY = 10;
826
827 std::unique_ptr< QgsFillSymbol > mChartBackgroundSymbol;
828 std::unique_ptr< QgsFillSymbol > mChartBorderSymbol;
829
830 QgsPlotAxis mXAxis;
831 QgsPlotAxis mYAxis;
832};
833
919
920#endif // QGSPLOT_H
PlotAxisSuffixPlacement
Placement options for suffixes in the labels for axis of plots.
Definition qgis.h:3318
@ EveryLabel
Place suffix after every value label.
Definition qgis.h:3320
PlotAxisType
Plots axis types.
Definition qgis.h:3334
@ Interval
The axis represents a range of values.
Definition qgis.h:3335
Base class for 2-dimensional plot/chart/graphs.
Definition qgsplot.h:562
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the plot's properties into an XML element.
Definition qgsplot.cpp:234
void applyDataDefinedProperties(QgsRenderContext &context, QgsMargins &margins) const
Applies 2D plot data-defined properties.
Definition qgsplot.cpp:302
virtual void renderContent(QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QRectF &plotArea, const QgsPlotData &plotData=QgsPlotData())
Renders the plot content.
Definition qgsplot.cpp:263
void setSize(QSizeF size)
Sets the overall size of the plot (including titles and over components which sit outside the plot ar...
Definition qgsplot.cpp:274
const QgsMargins & margins() const
Returns the margins of the plot area (in millimeters).
Definition qgsplot.cpp:292
Qgs2DPlot & operator=(const Qgs2DPlot &other)=delete
~Qgs2DPlot() override
friend class Qgs2DXyPlot
Definition qgsplot.h:644
virtual QRectF interiorPlotArea(QgsRenderContext &context, QgsPlotRenderContext &plotContext) const
Returns the area of the plot which corresponds to the actual plot content (excluding all titles and o...
Definition qgsplot.cpp:279
Qgs2DPlot()
Constructor for Qgs2DPlot.
Definition qgsplot.cpp:229
Qgs2DPlot(const Qgs2DPlot &other)=delete
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the plot's properties from an XML element.
Definition qgsplot.cpp:243
virtual void render(QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QgsPlotData &plotData=QgsPlotData())
Renders the plot.
Definition qgsplot.cpp:252
QSizeF size() const
Returns the overall size of the plot (in millimeters) (including titles and other components which si...
Definition qgsplot.cpp:269
void setMargins(const QgsMargins &margins)
Sets the margins of the plot area (in millimeters).
Definition qgsplot.cpp:297
Base class for 2-dimensional plot/chart/graphs with an X and Y axes.
Definition qgsplot.h:659
void setXMaximum(double maximum)
Sets the maximum value of the x axis.
Definition qgsplot.h:735
double yMaximum() const
Returns the maximum value of the y axis.
Definition qgsplot.h:742
void setYMinimum(double minimum)
Sets the minimum value of the y axis.
Definition qgsplot.h:721
double xMinimum() const
Returns the minimum value of the x axis.
Definition qgsplot.h:700
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the plot's properties from an XML element.
Definition qgsplot.cpp:390
Qgs2DXyPlot(const Qgs2DXyPlot &other)=delete
Qgs2DXyPlot & operator=(const Qgs2DXyPlot &other)=delete
QgsPlotAxis & yAxis()
Returns a reference to the plot's y axis.
Definition qgsplot.h:770
~Qgs2DXyPlot() override
void render(QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QgsPlotData &plotData=QgsPlotData()) override
Renders the plot.
Definition qgsplot.cpp:412
void setXMinimum(double minimum)
Sets the minimum value of the x axis.
Definition qgsplot.h:707
void calculateOptimisedIntervals(QgsRenderContext &context, QgsPlotRenderContext &plotContext)
Automatically sets the grid and label intervals to optimal values for display in the given render con...
Definition qgsplot.cpp:889
const QgsPlotAxis & yAxis() const
Returns a reference to the plot's y axis.
Definition qgsplot.h:777
QgsPlotAxis & xAxis()
Returns a reference to the plot's x axis.
Definition qgsplot.h:756
Qgs2DXyPlot()
Constructor for Qgs2DXyPlot.
Definition qgsplot.cpp:356
const QgsPlotAxis & xAxis() const
Returns a reference to the plot's x axis.
Definition qgsplot.h:763
double yMinimum() const
Returns the minimum value of the y axis.
Definition qgsplot.h:714
QRectF interiorPlotArea(QgsRenderContext &context, QgsPlotRenderContext &plotContext) const override
Returns the area of the plot which corresponds to the actual plot content (excluding all titles and o...
Definition qgsplot.cpp:760
void setYMaximum(double maximum)
Sets the maximum value of the y axis.
Definition qgsplot.h:749
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the plot's properties into an XML element.
Definition qgsplot.cpp:364
double xMaximum() const
Returns the maximum value of the x axis.
Definition qgsplot.h:728
An abstract class used to encapsulate the data for a plot series.
Definition qgsplot.h:204
void setName(const QString &name)
Sets the series' name.
Definition qgsplot.cpp:1319
virtual ~QgsAbstractPlotSeries()=default
QString name() const
Returns the series' name.
Definition qgsplot.cpp:1314
QgsAbstractPlotSeries()=default
virtual QgsAbstractPlotSeries * clone() const =0
Clones the series.
A simple bar chart class.
Abstract base class for color ramps.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A simple line chart class.
A line symbol type, for rendering LineString and MultiLineString geometries.
Defines the four margins of a rectangle.
Definition qgsmargins.h:38
A marker symbol type, for rendering Point and MultiPoint geometries.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
A simple pie chart class.
Encapsulates the properties of a plot axis.
Definition qgsplot.h:353
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the axis' properties from an XML element.
Definition qgsplot.cpp:140
double gridIntervalMinor() const
Returns the interval of minor grid lines for the axis.
Definition qgsplot.h:389
double gridIntervalMajor() const
Returns the interval of major grid lines for the axis.
Definition qgsplot.h:403
void setType(Qgis::PlotAxisType type)
Sets the axis type.
Definition qgsplot.cpp:108
void setGridIntervalMajor(double interval)
Sets the interval of major grid lines for the axis.
Definition qgsplot.h:410
void setGridIntervalMinor(double interval)
Sets the interval of minor grid lines for the axis.
Definition qgsplot.h:396
void setLabelInterval(double interval)
Sets the interval of labels for the axis.
Definition qgsplot.h:424
Qgis::PlotAxisType type() const
Returns the axis type.
Definition qgsplot.cpp:103
double labelInterval() const
Returns the interval of labels for the axis.
Definition qgsplot.h:417
QgsPlotAxis & operator=(const QgsPlotAxis &other)=delete
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes the axis' properties into an XML element.
Definition qgsplot.cpp:113
QgsPlotAxis(const QgsPlotAxis &other)=delete
Encapsulates one or more plot series.
Definition qgsplot.h:300
QgsPlotData()=default
QStringList categories() const
Returns the name of the series' categories.
Definition qgsplot.cpp:1300
QgsPlotData & operator=(const QgsPlotData &other)
Definition qgsplot.cpp:1254
void clearSeries()
Clears all series from the plot data.
Definition qgsplot.cpp:1294
QList< QgsAbstractPlotSeries * > series() const
Returns the list of series forming the plot data.
Definition qgsplot.cpp:1281
void setCategories(const QStringList &categories)
Sets the name of the series' categories.
Definition qgsplot.cpp:1305
void addSeries(QgsAbstractPlotSeries *series)
Adds a series to the plot data.
Definition qgsplot.cpp:1286
Manages default settings for plot objects.
Definition qgsplot.h:843
static QgsFillSymbol * chartBorderSymbol()
Returns the default fill symbol to use for the chart area border.
Definition qgsplot.cpp:1190
static QgsNumericFormat * pieChartNumericFormat()
Returns the default color ramp to use for pie charts.
Definition qgsplot.cpp:1225
static QgsNumericFormat * axisLabelNumericFormat()
Returns the default numeric format to use for plot axis labels.
Definition qgsplot.cpp:1165
static QgsColorRamp * pieChartColorRamp()
Returns the default color ramp to use for pie charts.
Definition qgsplot.cpp:1220
static QgsFillSymbol * barChartFillSymbol()
Returns the default fill symbol to use for bar charts.
Definition qgsplot.cpp:1208
static QgsLineSymbol * lineChartLineSymbol()
Returns the default line symbol to use for line charts.
Definition qgsplot.cpp:1202
static QgsLineSymbol * axisGridMinorSymbol()
Returns the default line symbol to use for axis minor grid lines.
Definition qgsplot.cpp:1177
static QgsFillSymbol * pieChartFillSymbol()
Returns the default fill symbol to use for pie charts.
Definition qgsplot.cpp:1214
static QgsMarkerSymbol * lineChartMarkerSymbol()
Returns the default marker symbol to use for line charts.
Definition qgsplot.cpp:1196
static QgsFillSymbol * chartBackgroundSymbol()
Returns the default fill symbol to use for the chart area background fill.
Definition qgsplot.cpp:1184
static QgsLineSymbol * axisGridMajorSymbol()
Returns the default line symbol to use for axis major grid lines.
Definition qgsplot.cpp:1170
Contains information about the context of a plot rendering operation.
Definition qgsplot.h:184
~QgsPlotRenderContext()=default
QgsPlotRenderContext()=default
Constructor for QgsPlotRenderContext.
QgsPlot()=default
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the plot's property collection, used for data defined overrides.
Definition qgsplot.h:151
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the plot's property collection, used for data defined overrides.
Definition qgsplot.h:143
virtual ~QgsPlot()
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the plot's property collection, used for data defined overrides.
Definition qgsplot.h:136
virtual QString type() const
Returns the plot's type.
Definition qgsplot.h:121
QgsPropertyCollection mDataDefinedProperties
Definition qgsplot.h:169
void setDataDefinedProperty(DataDefinedProperty key, const QgsProperty &property)
Sets a data defined property for the plot.
Definition qgsplot.h:129
DataDefinedProperty
Data defined properties for different plot types.
Definition qgsplot.h:96
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
A store for object properties.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Container for all settings relating to text rendering.
Encapsulates the data for an XY plot series.
Definition qgsplot.h:258
void setData(const QList< std::pair< double, double > > &data)
Sets the series' list of XY pairs of double.
Definition qgsplot.cpp:1333
QgsAbstractPlotSeries * clone() const override
Clones the series.
Definition qgsplot.cpp:1348
QgsXyPlotSeries()=default
void append(double x, double y)
Appends a pair of X/Y double values to the series.
Definition qgsplot.cpp:1338
void clear()
Clears the series' data.
Definition qgsplot.cpp:1343
QList< std::pair< double, double > > data() const
Returns the series' list of XY pairs of double.
Definition qgsplot.cpp:1328
~QgsXyPlotSeries() override=default
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:268
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_END
Definition qgis_sip.h:216
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.