Skip to content
Permalink
Browse files

Add histogram

  • Loading branch information
m-kuhn committed Aug 9, 2012
1 parent 20f97d2 commit 2c6b15210cdbe64924b64ab9c4e2275e88e17c4b
@@ -824,6 +824,10 @@ void QgsVectorLayerProperties::apply()
{
diagram = new QgsPieDiagram();
}
else if ( mDiagramTypeComboBox->currentText() == tr( "Histogram" ) )
{
diagram = new QgsHistogramDiagram();
}

QgsDiagramSettings ds;
ds.font = mDiagramFont;
@@ -857,11 +861,11 @@ void QgsVectorLayerProperties::apply()

if ( mIncreaseSmallDiagramsCheckBox->isChecked() )
{
ds.mMinimumSize = mIncreaseMinimumSizeSpinBox->value();
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
}
else
{
ds.mMinimumSize = 0;
ds.minimumSize = 0;
}

ds.backgroundColor = mBackgroundColorButton->color();
@@ -878,6 +882,24 @@ void QgsVectorLayerProperties::apply()
ds.maxScaleDenominator = -1;
}

// Diagram orientation (histogram)
if ( tr( "Up" ) == mOrientationButtonGroup->checkedButton()->text() )
{
ds.diagramOrientation = QgsDiagramSettings::Up;
}
else if ( tr( "Down" ) == mOrientationButtonGroup->checkedButton()->text() )
{
ds.diagramOrientation = QgsDiagramSettings::Down;
}
else if ( tr( "Right" ) == mOrientationButtonGroup->checkedButton()->text() )
{
ds.diagramOrientation = QgsDiagramSettings::Right;
}
else if ( tr( "Left" ) == mOrientationButtonGroup->checkedButton()->text() )
{
ds.diagramOrientation = QgsDiagramSettings::Left;
}

if ( mFixedSizeCheckBox->isChecked() )
{
QgsSingleCategoryDiagramRenderer* dr = new QgsSingleCategoryDiagramRenderer();
@@ -1500,8 +1522,8 @@ void QgsVectorLayerProperties::on_mFixedSizeCheckBox_stateChanged( int state )
//enable / disable all widget in the scaling layout
mLinearlyScalingLabel->setEnabled( state != Qt::Checked );
mIncreaseSmallDiagramsCheckBox->setEnabled( state != Qt::Checked );
mIncreaseMinimumSizeLabel->setEnabled( state != Qt::Checked && mIncreaseSmallDiagramsCheckBox->isChecked() == Qt::Checked );
mIncreaseMinimumSizeSpinBox->setEnabled( state != Qt::Checked && mIncreaseSmallDiagramsCheckBox->isChecked() == Qt::Checked );
mIncreaseMinimumSizeLabel->setEnabled( state != Qt::Checked && mIncreaseSmallDiagramsCheckBox->isChecked() );
mIncreaseMinimumSizeSpinBox->setEnabled( state != Qt::Checked && mIncreaseSmallDiagramsCheckBox->isChecked() );
QWidget* currentWidget = 0;
for ( int i = 0; i < mLinearlyScalingLayout->count(); ++i )
{
@@ -1614,6 +1636,7 @@ void QgsVectorLayerProperties::initDiagramTab()

mDiagramTypeComboBox->addItem( tr( "Pie chart" ) );
mDiagramTypeComboBox->addItem( tr( "Text diagram" ) );
mDiagramTypeComboBox->addItem( tr( "Histogram" ) );

mLabelPlacementComboBox->addItem( tr( "Height" ) );
mLabelPlacementComboBox->addItem( tr( "x-height" ) );
@@ -1708,8 +1731,28 @@ void QgsVectorLayerProperties::initDiagramTab()
mLabelPlacementComboBox->setCurrentIndex( 1 );
}

mIncreaseSmallDiagramsCheckBox->setChecked( settingList.at( 0 ).mMinimumSize != 0 );
mIncreaseMinimumSizeSpinBox->setValue( settingList.at( 0 ).mMinimumSize );
switch( settingList.at( 0 ).diagramOrientation )
{
case QgsDiagramSettings::Left:
mOrientationLeftButton->setChecked( true );
break;

case QgsDiagramSettings::Right:
mOrientationRightButton->setChecked( true );
break;

case QgsDiagramSettings::Up:
mOrientationUpButton->setChecked( true );
break;

case QgsDiagramSettings::Down:
mOrientationDownButton->setChecked( true );
break;

}

mIncreaseSmallDiagramsCheckBox->setChecked( settingList.at( 0 ).minimumSize != 0 );
mIncreaseMinimumSizeSpinBox->setValue( settingList.at( 0 ).minimumSize );

QList< QColor > categoryColors = settingList.at( 0 ).categoryColors;
QList< int > categoryIndices = settingList.at( 0 ).categoryIndices;
@@ -1758,6 +1801,10 @@ void QgsVectorLayerProperties::initDiagramTab()
{
mDiagramTypeComboBox->setCurrentIndex( mDiagramTypeComboBox->findText( tr( "Pie chart" ) ) );
}
else if ( diagramName == "Histogram" )
{
mDiagramTypeComboBox->setCurrentIndex( mDiagramTypeComboBox->findText( tr( "Histogram" ) ) );
}
}
}

@@ -43,6 +43,18 @@ QSizeF QgsDiagram::sizePainterUnits( const QSizeF& size, const QgsDiagramSetting
}
}

