QGIS API Documentation  2.99.0-Master (b698612)
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  Q_FOREACH ( const QString &cat, s.categoryAttributes )
53  {
54  QgsExpression *expression = getExpression( cat, expressionContext );
55  maxValue = qMax( 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 {
99  Q_UNUSED( c );
100  QSizeF size;
101 
102  if ( attributes.isEmpty() )
103  {
104  return QSizeF(); //zero size if no attributes
105  }
106 
107  double maxValue = attributes.at( 0 ).toDouble();
108 
109  for ( int i = 0; i < attributes.count(); ++i )
110  {
111  maxValue = qMax( attributes.at( i ).toDouble(), maxValue );
112  }
113 
114  switch ( s.diagramOrientation )
115  {
118  mScaleFactor = maxValue / s.size.height();
119  size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio );
120  break;
121 
124  default: // just in case...
125  mScaleFactor = maxValue / s.size.width();
126  size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio );
127  break;
128  }
129 
130  return size;
131 }
132 
133 void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
134 {
135  QPainter *p = c.painter();
136  if ( !p )
137  {
138  return;
139  }
140 
141  QList<double> values;
142  double maxValue = 0;
143 
144  QgsExpressionContext expressionContext = c.expressionContext();
145  expressionContext.setFeature( feature );
146  if ( !feature.fields().isEmpty() )
147  expressionContext.setFields( feature.fields() );
148 
149  Q_FOREACH ( const QString &cat, s.categoryAttributes )
150  {
151  QgsExpression *expression = getExpression( cat, expressionContext );
152  double currentVal = expression->evaluate( &expressionContext ).toDouble();
153  values.push_back( currentVal );
154  maxValue = qMax( currentVal, maxValue );
155  }
156 
157  double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
158 
159  double currentOffset = 0;
160  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
161 
162  double baseX = position.x();
163  double baseY = position.y();
164 
165  mPen.setColor( s.penColor );
166  setPenWidth( mPen, s, c );
167  p->setPen( mPen );
168 
169  QList<double>::const_iterator valIt = values.constBegin();
170  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
171  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
172  {
173  double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
174 
175  mCategoryBrush.setColor( *colIt );
176  p->setBrush( mCategoryBrush );
177 
178  switch ( s.diagramOrientation )
179  {
181  p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
182  break;
183 
185  p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
186  break;
187 
189  p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
190  break;
191 
193  p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
194  break;
195  }
196 
197  currentOffset += scaledWidth;
198  }
199 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:65
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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QgsFields fields
Definition: qgsfeature.h:72
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:203
DiagramOrientation diagramOrientation
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:48
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:59
A vector of attributes.
Definition: qgsattributes.h:57
QList< QColor > categoryColors
Stores the settings for rendering a single diagram.
QgsAttributes attributes
Definition: qgsfeature.h:71