25 #include <QMouseEvent> 28 #include <qwt_global.h> 29 #include <qwt_plot_canvas.h> 31 #include <qwt_plot_curve.h> 32 #include <qwt_plot_grid.h> 33 #include <qwt_plot_marker.h> 34 #include <qwt_plot_picker.h> 35 #include <qwt_picker_machine.h> 36 #include <qwt_plot_layout.h> 37 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 38 #include <qwt_plot_renderer.h> 39 #include <qwt_plot_histogram.h> 41 #include "../raster/qwt5_histogram_item.h" 47 , mVectorLayer( layer )
48 , mSourceFieldExp( fieldOrExp )
49 , mXAxisTitle(
QObject::tr(
"Value" ) )
50 , mYAxisTitle(
QObject::tr(
"Count" ) )
57 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 58 QFrame* plotCanvasFrame =
dynamic_cast<QFrame*
>( mpPlot->canvas() );
59 if ( plotCanvasFrame )
62 mpPlot->canvas()->setFrameStyle( QFrame::NoFrame );
66 mMeanCheckBox->setChecked( settings.
value(
"/HistogramWidget/showMean",
false ).
toBool() );
67 mStdevCheckBox->setChecked( settings.
value(
"/HistogramWidget/showStdev",
false ).
toBool() );
69 connect( mBinsSpinBox, SIGNAL( valueChanged(
int ) ),
this, SLOT(
refresh() ) );
70 connect( mMeanCheckBox, SIGNAL( toggled(
bool ) ),
this, SLOT(
refresh() ) );
71 connect( mStdevCheckBox, SIGNAL( toggled(
bool ) ),
this, SLOT(
refresh() ) );
80 if ( layer && !mSourceFieldExp.
isEmpty() )
89 settings.
setValue(
"/HistogramWidget/showMean", mMeanCheckBox->isChecked() );
90 settings.
setValue(
"/HistogramWidget/showStdev", mStdevCheckBox->isChecked() );
108 if ( !mVectorLayer || mSourceFieldExp.
isEmpty() )
123 qSort( mValues.
begin(), mValues.
end() );
125 mBinsSpinBox->blockSignals(
true );
127 mBinsSpinBox->blockSignals(
false );
132 mpPlot->setEnabled(
true );
133 mMeanCheckBox->setEnabled(
true );
134 mStdevCheckBox->setEnabled(
true );
135 mBinsSpinBox->setEnabled(
true );
150 if ( layer == mVectorLayer )
153 mVectorLayer =
layer;
157 void QgsHistogramWidget::clearHistogram()
163 mpPlot->setEnabled(
false );
164 mMeanCheckBox->setEnabled(
false );
165 mStdevCheckBox->setEnabled(
false );
166 mBinsSpinBox->setEnabled(
false );
171 if ( fieldOrExp == mSourceFieldExp )
174 mSourceFieldExp = fieldOrExp;
181 mpPlot->detachItems();
184 mpPlot->setAutoDelete(
true );
187 mpPlot->setAxisTitle( QwtPlot::xBottom, mXAxisTitle );
189 mpPlot->setAxisTitle( QwtPlot::yLeft, mYAxisTitle );
190 mpPlot->setAxisFont( QwtPlot::xBottom, this->
font() );
191 mpPlot->setAxisFont( QwtPlot::yLeft, this->
font() );
194 QwtText xAxisText = mpPlot->axisTitle( QwtPlot::xBottom );
195 xAxisText.setFont( titleFont );
196 mpPlot->setAxisTitle( QwtPlot::xBottom, xAxisText );
197 QwtText yAxisText = mpPlot->axisTitle( QwtPlot::yLeft );
198 yAxisText.setFont( titleFont );
199 mpPlot->setAxisTitle( QwtPlot::yLeft, yAxisText );
200 mpPlot->setAxisAutoScale( QwtPlot::yLeft );
201 mpPlot->setAxisAutoScale( QwtPlot::xBottom );
204 QwtPlotGrid * grid =
new QwtPlotGrid();
205 grid->enableX(
false );
206 grid->setPen( mGridPen );
207 grid->attach( mpPlot );
210 mHistoColors.
clear();
217 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 218 QwtPlotHistogram *plotHistogram =
nullptr;
224 HistogramItem *plotHistogramItem =
nullptr;
229 QwtArray<QwtDoubleInterval> intervalsHisto;
230 QwtArray<double> valuesHisto;
233 int bins = mBinsSpinBox->value();
240 for (
int bin = 0; bin < bins; ++bin )
242 int binValue = counts.
at( bin );
249 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 250 plotHistogram->setSamples( dataHisto );
251 plotHistogram->attach( mpPlot );
252 plotHistogram = createPlotHistogram(
mRanges.
at( rangeIndex ).label(), mHistoColors.
at( rangeIndex ) );
254 dataHisto << QwtIntervalSample( lastValue,
mRanges.
at( rangeIndex - 1 ).upperValue(), edges.
at( bin ) );
256 plotHistogramItem->setData( QwtIntervalData( intervalsHisto, valuesHisto ) );
257 plotHistogramItem->attach( mpPlot );
258 plotHistogramItem = createHistoItem(
mRanges.
at( rangeIndex ).label(), mHistoColors.
at( rangeIndex ) );
259 intervalsHisto.clear();
261 intervalsHisto.append( QwtDoubleInterval(
mRanges.
at( rangeIndex - 1 ).upperValue(), edges.
at( bin ) ) );
262 valuesHisto.append( lastValue );
267 : edges.
at( bin + 1 );
269 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 270 dataHisto << QwtIntervalSample( binValue, edges.
at( bin ), upperEdge );
272 intervalsHisto.append( QwtDoubleInterval( edges.
at( bin ), upperEdge ) );
273 valuesHisto.append(
double( binValue ) );
276 lastValue = binValue;
279 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 280 plotHistogram->setSamples( dataHisto );
281 plotHistogram->attach( mpPlot );
283 plotHistogramItem->setData( QwtIntervalData( intervalsHisto, valuesHisto ) );
284 plotHistogramItem->attach( mpPlot );
290 QwtPlotMarker* rangeMarker =
new QwtPlotMarker();
291 rangeMarker->attach( mpPlot );
292 rangeMarker->setLineStyle( QwtPlotMarker::VLine );
295 rangeMarker->setLabelOrientation( Qt::Vertical );
296 rangeMarker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
301 if ( mMeanCheckBox->isChecked() )
303 QwtPlotMarker* meanMarker =
new QwtPlotMarker();
304 meanMarker->attach( mpPlot );
305 meanMarker->setLineStyle( QwtPlotMarker::VLine );
306 meanMarker->setLinePen( mMeanPen );
307 meanMarker->setXValue( mStats.
mean() );
309 meanMarker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
313 if ( mStdevCheckBox->isChecked() )
315 QwtPlotMarker* stdev1Marker =
new QwtPlotMarker();
316 stdev1Marker->attach( mpPlot );
317 stdev1Marker->setLineStyle( QwtPlotMarker::VLine );
318 stdev1Marker->setLinePen( mStdevPen );
319 stdev1Marker->setXValue( mStats.
mean() - mStats.
stDev() );
321 stdev1Marker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
322 stdev1Marker->show();
324 QwtPlotMarker* stdev2Marker =
new QwtPlotMarker();
325 stdev2Marker->attach( mpPlot );
326 stdev2Marker->setLineStyle( QwtPlotMarker::VLine );
327 stdev2Marker->setLinePen( mStdevPen );
328 stdev2Marker->setXValue( mStats.
mean() + mStats.
stDev() );
330 stdev2Marker->setLabelAlignment( Qt::AlignLeft | Qt::AlignTop );
331 stdev2Marker->show();
334 mpPlot->setEnabled(
true );
338 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 339 QwtPlotHistogram* QgsHistogramWidget::createPlotHistogram(
const QString& title,
const QBrush&
brush,
const QPen&
pen )
const 341 QwtPlotHistogram* histogram =
new QwtPlotHistogram( title );
342 histogram->setBrush( brush );
343 if ( pen != Qt::NoPen )
345 histogram->setPen( pen );
353 histogram->setPen( p );
357 histogram->setPen(
QPen( Qt::NoPen ) );
362 HistogramItem * QgsHistogramWidget::createHistoItem(
const QString& title,
const QBrush& brush,
const QPen& pen )
const 364 HistogramItem* item =
new HistogramItem( title );
365 item->setColor( brush.
color() );
366 item->setFlat(
true );
367 item->setSpacing( 0 );
368 if ( pen != Qt::NoPen )
void setStyle(Qt::PenStyle style)
QColor darker(int factor) const
QList< double > binEdges(int bins) const
Returns a list of edges for the histogram for a specified number of bins.
QgsSymbolV2 * symbol() const
const T & at(int i) const
QList< double > getDoubleValues(const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, int *nullCount=nullptr)
Fetches all double values from a specified field name or expression.
void setStatistics(const Statistics &stats)
Sets flags which specify which statistics will be calculated.
void setFrameStyle(int style)
double stDev() const
Returns population standard deviation.
const QColor & color() const
void setBold(bool enable)
void setValue(const QString &key, const QVariant &value)
QString number(int n, int base)
int count(const T &value) const
void setCosmetic(bool cosmetic)
void setOverrideCursor(const QCursor &cursor)
void restoreOverrideCursor()
void setColor(const QColor &color)
double mean() const
Returns calculated mean of values.
void setValues(const QList< double > &values)
Assigns numeric source values for the histogram.
QList< int > counts(int bins) const
Returns the calculated list of the counts for the histogram bins.
const T & at(int i) const
QVariant value(const QString &key, const QVariant &defaultValue) const
void calculate(const QList< double > &values)
Calculates summary statistics for a list of values.
Standard deviation of values.
double lowerValue() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
int optimalNumberBins() const
Returns the optimal number of bins for the source values, calculated using the Freedman-Diaconis rule...
double upperValue() const