29using namespace Qt::StringLiterals;
38 if ( mFillSymbols.empty() )
43 const QList<QgsAbstractPlotSeries *> seriesList = plotData.
series();
44 if ( seriesList.isEmpty() )
49 const QStringList categories = plotData.
categories();
53 if ( categories.isEmpty() )
67 context.
painter()->setClipRect( plotArea );
79 Qgs2DXyPlot::applyDataDefinedProperties( context, minX, maxX, minY, maxY, majorIntervalX, minorIntervalX, labelIntervalX, majorIntervalY, minorIntervalY, labelIntervalY );
83 double categoriesWidth = 0.0;
84 double valuesWidth = 0.0;
85 double barsWidth = 0.0;
86 double barWidth = 0.0;
89 xScale = plotArea.height() / ( maxX - minX );
90 yScale = plotArea.width() / ( maxY - minY );
91 categoriesWidth = plotArea.height() /
static_cast<double>( categories.size() );
92 valuesWidth = plotArea.height() * ( minorIntervalX / ( maxX - minY ) );
94 barWidth = barsWidth / seriesList.size();
98 xScale = plotArea.width() / ( maxX - minX );
99 yScale = plotArea.height() / ( maxY - minY );
100 categoriesWidth = plotArea.width() /
static_cast<double>( categories.size() );
101 valuesWidth = plotArea.width() * ( minorIntervalX / ( maxX - minX ) );
103 barWidth = barsWidth / seriesList.size();
118 const double barStartAdjustment = -( barsWidth / 2 ) + barWidth * seriesIndex;
121 const QList<std::pair<double, double>> data = xySeries->data();
122 for (
const std::pair<double, double> &pair : data )
128 if ( pair.first < 0 || pair.first >= categories.size() )
132 x = ( categoriesWidth * pair.first ) + ( categoriesWidth / 2 ) + barStartAdjustment;
137 x = ( pair.first - minX ) * xScale + barStartAdjustment;
141 const double y = ( pair.second - minY ) * yScale;
142 const double zero = ( 0.0 - minY ) * yScale;
147 topLeft = QPoint( plotArea.x() + zero, plotArea.bottom() - x - barWidth );
148 bottomRight = QPoint( plotArea.x() + y, plotArea.bottom() - x );
152 topLeft = QPoint( plotArea.left() + x, plotArea.y() + plotArea.height() - y );
153 bottomRight = QPoint( plotArea.left() + x + barWidth, plotArea.y() + plotArea.height() - zero );
157 symbol->
renderPolygon( QPolygonF( QRectF( topLeft, bottomRight ) ),
nullptr,
nullptr, context );
168 if ( index < 0 || index >=
static_cast<int>( mFillSymbols.size() ) )
173 return mFillSymbols[index].get();
183 if ( index + 1 >=
static_cast<int>( mFillSymbols.size() ) )
185 mFillSymbols.resize( index + 1 );
188 mFillSymbols[index].reset( symbol );
195 QDomElement fillSymbolsElement = document.createElement( u
"fillSymbols"_s );
196 for (
int i = 0; i < static_cast<int>( mFillSymbols.size() ); i++ )
198 QDomElement fillSymbolElement = document.createElement( u
"fillSymbol"_s );
199 fillSymbolElement.setAttribute( u
"index"_s, QString::number( i ) );
200 if ( mFillSymbols[i] )
204 fillSymbolsElement.appendChild( fillSymbolElement );
206 element.appendChild( fillSymbolsElement );
215 const QDomNodeList fillSymbolsList = element.firstChildElement( u
"fillSymbols"_s ).childNodes();
216 for (
int i = 0; i < fillSymbolsList.count(); i++ )
218 const QDomElement fillSymbolElement = fillSymbolsList.at( i ).toElement();
219 const int index = fillSymbolElement.attribute( u
"index"_s, u
"-1"_s ).toInt();
222 if ( fillSymbolElement.hasChildNodes() )
224 const QDomElement symbolElement = fillSymbolElement.firstChildElement( u
"symbol"_s );
271 for (
int idx = 0; idx < barChartPlot->fillSymbolCount(); idx++ )
@ Categorical
The axis represents categories.
@ Interval
The axis represents a range of values.
void copyCommonProperties(const Qgs2DPlot *other)
Copies all Qgs2DPlot-level properties (size, margins, data-defined properties) from other to this plo...
Qgs2DPlot()
Constructor for Qgs2DPlot.
void copyCommonProperties(const Qgs2DXyPlot *other)
Copies all Qgs2DXyPlot-level properties (axis ranges, axes, flip state, background and border symbols...
double yMaximum() const
Returns the maximum value of the y axis.
double xMinimum() const
Returns the minimum value of the x axis.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the plot's properties from an XML element.
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
Applies 2D XY plot data-defined properties.
QgsPlotAxis & yAxis()
Returns a reference to the plot's y axis.
bool flipAxes() const
Returns whether the X and Y axes are flipped.
QgsPlotAxis & xAxis()
Returns a reference to the plot's x axis.
Qgs2DXyPlot()
Constructor for Qgs2DXyPlot.
double yMinimum() const
Returns the minimum value of the y axis.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the plot's properties into an XML element.
double xMaximum() const
Returns the maximum value of the x axis.
An abstract class used to encapsulate the data for a plot series.
QString type() const override
Returns the plot's type.
void setFillSymbolAt(int index, QgsFillSymbol *symbol)
Sets the fill symbol to use for the series with matching index.
void renderContent(QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QRectF &plotArea, const QgsPlotData &plotData=QgsPlotData()) override
Renders the plot content.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the plot's properties from an XML element.
static QgsVectorLayerAbstractPlotDataGatherer * createDataGatherer(QgsPlot *plot)
Returns a new data gatherer for a given bar chart plot.
QgsFillSymbol * fillSymbolAt(int index) const
Returns the fill symbol for the series with matching index.
void initFromPlot(const QgsPlot *plot) override
Initializes properties of this plot from an existing plot, transferring all applicable settings.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the plot's properties into an XML element.
static QgsBarChartPlot * create()
Returns a new bar chart.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
void renderPolygon(const QPolygonF &points, const QVector< QPolygonF > *rings, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol using the given render context.
double gridIntervalMinor() const
Returns the interval of minor grid lines for the axis.
double gridIntervalMajor() const
Returns the interval of major grid lines for the axis.
Qgis::PlotAxisType type() const
Returns the axis type.
double labelInterval() const
Returns the interval of labels for the axis.
Encapsulates one or more plot series.
QStringList categories() const
Returns the name of the series' categories.
QList< QgsAbstractPlotSeries * > series() const
Returns the list of series forming the plot data.
static QgsFillSymbol * barChartFillSymbol()
Returns the default fill symbol to use for bar charts.
Contains information about the context of a plot rendering operation.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
Scoped object for saving and restoring a QPainter object's state.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
An abstract vector layer plot data gatherer base class.
An vector layer plot data gatherer class for XY series.
Encapsulates the data for an XY plot series.
Single variable definition for use within a QgsExpressionContextScope.