QGIS API Documentation 3.99.0-Master (c22de0620c0)
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 "qgslayoutitemmap.h"
23#include "qgsplot.h"
25#include "qgsvectorlayerref.h"
26
27#include <QTimer>
28
34class CORE_EXPORT QgsLayoutItemChart : public QgsLayoutItem
35{
36 Q_OBJECT
37
38 public:
39
50 {
51 public:
52
57 explicit SeriesDetails( const QString &name = QString() )
58 : mName( name )
59 {};
60
61 bool operator==( const SeriesDetails &other ) const
62 {
63 return mName == other.mName
64 && mXExpression == other.mXExpression
65 && mYExpression == other.mYExpression
66 && mFilterExpression == other.mFilterExpression;
67 }
68
72 QString name() const { return mName; }
73
77 void setName( const QString &name ) { mName = name; }
78
85 QString xExpression() const { return mXExpression; }
86
93 void setXExpression( const QString &xExpression ) { mXExpression = xExpression; }
94
99 QString yExpression() const { return mYExpression; }
100
105 void setYExpression( const QString &yExpression ) { mYExpression = yExpression; }
106
111 QString filterExpression() const { return mFilterExpression; }
112
117 void setFilterExpression( const QString &filterExpression ) { mFilterExpression = filterExpression; }
118
119 private:
120 QString mName;
121 QString mXExpression;
122 QString mYExpression;
123 QString mFilterExpression;
124 };
125
129 QgsLayoutItemChart( QgsLayout *layout );
130
131 int type() const override;
132 QIcon icon() const override;
133
139 void setPlot( QgsPlot *plot SIP_TRANSFER );
140
144 QgsPlot *plot() { return mPlot.get(); }
145
151 void setSourceLayer( QgsVectorLayer *layer );
152
158 QgsVectorLayer *sourceLayer() const { return mVectorLayer.get(); }
159
168 void setSortFeatures( bool sorted );
169
178 bool sortFeatures() const { return mSortFeatures; }
179
190 void setSortAscending( bool ascending );
191
202 bool sortAscending() const { return mSortAscending; }
203
212 void setSortExpression( const QString &expression );
213
222 QString sortExpression() const { return mSortExpression; }
223
229 void setSeriesList( const QList<QgsLayoutItemChart::SeriesDetails> &seriesList );
230
236 QList<QgsLayoutItemChart::SeriesDetails> seriesList() const { return mSeriesList; }
237
245 void setMap( QgsLayoutItemMap *map );
246
253 QgsLayoutItemMap *map() const { return mMap; }
254
262 void setFilterOnlyVisibleFeatures( bool visibleOnly );
263
271 bool filterOnlyVisibleFeatures() const { return mFilterOnlyVisibleFeatures; }
272
278 void setFilterToAtlasFeature( bool filterToAtlas );
279
286 bool filterToAtlasFeature() const { return mFilterToAtlasIntersection; }
287
293 static QgsLayoutItemChart *create( QgsLayout *layout ) SIP_FACTORY;
294
295 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
296 void finalizeRestoreFromXml() override;
297
298 public slots:
299
300 void refresh() override;
301
302 protected:
303
304 void draw( QgsLayoutItemRenderContext &context ) override;
305 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
306 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
307
308 private:
309
310 void refreshData();
311 void gatherData();
312 void processData();
313
314 void prepareGatherer();
315
316 QgsLayoutItemChart() = delete;
317 QgsLayoutItemChart( const QgsLayoutItemChart & ) = delete;
318 QgsLayoutItemChart &operator=( const QgsLayoutItemChart & ) = delete;
319
320 std::unique_ptr<Qgs2DPlot> mPlot;
321 QgsPlotData mPlotData;
322
323 QgsVectorLayerRef mVectorLayer = nullptr;
324 bool mSortFeatures = false;
325 bool mSortAscending = true;
326 QString mSortExpression;
327
328 QList<QgsLayoutItemChart::SeriesDetails> mSeriesList;
329
330 QPointer< QgsLayoutItemMap > mMap = nullptr;
331 QString mMapUuid;
332
333 bool mFilterOnlyVisibleFeatures = false;
334 bool mFilterToAtlasIntersection = false;
335
336 bool mNeedsGathering = false;
337 bool mIsGathering = false;
338 QTimer mGathererTimer;
339 QPointer<QgsVectorLayerAbstractPlotDataGatherer> mGatherer;
340};
341
342#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.
bool filterOnlyVisibleFeatures() const
Returns true if the series are set to use only features visible on a corresponding map item.
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...
bool filterToAtlasFeature() const
Returns true if the series are set to only show features which intersect the current atlas feature.
QgsLayoutItemChart(QgsLayout *layout)
Constructor for QgsLayoutItemChart, with the specified parent layout.
QgsLayoutItemMap * map() const
Returns the layout map to use to limit the series' use of features.
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.
Layout graphical items for displaying a map.
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.
friend class QgsLayoutItemMap
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
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:35
#define SIP_FACTORY
Definition qgis_sip.h:83
_LayerRef< QgsVectorLayer > QgsVectorLayerRef