QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 << mMin;
87  double current = mMin;
88  for ( int i = 0; i < bins; ++i )
89  {
90  current += binWidth;
91  edges << current;
92  }
93  return edges;
94 }
95 
97 {
98  QList<double> edges = binEdges( bins );
99 
100  QList<int> binCounts;
101  binCounts.reserve( bins );
102  int currentValueIndex = 0;
103  for ( int i = 0; i < bins; ++i )
104  {
105  int count = 0;
106  while ( currentValueIndex < mValues.count() && mValues.at( currentValueIndex ) < edges.at( i + 1 ) )
107  {
108  count++;
109  currentValueIndex++;
110  if ( currentValueIndex >= mValues.count() )
111  break;
112  }
113  binCounts << count;
114  }
115 
116  if ( currentValueIndex < mValues.count() )
117  {
118  //last value needs to be added
119  binCounts[ bins - 1 ] = binCounts.last() + 1;
120  }
121 
122  return binCounts;
123 }
124 
125 
void clear()
virtual ~QgsHistogram()
void reserve(int alloc)
const T & at(int i) const
int count(const T &value) const
iterator end()
void setValues(const QList< double > &values)
Assigns numeric source values for the histogram.
QList< double > getDoubleValues(const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, int *nullCount=0)
Fetches all double values from a specified field name or expression.
int optimalNumberBins() const
Returns the optimal number of bins for the source values, calculated using the Freedman-Diaconis rule...
QList< int > counts(int bins) const
Returns the calculated list of the counts for the histogram bins.
double optimalBinWidth() const
Calculates the optimal bin width using the Freedman-Diaconis rule.
T & last()
Calculator for summary statistics for a list of doubles.
Represents a vector layer which manages a vector based data sets.
void setStatistics(Statistics stats)
Sets flags which specify which statistics will be calculated.
iterator begin()
QList< double > binEdges(int bins) const
Returns a list of edges for the histogram for a specified number of bins.