QGIS API Documentation  2.99.0-Master (f1c3692)
qgshistogramdiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshistogramdiagram.cpp
3  ---------------------
4  begin : August 2012
5  copyright : (C) 2012 by Matthias Kuhn
6  email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgshistogramdiagram.h"
16 #include "qgsdiagramrenderer.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
23 {
24  mCategoryBrush.setStyle( Qt::SolidPattern );
25  mPen.setStyle( Qt::SolidLine );
26  mScaleFactor = 0;
27 }
28 
30 {
31  return new QgsHistogramDiagram( *this );
32 }
33 
35 {
36  QSizeF size;
37  if ( feature.attributes().isEmpty() )
38  {
39  return size; //zero size if no attributes
40  }
41 
42  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
43  return size; // invalid value range => zero size
44 
45  double maxValue = 0;
46 
47  QgsExpressionContext expressionContext = c.expressionContext();
48  expressionContext.setFeature( feature );
49  if ( !feature.fields().isEmpty() )
50  expressionContext.setFields( feature.fields() );
51 
52  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
53  {
54  QgsExpression *expression = getExpression( cat, expressionContext );
55  maxValue = std::max( expression->evaluate( &expressionContext ).toDouble(), maxValue );
56  }
57 
58  // Scale, if extension is smaller than the specified minimum
59  if ( maxValue < s.minimumSize )
60  {
61  maxValue = s.minimumSize;
62  }
63 
64  switch ( s.diagramOrientation )
65  {
68  mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
69  size.scale( s.barWidth * s.categoryAttributes.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
70  break;
71 
74  mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
75  size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size(), Qt::IgnoreAspectRatio );
76  break;
77  }
78 
79  return size;
80 }
81 
83 {
84  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
85  return s.minimumSize; // invalid value range => zero size
86 
87  // Scale, if extension is smaller than the specified minimum
88  if ( value < s.minimumSize )
89  {
90  value = s.minimumSize;
91  }
92 
93  double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
94  return value * scaleFactor;
95 }
96 
98 {
100 }
101 
103 {
104  Q_UNUSED( c );
105  QSizeF size;
106 
107  if ( attributes.isEmpty() )
108  {
109  return QSizeF(); //zero size if no attributes
110  }
111 
112  double maxValue = attributes.at( 0 ).toDouble();
113 
114  for ( int i = 0; i < attributes.count(); ++i )
115  {
116  maxValue = std::max( attributes.at( i ).toDouble(), maxValue );
117  }
118 
119  switch ( s.diagramOrientation )
120  {
123  mScaleFactor = maxValue / s.size.height();
124  size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio );
125  break;
126 
129  default: // just in case...
130  mScaleFactor = maxValue / s.size.width();
131  size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio );
132  break;
133  }
134 
135  return size;
136 }
137 
138 void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
139 {
140  QPainter *p = c.painter();
141  if ( !p )
142  {
143  return;
144  }
145 
146  QList<double> values;
147  double maxValue = 0;
148 
149  QgsExpressionContext expressionContext = c.expressionContext();
150  expressionContext.setFeature( feature );
151  if ( !feature.fields().isEmpty() )
152  expressionContext.setFields( feature.fields() );
153 
154  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
155  {
156  QgsExpression *expression = getExpression( cat, expressionContext );
157  double currentVal = expression->evaluate( &expressionContext ).toDouble();
158  values.push_back( currentVal );
159  maxValue = std::max( currentVal, maxValue );
160  }
161 
162  double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
163 
164  double currentOffset = 0;
165  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
166 
167  double baseX = position.x();
168  double baseY = position.y();
169 
170  mPen.setColor( s.penColor );
171  setPenWidth( mPen, s, c );
172  p->setPen( mPen );
173 
174  QList<double>::const_iterator valIt = values.constBegin();
175  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
176  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
177  {
178  double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
179 
180  mCategoryBrush.setColor( *colIt );
181  p->setBrush( mCategoryBrush );
182 
183  switch ( s.diagramOrientation )
184  {
186  p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
187  break;
188 
190  p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
191  break;
192 
194  p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
195  break;
196 
198  p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
199  break;
200  }
201 
202  currentOffset += scaledWidth;
203  }
204 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:58
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
virtual QgsHistogramDiagram * clone() const override
Returns an instance that is equivalent to this one.
QList< QString > categoryAttributes
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const override
Returns the size of the legend item for the diagram corresponding to a specified value.
#define DIAGRAM_NAME_HISTOGRAM
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QgsFields fields
Definition: qgsfeature.h:73
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:227
QString diagramName() const override
Get a descriptive name for this diagram type.
DiagramOrientation diagramOrientation
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:41
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QgsExpressionContext & expressionContext()
Gets the expression context.
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfields.cpp:110
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
Definition: qgsdiagram.cpp:52
A vector of attributes.
Definition: qgsattributes.h:58
QList< QColor > categoryColors
Stores the settings for rendering a single diagram.
QgsAttributes attributes
Definition: qgsfeature.h:72