QGIS API Documentation 4.1.0-Master (376402f9aeb)
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
288 void setGenerateCategoriesFromRenderer( bool generateCategoriesFromRenderer );
289
296 bool generateCategoriesFromRenderer() const { return mGenerateCategoriesFromRenderer; }
297
304 void setApplyRendererStyle( bool applyRendererStyle );
305
312 bool applyRendererStyle() const { return mApplyRendererStyle; }
313
319 static QgsLayoutItemChart *create( QgsLayout *layout ) SIP_FACTORY;
320
321 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
322 void finalizeRestoreFromXml() override;
323
324 public slots:
325
326 void refresh() override;
327
328 protected:
329 void draw( QgsLayoutItemRenderContext &context ) override;
330 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
331 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
332
333 private:
334 void refreshData();
335 void gatherData();
336 void processData();
337
338 void prepareGatherer();
339
340 void createRendererSeriesDetails( QString &rendererXExpression, QStringList &rendererCategories );
341 void applyRendererStyleToPlot( Qgs2DPlot *plot ) const;
342
343 QgsLayoutItemChart() = delete;
344 QgsLayoutItemChart( const QgsLayoutItemChart & ) = delete;
345 QgsLayoutItemChart &operator=( const QgsLayoutItemChart & ) = delete;
346
347 std::unique_ptr<Qgs2DPlot> mPlot;
348 QgsPlotData mPlotData;
349
350 QgsVectorLayerRef mVectorLayer = nullptr;
351 bool mSortFeatures = false;
352 bool mSortAscending = true;
353 QString mSortExpression;
354
355 QList<QgsLayoutItemChart::SeriesDetails> mSeriesList;
356 bool mGenerateCategoriesFromRenderer = false;
357 bool mApplyRendererStyle = true;
358
359 QPointer< QgsLayoutItemMap > mMap = nullptr;
360 QString mMapUuid;
361
362 bool mFilterOnlyVisibleFeatures = false;
363 bool mFilterToAtlasIntersection = false;
364
365 bool mNeedsGathering = false;
366 bool mIsGathering = false;
367 QTimer mGathererTimer;
368 QPointer<QgsVectorLayerAbstractPlotDataGatherer> mGatherer;
369};
370
371#endif // QGSLAYOUTITEMCHART_H
Base class for 2-dimensional plot/chart/graphs.
Definition qgsplot.h:585
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 applyRendererStyle() const
Returns true if chart symbols will adopt the source layer's symbology renderer (e....
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 generateCategoriesFromRenderer() const
Returns true if series' X axis will adopt categories generated from the source layer's symbology rend...
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:51
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