Skip to content

Commit

Permalink
[diagrams] Fix scaling and positioning of histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 7, 2013
1 parent 63617ba commit 870ecf5
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/core/diagram/qgshistogramdiagram.cpp
Expand Up @@ -32,17 +32,17 @@ QgsHistogramDiagram::~QgsHistogramDiagram()
QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
{ {
Q_UNUSED( c ); Q_UNUSED( c );
QSize size; QSizeF size;
if ( attributes.count() == 0 ) if ( attributes.count() == 0 )
{ {
return QSizeF(); //zero size if no attributes return size; //zero size if no attributes
} }


double maxValue = attributes[0].toDouble(); double maxValue = 0;


for ( int i = 1; i < attributes.count(); ++i ) foreach ( int cat, s.categoryIndices )
{ {
maxValue = qMax( attributes[i].toDouble(), maxValue ); maxValue = qMax( attributes[cat].toDouble(), maxValue );
} }


// Scale, if extension is smaller than the specified minimum // Scale, if extension is smaller than the specified minimum
Expand All @@ -56,13 +56,13 @@ QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const
case QgsDiagramSettings::Up: case QgsDiagramSettings::Up:
case QgsDiagramSettings::Down: case QgsDiagramSettings::Down:
mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) ); mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
size.scale( s.barWidth * attributes.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio ); size.scale( s.barWidth * s.categoryIndices.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
break; break;


case QgsDiagramSettings::Right: case QgsDiagramSettings::Right:
case QgsDiagramSettings::Left: case QgsDiagramSettings::Left:
mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) ); mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
size.scale( maxValue * mScaleFactor, s.barWidth * attributes.size(), Qt::IgnoreAspectRatio ); size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryIndices.size(), Qt::IgnoreAspectRatio );
break; break;
} }


Expand Down Expand Up @@ -114,14 +114,17 @@ void QgsHistogramDiagram::renderDiagram( const QgsAttributes& att, QgsRenderCont
} }


QList<double> values; QList<double> values;
double maxValue = 0;


QList<int>::const_iterator catIt = s.categoryIndices.constBegin(); foreach ( int cat, s.categoryIndices )
for ( ; catIt != s.categoryIndices.constEnd(); ++catIt )
{ {
double currentVal = att[*catIt].toDouble(); double currentVal = att[cat].toDouble();
values.push_back( currentVal ); values.push_back( currentVal );
maxValue = qMax( currentVal, maxValue );
} }


double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );

double currentOffset = 0; double currentOffset = 0;
double scaledWidth = sizePainterUnits( s.barWidth, s, c ); double scaledWidth = sizePainterUnits( s.barWidth, s, c );


Expand All @@ -144,19 +147,19 @@ void QgsHistogramDiagram::renderDiagram( const QgsAttributes& att, QgsRenderCont
switch ( s.diagramOrientation ) switch ( s.diagramOrientation )
{ {
case QgsDiagramSettings::Up: case QgsDiagramSettings::Up:
p->drawRect( baseX + currentOffset, baseY, scaledWidth, 0 - length ); p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
break; break;


case QgsDiagramSettings::Down: case QgsDiagramSettings::Down:
p->drawRect( baseX + currentOffset, baseY, scaledWidth, length ); p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
break; break;


case QgsDiagramSettings::Right: case QgsDiagramSettings::Right:
p->drawRect( baseX, baseY + currentOffset, length, scaledWidth ); p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
break; break;


case QgsDiagramSettings::Left: case QgsDiagramSettings::Left:
p->drawRect( baseX, baseY + currentOffset, 0 - length, scaledWidth ); p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
break; break;
} }


Expand Down

0 comments on commit 870ecf5

Please sign in to comment.