QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgshistogram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshistogram.cpp
3  ----------------
4  begin : May 2015
5  copyright : (C) 2015 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgshistogram.h"
19 
20 #include "qgsstatisticalsummary.h"
21 #include "qgsvectorlayer.h"
22 #include <qmath.h>
23 
25  : mMax( 0 )
26  , mMin( 0 )
27  , mIQR( 0 )
28 {
29 
30 }
31 
33 {
34 
35 }
36 
37 void QgsHistogram::prepareValues()
38 {
39  qSort( mValues.begin(), mValues.end() );
40 
43  s.calculate( mValues );
44  mMin = s.min();
45  mMax = s.max();
46  mIQR = s.interQuartileRange();
47 }
48 
50 {
51  mValues = values;
52  prepareValues();
53 }
54 
55 bool QgsHistogram::setValues( QgsVectorLayer *layer, const QString &fieldOrExpression )
56 {
57  mValues.clear();
58  if ( !layer )
59  return false;
60 
61  bool ok;
62  mValues = layer->getDoubleValues( fieldOrExpression, ok );
63  if ( !ok )
64  return false;
65 
66  prepareValues();
67  return true;
68 }
69 
71 {
72  //Freedman-Diaconis rule
73  return 2.0 * mIQR * qPow( mValues.count(), -1 / 3.0 );
74 }
75 
77 {
78  return ceil(( mMax - mMin ) / optimalBinWidth() );
79 }
80 
82 {
83  double binWidth = ( mMax - mMin ) / bins;
84 
85  QList<double> edges;
86  edges.reserve( bins + 1 );
87  edges << mMin;
88  double current = mMin;
89  for ( int i = 0; i < bins; ++i )
90  {
91  current += binWidth;
92  edges << current;
93  }
94  return edges;
95 }
96 
98 {
99  QList<double> edges = binEdges( bins );
100 
101  QList<int> binCounts;
102  binCounts.reserve( bins );
103  int currentValueIndex = 0;
104  for ( int i = 0; i < bins; ++i )
105  {
106  int count = 0;
107  while ( currentValueIndex < mValues.count() && mValues.at( currentValueIndex ) < edges.at( i + 1 ) )
108  {
109  count++;
110  currentValueIndex++;
111  if ( currentValueIndex >= mValues.count() )
112  break;
113  }
114  binCounts << count;
115  }
116 
117  if ( currentValueIndex < mValues.count() )
118  {
119  //last value needs to be added
120  binCounts[ bins - 1 ] = binCounts.last() + 1;
121  }
122 
123  return binCounts;
124 }
125 
126 
void clear()
virtual ~QgsHistogram()
QList< double > binEdges(int bins) const
Returns a list of edges for the histogram for a specified number of bins.
void reserve(int alloc)
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.
int count(const T &value) const
iterator end()
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.
T & last()
Calculator for summary statistics for a list of doubles.
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...
iterator begin()
double optimalBinWidth() const
Calculates the optimal bin width using the Freedman-Diaconis rule.