QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
49 {
50 public:
55 explicit SeriesDetails( const QString &name = QString() )
56 : mName( name ) {};
57
58 bool operator==( const SeriesDetails &other ) const
59 {
60 return mName == other.mName && mXExpression == other.mXExpression && mYExpression == other.mYExpression && mFilterExpression == other.mFilterExpression;
61 }
62
66 QString name() const { return mName; }
67
71 void setName( const QString &name ) { mName = name; }
72
79 QString xExpression() const { return mXExpression; }
80
87 void setXExpression( const QString &xExpression ) { mXExpression = xExpression; }
88
93 QString yExpression() const { return mYExpression; }
94
99 void setYExpression( const QString &yExpression ) { mYExpression = yExpression; }
100
105 QString filterExpression() const { return mFilterExpression; }
106
111 void setFilterExpression( const QString &filterExpression ) { mFilterExpression = filterExpression; }
112
113 private:
114 QString mName;
115 QString mXExpression;
116 QString mYExpression;
117 QString mFilterExpression;
118 };
119
123 QgsLayoutItemChart( QgsLayout *layout );
124
125 int type() const override;
126 QIcon icon() const override;
127
133 void setPlot( QgsPlot *plot SIP_TRANSFER );
134
138 QgsPlot *plot() { return mPlot.get(); }
139
145 void setSourceLayer( QgsVectorLayer *layer );
146
152 QgsVectorLayer *sourceLayer() const { return mVectorLayer.get(); }
153
162 void setSortFeatures( bool sorted );
163
172 bool sortFeatures() const { return mSortFeatures; }
173
184 void setSortAscending( bool ascending );
185
196 bool sortAscending() const { return mSortAscending; }
197
206 void setSortExpression( const QString &expression );
207
216 QString sortExpression() const { return mSortExpression; }
217
223 void setSeriesList( const QList<QgsLayoutItemChart::SeriesDetails> &seriesList );
224
230 QList<QgsLayoutItemChart::SeriesDetails> seriesList() const { return mSeriesList; }
231
239 void setMap( QgsLayoutItemMap *map );
240
247 QgsLayoutItemMap *map() const { return mMap; }
248
256 void setFilterOnlyVisibleFeatures( bool visibleOnly );
257
265 bool filterOnlyVisibleFeatures() const { return mFilterOnlyVisibleFeatures; }
266
272 void setFilterToAtlasFeature( bool filterToAtlas );
273
280 bool filterToAtlasFeature() const { return mFilterToAtlasIntersection; }
281
287 static QgsLayoutItemChart *create( QgsLayout *layout ) SIP_FACTORY;
288
289 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
290 void finalizeRestoreFromXml() override;
291
292 public slots:
293
294 void refresh() override;
295
296 protected:
297 void draw( QgsLayoutItemRenderContext &context ) override;
298 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
299 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
300
301 private:
302 void refreshData();
303 void gatherData();
304 void processData();
305
306 void prepareGatherer();
307
308 QgsLayoutItemChart() = delete;
309 QgsLayoutItemChart( const QgsLayoutItemChart & ) = delete;
310 QgsLayoutItemChart &operator=( const QgsLayoutItemChart & ) = delete;
311
312 std::unique_ptr<Qgs2DPlot> mPlot;
313 QgsPlotData mPlotData;
314
315 QgsVectorLayerRef mVectorLayer = nullptr;
316 bool mSortFeatures = false;
317 bool mSortAscending = true;
318 QString mSortExpression;
319
320 QList<QgsLayoutItemChart::SeriesDetails> mSeriesList;
321
322 QPointer< QgsLayoutItemMap > mMap = nullptr;
323 QString mMapUuid;
324
325 bool mFilterOnlyVisibleFeatures = false;
326 bool mFilterToAtlasIntersection = false;
327
328 bool mNeedsGathering = false;
329 bool mIsGathering = false;
330 QTimer mGathererTimer;
331 QPointer<QgsVectorLayerAbstractPlotDataGatherer> mGatherer;
332};
333
334#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:303
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