Skip to content

Commit 468650b

Browse files
committed
Add option to scale by area/scale by diameter for PieChart and Text Diagram
1 parent 2c9a3a7 commit 468650b

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

src/app/qgsvectorlayerproperties.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,15 @@ void QgsVectorLayerProperties::apply()
862862
ds.labelPlacementMethod = QgsDiagramSettings::XHeight;
863863
}
864864

865+
if ( tr( "Area" ) == mScaleDependencyComboBox->currentText() )
866+
{
867+
ds.scaleByArea = true;
868+
}
869+
else
870+
{
871+
ds.scaleByArea = false;
872+
}
873+
865874
if ( mIncreaseSmallDiagramsCheckBox->isChecked() )
866875
{
867876
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
@@ -1374,6 +1383,17 @@ void QgsVectorLayerProperties::handleDiagramTypeChanged( const QString& itemtext
13741383
mDiagramOptionsGroupBox->hide();
13751384
mBarWidthLabel->hide();
13761385
}
1386+
1387+
if ( tr( "Text diagram" ) == itemtext || tr( "Pie chart" ) == itemtext )
1388+
{
1389+
mScaleDependencyComboBox->show();
1390+
mScaleDependencyLabel->show();
1391+
}
1392+
else
1393+
{
1394+
mScaleDependencyComboBox->hide();
1395+
mScaleDependencyLabel->hide();
1396+
}
13771397
}
13781398

13791399
void QgsVectorLayerProperties::on_mIncreaseSmallDiagramsCheckBox_stateChanged( int state )
@@ -1672,6 +1692,9 @@ void QgsVectorLayerProperties::initDiagramTab()
16721692
mLabelPlacementComboBox->addItem( tr( "Height" ) );
16731693
mLabelPlacementComboBox->addItem( tr( "x-height" ) );
16741694

1695+
mScaleDependencyComboBox->addItem( tr( "Area" ) );
1696+
mScaleDependencyComboBox->addItem( tr( "Diameter" ) );
1697+
16751698
//insert all attributes into the combo boxes
16761699
const QgsFieldMap& layerFields = layer->pendingFields();
16771700
QgsFieldMap::const_iterator fieldIt = layerFields.constBegin();

src/core/diagram/qgspiediagram.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ QSizeF QgsPieDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsR
3636
{
3737
return QSizeF(); //zero size if attribute is missing
3838
}
39-
40-
bool scaleByArea = true;
4139

4240
double scaledValue = attIt.value().toDouble();
4341
double scaledLowerValue = is.lowerValue;
@@ -48,7 +46,7 @@ QSizeF QgsPieDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsR
4846
double scaledUpperSizeHeight = is.upperSize.height();
4947

5048
// interpolate the squared value if scale by area
51-
if ( scaleByArea )
49+
if ( s.scaleByArea )
5250
{
5351
scaledValue = sqrt( scaledValue );
5452
scaledLowerValue = sqrt( scaledLowerValue );

src/core/diagram/qgstextdiagram.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,37 @@ QgsTextDiagram::~QgsTextDiagram()
3232

3333
QSizeF QgsTextDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
3434
{
35-
QgsAttributeMap::const_iterator attIt = attributes.find( is.classificationAttribute );
35+
QgsAttributeMap::const_iterator attIt = attributes.find( is.classificationAttribute );
3636
if ( attIt == attributes.constEnd() )
3737
{
3838
return QSizeF(); //zero size if attribute is missing
3939
}
40-
double value = attIt.value().toDouble();
40+
41+
double scaledValue = attIt.value().toDouble();
42+
double scaledLowerValue = is.lowerValue;
43+
double scaledUpperValue = is.upperValue;
44+
double scaledLowerSizeWidth = is.lowerSize.width();
45+
double scaledLowerSizeHeight = is.lowerSize.height();
46+
double scaledUpperSizeWidth = is.upperSize.width();
47+
double scaledUpperSizeHeight = is.upperSize.height();
48+
49+
// interpolate the squared value if scale by area
50+
if ( s.scaleByArea )
51+
{
52+
scaledValue = sqrt( scaledValue );
53+
scaledLowerValue = sqrt( scaledLowerValue );
54+
scaledUpperValue = sqrt( scaledUpperValue );
55+
scaledLowerSizeWidth = sqrt( scaledLowerSizeWidth );
56+
scaledLowerSizeHeight = sqrt( scaledLowerSizeHeight );
57+
scaledUpperSizeWidth = sqrt( scaledUpperSizeWidth );
58+
scaledUpperSizeHeight = sqrt( scaledUpperSizeHeight );
59+
}
4160

4261
//interpolate size
43-
double ratio = ( value - is.lowerValue ) / ( is.upperValue - is.lowerValue );
44-
QSizeF size = QSizeF( sqrt( is.upperSize.width() * ratio + is.lowerSize.width() * ( 1 - ratio ) ),
45-
sqrt( is.upperSize.height() * ratio + is.lowerSize.height() * ( 1 - ratio ) ) );
62+
double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
63+
64+
QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
65+
is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
4666

4767
// Scale, if extension is smaller than the specified minimum
4868
if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )

src/core/qgsdiagramrendererv2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct CORE_EXPORT QgsDiagramSettings
124124
LabelPlacementMethod labelPlacementMethod;
125125
DiagramOrientation diagramOrientation;
126126
double barWidth;
127+
bool scaleByArea;
127128

128129
//scale range (-1 if no lower / upper bound )
129130
double minScaleDenominator;

src/ui/qgsvectorlayerpropertiesbase.ui

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,16 @@
14571457
</property>
14581458
</widget>
14591459
</item>
1460+
<item>
1461+
<widget class="QLabel" name="mScaleDependencyLabel">
1462+
<property name="text">
1463+
<string>Scale</string>
1464+
</property>
1465+
</widget>
1466+
</item>
1467+
<item>
1468+
<widget class="QComboBox" name="mScaleDependencyComboBox"/>
1469+
</item>
14601470
</layout>
14611471
</item>
14621472
<item row="0" column="2">

0 commit comments

Comments
 (0)