46 , mVectorLayer( layer )
47 , mSourceFieldExp( fieldOrExp )
48 , mXAxisTitle( QObject::tr(
"Value" ) )
49 , mYAxisTitle( QObject::tr(
"Count" ) )
56 QFrame *plotCanvasFrame =
dynamic_cast<QFrame *
>( mpPlot->canvas() );
57 if ( plotCanvasFrame )
58 plotCanvasFrame->setFrameStyle( QFrame::NoFrame );
61 mMeanCheckBox->setChecked( settings.
value( QStringLiteral(
"HistogramWidget/showMean" ),
false ).toBool() );
62 mStdevCheckBox->setChecked( settings.
value( QStringLiteral(
"HistogramWidget/showStdev" ),
false ).toBool() );
64 connect( mBinsSpinBox,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &
QgsHistogramWidget::refresh );
69 mGridPen = QPen( QColor( 0, 0, 0, 40 ) );
70 mMeanPen = QPen( QColor( 10, 10, 10, 220 ) );
71 mMeanPen.setStyle( Qt::DashLine );
72 mStdevPen = QPen( QColor( 30, 30, 30, 200 ) );
73 mStdevPen.setStyle( Qt::DashLine );
75 if (
layer && !mSourceFieldExp.isEmpty() )
176 mpPlot->detachItems();
179 mpPlot->setAutoDelete(
true );
181 if ( !mXAxisTitle.isEmpty() )
182 mpPlot->setAxisTitle( QwtPlot::xBottom, mXAxisTitle );
183 if ( !mYAxisTitle.isEmpty() )
184 mpPlot->setAxisTitle( QwtPlot::yLeft, mYAxisTitle );
185 mpPlot->setAxisFont( QwtPlot::xBottom, this->font() );
186 mpPlot->setAxisFont( QwtPlot::yLeft, this->font() );
187 QFont titleFont = this->font();
188 titleFont.setBold(
true );
189 QwtText xAxisText = mpPlot->axisTitle( QwtPlot::xBottom );
190 xAxisText.setFont( titleFont );
191 mpPlot->setAxisTitle( QwtPlot::xBottom, xAxisText );
192 QwtText yAxisText = mpPlot->axisTitle( QwtPlot::yLeft );
193 yAxisText.setFont( titleFont );
194 mpPlot->setAxisTitle( QwtPlot::yLeft, yAxisText );
195 mpPlot->setAxisAutoScale( QwtPlot::yLeft );
196 mpPlot->setAxisAutoScale( QwtPlot::xBottom );
199 QwtPlotGrid *grid =
new QwtPlotGrid();
200 grid->enableX(
false );
201 grid->setPen( mGridPen );
202 grid->attach( mpPlot );
205 mHistoColors.clear();
208 mHistoColors << ( range.symbol() ? range.symbol()->color() : Qt::black );
212 QwtPlotHistogram *plotHistogram =
nullptr;
213 plotHistogram = createPlotHistogram( !
mRanges.isEmpty() ?
mRanges.at( 0 ).label() : QString(),
214 !
mRanges.isEmpty() ? QBrush( mHistoColors.at( 0 ) ) : mBrush,
215 !
mRanges.isEmpty() ? Qt::NoPen : mPen );
216 QVector<QwtIntervalSample> dataHisto;
218 int bins = mBinsSpinBox->value();
219 QList<double> edges = mHistogram.
binEdges( bins );
220 QList<int> counts = mHistogram.
counts( bins );
225 for (
int bin = 0; bin < bins; ++bin )
227 int binValue = counts.at( bin );
231 if ( rangeIndex <
mRanges.count() - 1 && edges.at( bin ) >
mRanges.at( rangeIndex ).upperValue() )
234 plotHistogram->setSamples( dataHisto );
235 plotHistogram->attach( mpPlot );
236 plotHistogram = createPlotHistogram(
mRanges.at( rangeIndex ).label(), mHistoColors.at( rangeIndex ) );
238 dataHisto << QwtIntervalSample( lastValue,
mRanges.at( rangeIndex - 1 ).upperValue(), edges.at( bin ) );
241 double upperEdge = !
mRanges.isEmpty() ? std::min( edges.at( bin + 1 ),
mRanges.at( rangeIndex ).upperValue() )
242 : edges.at( bin + 1 );
244 dataHisto << QwtIntervalSample( binValue, edges.at( bin ), upperEdge );
246 lastValue = binValue;
249 plotHistogram->setSamples( dataHisto );
250 plotHistogram->attach( mpPlot );
255 QwtPlotMarker *rangeMarker =
new QwtPlotMarker();
256 rangeMarker->attach( mpPlot );
257 rangeMarker->setLineStyle( QwtPlotMarker::VLine );
258 rangeMarker->setXValue( range.upperValue() );
259 rangeMarker->setLabel( QLocale().toString( range.upperValue() ) );
260 rangeMarker->setLabelOrientation( Qt::Vertical );
261 rangeMarker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
266 if ( mMeanCheckBox->isChecked() )
268 QwtPlotMarker *meanMarker =
new QwtPlotMarker();
269 meanMarker->attach( mpPlot );
270 meanMarker->setLineStyle( QwtPlotMarker::VLine );
271 meanMarker->setLinePen( mMeanPen );
272 meanMarker->setXValue( mStats.
mean() );
273 meanMarker->setLabel( QString( QChar( 956 ) ) );
274 meanMarker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
278 if ( mStdevCheckBox->isChecked() )
280 QwtPlotMarker *stdev1Marker =
new QwtPlotMarker();
281 stdev1Marker->attach( mpPlot );
282 stdev1Marker->setLineStyle( QwtPlotMarker::VLine );
283 stdev1Marker->setLinePen( mStdevPen );
284 stdev1Marker->setXValue( mStats.
mean() - mStats.
stDev() );
285 stdev1Marker->setLabel( QString( QChar( 963 ) ) );
286 stdev1Marker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
287 stdev1Marker->show();
289 QwtPlotMarker *stdev2Marker =
new QwtPlotMarker();
290 stdev2Marker->attach( mpPlot );
291 stdev2Marker->setLineStyle( QwtPlotMarker::VLine );
292 stdev2Marker->setLinePen( mStdevPen );
293 stdev2Marker->setXValue( mStats.
mean() + mStats.
stDev() );
294 stdev2Marker->setLabel( QString( QChar( 963 ) ) );
295 stdev2Marker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
296 stdev2Marker->show();
299 mpPlot->setEnabled(
true );