QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslayoutitemchart.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemchart.h
3 -------------------
4 begin : August 2025
5 copyright : (C) 2025 by Mathieu
6 email : mathieu at opengis dot ch
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 QGSLAYOUTITEMCHART_H
18#define QGSLAYOUTITEMCHART_H
19
20#include "qgis_core.h"
21#include "qgslayoutitem.h"
22#include "qgsplot.h"
24#include "qgsvectorlayerref.h"
25
26#include <QTimer>
27
33class CORE_EXPORT QgsLayoutItemChart : public QgsLayoutItem
34{
35 Q_OBJECT
36
37 public:
38
49 {
50 public:
51
56 explicit SeriesDetails( const QString &name = QString() )
57 : mName( name )
58 {};
59
60 bool operator==( const SeriesDetails &other ) const
61 {
62 return mName == other.mName
63 && mXExpression == other.mXExpression
64 && mYExpression == other.mYExpression
65 && mFilterExpression == other.mFilterExpression;
66 }
67
71 QString name() const { return mName; }
72
76 void setName( const QString &name ) { mName = name; }
77
84 QString xExpression() const { return mXExpression; }
85
92 void setXExpression( const QString &xExpression ) { mXExpression = xExpression; }
93
98 QString yExpression() const { return mYExpression; }
99
104 void setYExpression( const QString &yExpression ) { mYExpression = yExpression; }
105
110 QString filterExpression() const { return mFilterExpression; }
111
116 void setFilterExpression( const QString &filterExpression ) { mFilterExpression = filterExpression; }
117
118 private:
119 QString mName;
120 QString mXExpression;
121 QString mYExpression;
122 QString mFilterExpression;
123 };
124
128 QgsLayoutItemChart( QgsLayout *layout );
129
130 int type() const override;
131 QIcon icon() const override;
132
138 void setPlot( QgsPlot *plot SIP_TRANSFER );
139
143 QgsPlot *plot() { return mPlot.get(); }
144
150 void setSourceLayer( QgsVectorLayer *layer );
151
157 QgsVectorLayer *sourceLayer() const { return mVectorLayer.get(); }
158
167 void setSortFeatures( bool sorted );
168
177 bool sortFeatures() const { return mSortFeatures; }
178
189 void setSortAscending( bool ascending );
190
201 bool sortAscending() const { return mSortAscending; }
202
211 void setSortExpression( const QString &expression );
212
221 QString sortExpression() const { return mSortExpression; }
222
228 void setSeriesList( const QList<QgsLayoutItemChart::SeriesDetails> &seriesList );
229
235 QList<QgsLayoutItemChart::SeriesDetails> seriesList() const { return mSeriesList; }
236
242 static QgsLayoutItemChart *create( QgsLayout *layout ) SIP_FACTORY;
243
244 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
245
246 public slots:
247
248 void refresh() override;
249
250 protected:
251
252 void draw( QgsLayoutItemRenderContext &context ) override;
253 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
254 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
255
256 private:
257
258 void refreshData();
259 void gatherData();
260 void processData();
261
262 void prepareGatherer();
263
264 QgsLayoutItemChart() = delete;
265 QgsLayoutItemChart( const QgsLayoutItemChart & ) = delete;
266 QgsLayoutItemChart &operator=( const QgsLayoutItemChart & ) = delete;
267
268 std::unique_ptr<Qgs2DPlot> mPlot;
269 QgsPlotData mPlotData;
270
271 QgsVectorLayerRef mVectorLayer = nullptr;
272 bool mSortFeatures = false;
273 bool mSortAscending = true;
274 QString mSortExpression;
275
276 QList<QgsLayoutItemChart::SeriesDetails> mSeriesList;
277
278 bool mNeedsGathering = false;
279 bool mIsGathering = false;
280 QTimer mGathererTimer;
281 QPointer<QgsVectorLayerAbstractPlotDataGatherer> mGatherer;
282};
283
284#endif // QGSLAYOUTITEMCHART_H
QString yExpression() const
Returns the expression used to generate Y-axis values.
void setName(const QString &name)
Sets the series name.
void setFilterExpression(const QString &filterExpression)
Sets the filter expression used to generate a series against a subset of the source layer.
QString name() const
Returns the series name.
bool operator==(const SeriesDetails &other) const
SeriesDetails(const QString &name=QString())
Constructor for SeriesDetails with an optional name parameter to provide a name string to the series.
void setXExpression(const QString &xExpression)
Sets the expression used to generate X-axis values.
void setYExpression(const QString &yExpression)
Sets the expression used to generate Y-axis values.
QString filterExpression() const
Returns the filter expression used to generate a series against a subset of the source layer.
QString xExpression() const
Returns the expression used to generate X-axis values.
A layout item subclass that renders chart plots.
QgsPlot * plot()
Returns the plot used to render the chart.
QgsVectorLayer * sourceLayer() const
Returns the source vector layer from which the plot data will be gathered from.
bool sortFeatures() const
Returns true if features should be sorted when iterating through the vector layer from which the plot...
bool sortAscending() const
Returns true if features should be sorted in an ascending order when iterating through the vector lay...
QgsLayoutItemChart(QgsLayout *layout)
Constructor for QgsLayoutItemChart, with the specified parent layout.
QString sortExpression() const
Returns the expression used to sort features when iterating through the vector layer from which the p...
QList< QgsLayoutItemChart::SeriesDetails > seriesList() const
Returns the plot series details used to generate the plot data.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual QIcon icon() const
Returns the item's icon.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item's content.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
int type() const override
Returns a unique graphics item type identifier.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
Encapsulates one or more plot series.
Definition qgsplot.h:300
Base class for plot/chart/graphs.
Definition qgsplot.h:48
A container for the context for various read/write operations on objects.
Represents a vector layer which manages a vector based dataset.
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:84
_LayerRef< QgsVectorLayer > QgsVectorLayerRef