float QgsDiagram::sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
return l * c.scaleFactor();
}
else
{
return l / c.mapToPixel().mapUnitsPerPixel();
}
}

QFont QgsDiagram::scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c )
{
QFont f = s.font;
@@ -311,3 +323,73 @@ void QgsPieDiagram::renderDiagram( const QgsAttributeMap& att, QgsRenderContext&
totalAngle += currentAngle;
}
}

QgsHistogramDiagram::QgsHistogramDiagram()
{
mCategoryBrush.setStyle( Qt::SolidPattern );
mPen.setStyle( Qt::SolidLine );
}

QgsHistogramDiagram::~QgsHistogramDiagram()
{
}

void QgsHistogramDiagram::renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
{
QPainter* p = c.painter();
if ( !p )
{
return;
}

QList<double> values;

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

double currentOffset = 0;

double baseX = position.x();
double baseY = position.y();

mPen.setColor( s.penColor );
setPenWidth( mPen, s, c );
p->setPen( mPen );

p->drawPoint( baseX, baseY );

QList<double>::const_iterator valIt = values.constBegin();
QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
{
double length = sizePainterUnits( *valIt, s, c );

mCategoryBrush.setColor( *colIt );
p->setBrush( mCategoryBrush );

switch ( s.diagramOrientation )
{
case QgsDiagramSettings::Up:
p->drawRect( baseX + currentOffset, baseY, 10, 0 - length );
break;

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

case QgsDiagramSettings::Right:
p->drawRect( baseX, baseY + currentOffset, 0 - length, 10 );
break;

case QgsDiagramSettings::Left:
p->drawRect( baseX, baseY + currentOffset, length, 10 );
break;
}

currentOffset += 10;
}
}
@@ -37,6 +37,7 @@ class CORE_EXPORT QgsDiagram
protected:
void setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c );
QSizeF sizePainterUnits( const QSizeF& size, const QgsDiagramSettings& s, const QgsRenderContext& c );
float sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c );
QFont scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c );
};

@@ -87,4 +88,19 @@ class CORE_EXPORT QgsPieDiagram: public QgsDiagram
QPen mPen;
};

class CORE_EXPORT QgsHistogramDiagram: public QgsDiagram
{
public:
QgsHistogramDiagram();
~QgsHistogramDiagram();

void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
QString diagramName() const { return "Histogram"; }

private:
QBrush mCategoryBrush;
QPen mPen;
};


#endif // QGSDIAGRAM_H
@@ -75,6 +75,24 @@ void QgsDiagramSettings::readXML( const QDomElement& elem )
labelPlacementMethod = XHeight;
}

// orientation
if ( elem.attribute( "diagramOrientation" ) == "Left" )
{
diagramOrientation = Left;
}
else if ( elem.attribute( "diagramOrientation" ) == "Right" )
{
diagramOrientation = Right;
}
else if ( elem.attribute( "diagramOrientation" ) == "Down" )
{
diagramOrientation = Down;
}
else
{
diagramOrientation = Up;
}

minimumSize = elem.attribute( "minimumSize" ).toDouble();

//colors
@@ -108,6 +126,8 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc
categoryElem.setAttribute( "penWidth", QString::number( penWidth ) );
categoryElem.setAttribute( "minScaleDenominator", QString::number( minScaleDenominator ) );
categoryElem.setAttribute( "maxScaleDenominator", QString::number( maxScaleDenominator ) );

// site type (mm vs. map units)
if ( sizeType == MM )
{
categoryElem.setAttribute( "sizeType", "MM" );
@@ -117,6 +137,7 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc
categoryElem.setAttribute( "sizeType", "MapUnits" );
}

// label placement method (text diagram)
if ( labelPlacementMethod == Height )
{
categoryElem.setAttribute( "labelPlacementMethod", "Height" );
@@ -126,6 +147,29 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc
categoryElem.setAttribute( "labelPlacementMethod", "XHeight" );
}

// orientation (histogram)
switch ( diagramOrientation ) {
case Left:
categoryElem.setAttribute( "diagramOrientation", "Left" );
break;

case Right:
categoryElem.setAttribute( "diagramOrientation", "Right" );
break;

case Down:
categoryElem.setAttribute( "diagramOrientation", "Down" );
break;

case Up:
categoryElem.setAttribute( "diagramOrientation", "Up" );
break;

default:
categoryElem.setAttribute( "diagramOrientation", "Up" );
break;
}

categoryElem.setAttribute( "minimumSize", QString::number( minimumSize ) );

QString colors;
@@ -243,6 +287,10 @@ void QgsDiagramRendererV2::_readXML( const QDomElement& elem )
{
mDiagram = new QgsTextDiagram();
}
else if ( diagramType == "Histogram" )
{
mDiagram = new QgsHistogramDiagram();
}
else
{
mDiagram = 0;
@@ -102,6 +102,15 @@ struct CORE_EXPORT QgsDiagramSettings
XHeight
};

//! Orientation of histogram
enum DiagramOrientation
{
Up,
Down,
Left,
Right
};

QgsDiagramSettings(): sizeType( MM ), minScaleDenominator( -1 ), maxScaleDenominator( -1 )
{}
QFont font;
@@ -113,6 +122,7 @@ struct CORE_EXPORT QgsDiagramSettings
QColor penColor;
double penWidth;
LabelPlacementMethod labelPlacementMethod;
DiagramOrientation diagramOrientation;

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

0 comments on commit 2c6b152

Please sign in to comment.
You can’t perform that action at this time.