Skip to content

Commit 870ecf5

Browse files
committed
[diagrams] Fix scaling and positioning of histograms
1 parent 63617ba commit 870ecf5

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/core/diagram/qgshistogramdiagram.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ QgsHistogramDiagram::~QgsHistogramDiagram()
3232
QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
3333
{
3434
Q_UNUSED( c );
35-
QSize size;
35+
QSizeF size;
3636
if ( attributes.count() == 0 )
3737
{
38-
return QSizeF(); //zero size if no attributes
38+
return size; //zero size if no attributes
3939
}
4040

41-
double maxValue = attributes[0].toDouble();
41+
double maxValue = 0;
4242

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

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

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

@@ -114,14 +114,17 @@ void QgsHistogramDiagram::renderDiagram( const QgsAttributes& att, QgsRenderCont
114114
}
115115

116116
QList<double> values;
117+
double maxValue = 0;
117118

118-
QList<int>::const_iterator catIt = s.categoryIndices.constBegin();
119-
for ( ; catIt != s.categoryIndices.constEnd(); ++catIt )
119+
foreach ( int cat, s.categoryIndices )
120120
{
121-
double currentVal = att[*catIt].toDouble();
121+
double currentVal = att[cat].toDouble();
122122
values.push_back( currentVal );
123+
maxValue = qMax( currentVal, maxValue );
123124
}
124125

126+
double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
127+
125128
double currentOffset = 0;
126129
double scaledWidth = sizePainterUnits( s.barWidth, s, c );
127130

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

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

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

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

0 commit comments

Comments
 (0)