QGIS API Documentation  master-59fd5e0
src/core/diagram/qgshistogramdiagram.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgshistogramdiagram.cpp
00003     ---------------------
00004     begin                : August 2012
00005     copyright            : (C) 2012 by Matthias Kuhn
00006     email                : matthias dot kuhn at gmx dot ch
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #include "qgshistogramdiagram.h"
00016 #include "qgsdiagramrendererv2.h"
00017 #include "qgsrendercontext.h"
00018 
00019 #include <QPainter>
00020 
00021 QgsHistogramDiagram::QgsHistogramDiagram()
00022 {
00023   mCategoryBrush.setStyle( Qt::SolidPattern );
00024   mPen.setStyle( Qt::SolidLine );
00025   mScaleFactor = 0;
00026 }
00027 
00028 QgsHistogramDiagram::~QgsHistogramDiagram()
00029 {
00030 }
00031 
00032 QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
00033 {
00034   Q_UNUSED( c );
00035   QSizeF size;
00036   if ( attributes.count() == 0 )
00037   {
00038     return size; //zero size if no attributes
00039   }
00040 
00041   double maxValue = 0;
00042 
00043   foreach ( int cat, s.categoryIndices )
00044   {
00045     maxValue = qMax( attributes[cat].toDouble(), maxValue );
00046   }
00047 
00048   // Scale, if extension is smaller than the specified minimum
00049   if ( maxValue < s.minimumSize )
00050   {
00051     maxValue = s.minimumSize;
00052   }
00053 
00054   switch ( s.diagramOrientation )
00055   {
00056     case QgsDiagramSettings::Up:
00057     case QgsDiagramSettings::Down:
00058       mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
00059       size.scale( s.barWidth * s.categoryIndices.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
00060       break;
00061 
00062     case QgsDiagramSettings::Right:
00063     case QgsDiagramSettings::Left:
00064       mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
00065       size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryIndices.size(), Qt::IgnoreAspectRatio );
00066       break;
00067   }
00068 
00069   return size;
00070 }
00071 
00072 QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s )
00073 {
00074   Q_UNUSED( c );
00075   QSizeF size;
00076 
00077   if ( attributes.count() == 0 )
00078   {
00079     return QSizeF(); //zero size if no attributes
00080   }
00081 
00082   double maxValue = attributes[0].toDouble();
00083 
00084   for ( int i = 0; i < attributes.count(); ++i )
00085   {
00086     maxValue = qMax( attributes[i].toDouble(), maxValue );
00087   }
00088 
00089   switch ( s.diagramOrientation )
00090   {
00091     case QgsDiagramSettings::Up:
00092     case QgsDiagramSettings::Down:
00093       mScaleFactor = maxValue / s.size.height();
00094       size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio );
00095       break;
00096 
00097     case QgsDiagramSettings::Right:
00098     case QgsDiagramSettings::Left:
00099     default: // just in case...
00100       mScaleFactor = maxValue / s.size.width();
00101       size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio );
00102       break;
00103   }
00104 
00105   return size;
00106 }
00107 
00108 void QgsHistogramDiagram::renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
00109 {
00110   QPainter* p = c.painter();
00111   if ( !p )
00112   {
00113     return;
00114   }
00115 
00116   QList<double> values;
00117   double maxValue = 0;
00118 
00119   foreach ( int cat, s.categoryIndices )
00120   {
00121     double currentVal = att[cat].toDouble();
00122     values.push_back( currentVal );
00123     maxValue = qMax( currentVal, maxValue );
00124   }
00125 
00126   double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
00127 
00128   double currentOffset = 0;
00129   double scaledWidth = sizePainterUnits( s.barWidth, s, c );
00130 
00131   double baseX = position.x();
00132   double baseY = position.y();
00133 
00134   mPen.setColor( s.penColor );
00135   setPenWidth( mPen, s, c );
00136   p->setPen( mPen );
00137 
00138   QList<double>::const_iterator valIt = values.constBegin();
00139   QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
00140   for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
00141   {
00142     double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
00143 
00144     mCategoryBrush.setColor( *colIt );
00145     p->setBrush( mCategoryBrush );
00146 
00147     switch ( s.diagramOrientation )
00148     {
00149       case QgsDiagramSettings::Up:
00150         p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
00151         break;
00152 
00153       case QgsDiagramSettings::Down:
00154         p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
00155         break;
00156 
00157       case QgsDiagramSettings::Right:
00158         p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
00159         break;
00160 
00161       case QgsDiagramSettings::Left:
00162         p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
00163         break;
00164     }
00165 
00166     currentOffset += scaledWidth;
00167   }
00168 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines