Skip to content

Commit

Permalink
Add option to scale by area/scale by diameter for PieChart and Text D…
Browse files Browse the repository at this point in the history
…iagram
  • Loading branch information
m-kuhn committed Aug 10, 2012
1 parent 2c9a3a7 commit 468650b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
23 changes: 23 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -862,6 +862,15 @@ void QgsVectorLayerProperties::apply()
ds.labelPlacementMethod = QgsDiagramSettings::XHeight;
}

if ( tr( "Area" ) == mScaleDependencyComboBox->currentText() )
{
ds.scaleByArea = true;
}
else
{
ds.scaleByArea = false;
}

if ( mIncreaseSmallDiagramsCheckBox->isChecked() )
{
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
Expand Down Expand Up @@ -1374,6 +1383,17 @@ void QgsVectorLayerProperties::handleDiagramTypeChanged( const QString& itemtext
mDiagramOptionsGroupBox->hide();
mBarWidthLabel->hide();
}

if ( tr( "Text diagram" ) == itemtext || tr( "Pie chart" ) == itemtext )
{
mScaleDependencyComboBox->show();
mScaleDependencyLabel->show();
}
else
{
mScaleDependencyComboBox->hide();
mScaleDependencyLabel->hide();
}
}

void QgsVectorLayerProperties::on_mIncreaseSmallDiagramsCheckBox_stateChanged( int state )
Expand Down Expand Up @@ -1672,6 +1692,9 @@ void QgsVectorLayerProperties::initDiagramTab()
mLabelPlacementComboBox->addItem( tr( "Height" ) );
mLabelPlacementComboBox->addItem( tr( "x-height" ) );

mScaleDependencyComboBox->addItem( tr( "Area" ) );
mScaleDependencyComboBox->addItem( tr( "Diameter" ) );

//insert all attributes into the combo boxes
const QgsFieldMap& layerFields = layer->pendingFields();
QgsFieldMap::const_iterator fieldIt = layerFields.constBegin();
Expand Down
4 changes: 1 addition & 3 deletions src/core/diagram/qgspiediagram.cpp
Expand Up @@ -36,8 +36,6 @@ QSizeF QgsPieDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsR
{
return QSizeF(); //zero size if attribute is missing
}

bool scaleByArea = true;

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

// interpolate the squared value if scale by area
if ( scaleByArea )
if ( s.scaleByArea )
{
scaledValue = sqrt( scaledValue );
scaledLowerValue = sqrt( scaledLowerValue );
Expand Down
30 changes: 25 additions & 5 deletions src/core/diagram/qgstextdiagram.cpp
Expand Up @@ -32,17 +32,37 @@ QgsTextDiagram::~QgsTextDiagram()

QSizeF QgsTextDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
{
QgsAttributeMap::const_iterator attIt = attributes.find( is.classificationAttribute );
QgsAttributeMap::const_iterator attIt = attributes.find( is.classificationAttribute );
if ( attIt == attributes.constEnd() )
{
return QSizeF(); //zero size if attribute is missing
}
double value = attIt.value().toDouble();

double scaledValue = attIt.value().toDouble();
double scaledLowerValue = is.lowerValue;
double scaledUpperValue = is.upperValue;
double scaledLowerSizeWidth = is.lowerSize.width();
double scaledLowerSizeHeight = is.lowerSize.height();
double scaledUpperSizeWidth = is.upperSize.width();
double scaledUpperSizeHeight = is.upperSize.height();

// interpolate the squared value if scale by area
if ( s.scaleByArea )
{
scaledValue = sqrt( scaledValue );
scaledLowerValue = sqrt( scaledLowerValue );
scaledUpperValue = sqrt( scaledUpperValue );
scaledLowerSizeWidth = sqrt( scaledLowerSizeWidth );
scaledLowerSizeHeight = sqrt( scaledLowerSizeHeight );
scaledUpperSizeWidth = sqrt( scaledUpperSizeWidth );
scaledUpperSizeHeight = sqrt( scaledUpperSizeHeight );
}

//interpolate size
double ratio = ( value - is.lowerValue ) / ( is.upperValue - is.lowerValue );
QSizeF size = QSizeF( sqrt( is.upperSize.width() * ratio + is.lowerSize.width() * ( 1 - ratio ) ),
sqrt( is.upperSize.height() * ratio + is.lowerSize.height() * ( 1 - ratio ) ) );
double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );

QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );

// Scale, if extension is smaller than the specified minimum
if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsdiagramrendererv2.h
Expand Up @@ -124,6 +124,7 @@ struct CORE_EXPORT QgsDiagramSettings
LabelPlacementMethod labelPlacementMethod;
DiagramOrientation diagramOrientation;
double barWidth;
bool scaleByArea;

//scale range (-1 if no lower / upper bound )
double minScaleDenominator;
Expand Down
10 changes: 10 additions & 0 deletions src/ui/qgsvectorlayerpropertiesbase.ui
Expand Up @@ -1457,6 +1457,16 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="mScaleDependencyLabel">
<property name="text">
<string>Scale</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mScaleDependencyComboBox"/>
</item>
</layout>
</item>
<item row="0" column="2">
Expand Down

0 comments on commit 468650b

Please sign in to comment.