QGIS API Documentation  2.14.0-Essen
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 "qgsdiagramrendererv2.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 }
32 
34 {
35  return new QgsHistogramDiagram( *this );
36 }
37 
39 {
40  QSizeF size;
41  if ( feature.attributes().isEmpty() )
42  {
43  return size; //zero size if no attributes
44  }
45 
46  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
47  return size; // invalid value range => zero size
48 
49  double maxValue = 0;
50 
51  QgsExpressionContext expressionContext = c.expressionContext();
52  expressionContext.setFeature( feature );
53  if ( feature.fields() )
54  expressionContext.setFields( *feature.fields() );
55 
56  Q_FOREACH ( const QString& cat, s.categoryAttributes )
57  {
58  QgsExpression* expression = getExpression( cat, expressionContext );
59  maxValue = qMax( expression->evaluate( &expressionContext ).toDouble(), maxValue );
60  }
61 
62  // Scale, if extension is smaller than the specified minimum
63  if ( maxValue < s.minimumSize )
64  {
65  maxValue = s.minimumSize;
66  }
67 
68  switch ( s.diagramOrientation )
69  {
72  mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
73  size.scale( s.barWidth * s.categoryAttributes.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
74  break;
75 
78  mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
79  size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size(), Qt::IgnoreAspectRatio );
80  break;
81  }
82 
83  return size;
84 }
85 
87 {
88  Q_UNUSED( c );
89  QSizeF size;
90 
91  if ( attributes.isEmpty() )
92  {
93  return QSizeF(); //zero size if no attributes
94  }
95 
96  double maxValue = attributes.at( 0 ).toDouble();
97 
98  for ( int i = 0; i < attributes.count(); ++i )
99  {
100  maxValue = qMax( attributes.at( i ).toDouble(), maxValue );
101  }
102 
103  switch ( s.diagramOrientation )
104  {
107  mScaleFactor = maxValue / s.size.height();
108  size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio );
109  break;
110 
113  default: // just in case...
114  mScaleFactor = maxValue / s.size.width();
115  size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio );
116  break;
117  }
118 
119  return size;
120 }
121 
123 {
124  QPainter* p = c.painter();
125  if ( !p )
126  {
127  return;
128  }
129 
130  QList<double> values;
131  double maxValue = 0;
132 
133  QgsExpressionContext expressionContext = c.expressionContext();
134  expressionContext.setFeature( feature );
135  if ( feature.fields() )
136  expressionContext.setFields( *feature.fields() );
137 
138  Q_FOREACH ( const QString& cat, s.categoryAttributes )
139  {
140  QgsExpression* expression = getExpression( cat, expressionContext );
141  double currentVal = expression->evaluate( &expressionContext ).toDouble();
142  values.push_back( currentVal );
143  maxValue = qMax( currentVal, maxValue );
144  }
145 
146  double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
147 
148  double currentOffset = 0;
149  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
150 
151  double baseX = position.x();
152  double baseY = position.y();
153 
154  mPen.setColor( s.penColor );
155  setPenWidth( mPen, s, c );
156  p->setPen( mPen );
157 
158  QList<double>::const_iterator valIt = values.constBegin();
160  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
161  {
162  double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
163 
164  mCategoryBrush.setColor( *colIt );
165  p->setBrush( mCategoryBrush );
166 
167  switch ( s.diagramOrientation )
168  {
170  p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
171  break;
172 
174  p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
175  break;
176 
178  p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
179  break;
180 
182  p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
183  break;
184  }
185 
186  currentOffset += scaledWidth;
187  }
188 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:84
Class for parsing and evaluation of expressions (formerly called "search strings").
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.
void setStyle(Qt::PenStyle style)
void scale(qreal width, qreal height, Qt::AspectRatioMode mode)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
virtual QgsHistogramDiagram * clone() const override
Returns an instance that is equivalent to this one.
void push_back(const T &value)
QList< QString > categoryAttributes
Q_DECL_DEPRECATED QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
void setStyle(Qt::BrushStyle style)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:285
int size() const
void drawRect(const QRectF &rectangle)
DiagramOrientation diagramOrientation
qreal x() const
qreal y() const
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
void setPen(const QColor &color)
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:188
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setBrush(const QBrush &brush)
void setColor(const QColor &color)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QgsExpressionContext & expressionContext()
Gets the expression context.
const T & at(int i) const
Contains information about the context of a rendering operation.
QPainter * painter()
bool isEmpty() const
int count(const T &value) const
double toDouble(bool *ok) const
const_iterator constEnd() const
const_iterator constBegin() const
qreal height() const
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:71
A vector of attributes.
Definition: qgsfeature.h:115
QList< QColor > categoryColors
void setColor(const QColor &color)
qreal width() const