QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgspiediagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspiediagram.cpp
3  ---------------------
4  begin : March 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole 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 "qgspiediagram.h"
16 #include "qgsdiagramrenderer.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
22 
24 {
25  mCategoryBrush.setStyle( Qt::SolidPattern );
26  mPen.setStyle( Qt::SolidLine );
27 }
28 
30 {
31  return new QgsPieDiagram( *this );
32 }
33 
35 {
36  Q_UNUSED( c )
37 
38  QVariant attrVal;
40  {
41  QgsExpressionContext expressionContext = c.expressionContext();
42  if ( !feature.fields().isEmpty() )
43  expressionContext.setFields( feature.fields() );
44  expressionContext.setFeature( feature );
45 
46  QgsExpression *expression = getExpression( is.classificationAttributeExpression, expressionContext );
47  attrVal = expression->evaluate( &expressionContext );
48  }
49  else
50  {
51  attrVal = feature.attribute( is.classificationField );
52  }
53 
54  bool ok = false;
55  const double value = attrVal.toDouble( &ok );
56  if ( !ok )
57  {
58  return QSizeF(); //zero size if attribute is missing
59  }
60 
61  return sizeForValue( value, s, is );
62 }
63 
64 double QgsPieDiagram::legendSize( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
65 {
66  const QSizeF size = sizeForValue( value, s, is );
67  return std::max( size.width(), size.height() );
68 }
69 
71 {
72  return DIAGRAM_NAME_PIE;
73 }
74 
75 QSizeF QgsPieDiagram::diagramSize( const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s )
76 {
77  Q_UNUSED( c )
78  Q_UNUSED( attributes )
79  return s.size;
80 }
81 
82 void QgsPieDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
83 {
84  QPainter *p = c.painter();
85  if ( !p )
86  {
87  return;
88  }
89 
90  //get sum of values
91  QList<double> values;
92  double currentVal = 0;
93  double valSum = 0;
94  int valCount = 0;
95 
96  QgsExpressionContext expressionContext = c.expressionContext();
97  expressionContext.setFeature( feature );
98  if ( !feature.fields().isEmpty() )
99  expressionContext.setFields( feature.fields() );
100 
101  QList<QString>::const_iterator catIt = s.categoryAttributes.constBegin();
102  for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
103  {
104  QgsExpression *expression = getExpression( *catIt, expressionContext );
105  currentVal = expression->evaluate( &expressionContext ).toDouble();
106  values.push_back( currentVal );
107  valSum += currentVal;
108  if ( currentVal ) valCount++;
109  }
110 
111  //draw the slices
112  double totalAngle = 0;
113  double currentAngle;
114 
115  //convert from mm / map units to painter units
116  const QSizeF spu = sizePainterUnits( s.size, s, c );
117  const double w = spu.width();
118  const double h = spu.height();
119 
120  const double baseX = position.x();
121  const double baseY = position.y() - h;
122 
123  mPen.setColor( s.penColor );
124  setPenWidth( mPen, s, c );
125  p->setPen( mPen );
126 
127  // there are some values > 0 available
128  if ( valSum > 0 )
129  {
130  QList<double>::const_iterator valIt = values.constBegin();
131  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
132  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
133  {
134  if ( *valIt )
135  {
136  currentAngle = ( *valIt / valSum * 360 * 16 ) * ( s.direction() == QgsDiagramSettings::Clockwise ? -1 : 1 );
137  QColor brushColor( *colIt );
138  brushColor.setAlphaF( brushColor.alphaF() * s.opacity );
139  mCategoryBrush.setColor( brushColor );
140  p->setBrush( mCategoryBrush );
141  // if only 1 value is > 0, draw a circle
142  if ( valCount == 1 )
143  {
144  p->drawEllipse( QRectF( baseX, baseY, w, h ) );
145  }
146  else
147  {
148  p->drawPie( QRectF( baseX, baseY, w, h ), totalAngle - s.rotationOffset * 16.0, currentAngle );
149  }
150  totalAngle += currentAngle;
151  }
152  }
153  }
154  else // valSum > 0
155  {
156  // draw empty circle if no values are defined at all
157  mCategoryBrush.setColor( Qt::transparent );
158  p->setBrush( mCategoryBrush );
159  p->drawEllipse( QRectF( baseX, baseY, w, h ) );
160  }
161 }
QgsDiagram::getExpression
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:38
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsPieDiagram::diagramName
QString diagramName() const override
Gets a descriptive name for this diagram type.
Definition: qgspiediagram.cpp:70
QgsDiagram::sizeForValue
QSizeF sizeForValue(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const
Returns the scaled size of a diagram for a value, respecting the specified diagram interpolation sett...
Definition: qgsdiagram.cpp:81
QgsFields::isEmpty
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
QgsDiagramSettings::categoryColors
QList< QColor > categoryColors
Definition: qgsdiagramrenderer.h:421
QgsDiagramSettings::penColor
QColor penColor
Definition: qgsdiagramrenderer.h:451
qgsexpression.h
QgsDiagramSettings::Clockwise
@ Clockwise
Clockwise orientation.
Definition: qgsdiagramrenderer.h:406
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsExpressionContext::setFields
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Definition: qgsexpressioncontext.cpp:587
QgsPieDiagram::legendSize
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.
Definition: qgspiediagram.cpp:64
QgsDiagramInterpolationSettings::classificationField
QString classificationField
Name of the field for classification.
Definition: qgsdiagramrenderer.h:671
QgsDiagramSettings::opacity
double opacity
Opacity, from 0 (transparent) to 1.0 (opaque)
Definition: qgsdiagramrenderer.h:458
QgsPieDiagram::QgsPieDiagram
QgsPieDiagram()
Definition: qgspiediagram.cpp:23
QgsPieDiagram
A pie chart diagram.
Definition: qgspiediagram.h:38
QgsDiagram::setPenWidth
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:49
QgsDiagram::sizePainterUnits
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:55
QgsDiagramSettings::rotationOffset
double rotationOffset
Rotation offset, in degrees clockwise from horizontal.
Definition: qgsdiagramrenderer.h:466
qgspiediagram.h
qgsrendercontext.h
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
QgsDiagramSettings::direction
Direction direction() const
Returns the chart's angular direction.
Definition: qgsdiagramrenderer.cpp:928
qgsdiagramrenderer.h
QgsDiagramInterpolationSettings
Additional diagram settings for interpolated size rendering.
Definition: qgsdiagramrenderer.h:662
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:350
QgsDiagramInterpolationSettings::classificationAttributeIsExpression
bool classificationAttributeIsExpression
Definition: qgsdiagramrenderer.h:674
DIAGRAM_NAME_PIE
#define DIAGRAM_NAME_PIE
Definition: qgspiediagram.h:18
QgsDiagramSettings::size
QSizeF size
Definition: qgsdiagramrenderer.h:425
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:70
QgsPieDiagram::clone
QgsPieDiagram * clone() const override
Returns an instance that is equivalent to this one.
Definition: qgspiediagram.cpp:29
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsPieDiagram::diagramSize
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
Definition: qgspiediagram.cpp:75
QgsDiagramSettings::categoryAttributes
QList< QString > categoryAttributes
Definition: qgsdiagramrenderer.h:422
QgsPieDiagram::renderDiagram
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
Definition: qgspiediagram.cpp:82
QgsDiagramInterpolationSettings::classificationAttributeExpression
QString classificationAttributeExpression
Definition: qgsdiagramrenderer.h:673
QgsDiagramSettings
Stores the settings for rendering a single diagram.
Definition: qgsdiagramrenderer.h:381
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525