QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsclassificationlogarithmic.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsclassificationlogarithmic.h
3  ---------------------
4  begin : September 2019
5  copyright : (C) 2019 by Denis Rouzaud
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgssymbollayerutils.h"
18 #include "qgsapplication.h"
19 
21  : QgsClassificationMethod( ValuesNotRequired, 0 )
22 {
23 
24 }
25 
26 
28 {
30  copyBase( c );
31  return c;
32 }
33 
35 {
36  return QObject::tr( "Logarithmic scale" );
37 }
38 
40 {
41  return QStringLiteral( "Logarithmic" );
42 }
43 
45 {
46  return QgsApplication::getThemeIcon( "classification_methods/mClassificationLogarithmic.svg" );
47 }
48 
49 QList<double> QgsClassificationLogarithmic::calculateBreaks( double minimum, double maximum, const QList<double> &values, int nclasses )
50 {
51  Q_UNUSED( values );
52 
53  // get the min/max in log10 scale
54  int lmin = std::floor( std::log10( minimum ) );
55  int lmax = std::ceil( std::log10( maximum ) );
56 
57  // do not create too many classes
58  nclasses = std::min( lmax - lmin + 1, nclasses );
59 
60  // calculate pretty breaks
61  QList<double> breaks = QgsSymbolLayerUtils::prettyBreaks( lmin, lmax, nclasses );
62 
63  // create the value
64  for ( int i = 0; i < breaks.count(); i++ )
65  breaks[i] = std::pow( 10, breaks.at( i ) );
66 
67  return breaks;
68 }
69 
70 QString QgsClassificationLogarithmic::valueToLabel( double value ) const
71 {
72  QString label = QString( QStringLiteral( "10^%1" ) ).arg( std::log10( value ) );
73  return label;
74 }
75 
76 QString QgsClassificationLogarithmic::labelForRange( double lowerValue, double upperValue, QgsClassificationMethod::ClassPosition position ) const
77 {
78  QString lowerLabel;
79  const QString upperLabel = valueToLabel( upperValue );
80 
81  switch ( position )
82  {
83  case LowerBound:
84  lowerLabel = formatNumber( lowerValue ); // avoid to have float exponent for the minimum value
85  break;
86  case Inner:
87  case UpperBound:
88  lowerLabel = valueToLabel( lowerValue );
89  break;
90  }
91 
92  return labelFormat().arg( lowerLabel ).arg( upperLabel );
93 }
94 
QString formatNumber(double value) const
Format the number according to label properties.
QString id() const override
The id of the method as saved in the project, must be unique in registry.
QIcon icon() const override
The icon of the method.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
The class is not at a bound.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
The class is at the upper bound.
QString labelForRange(double lowerValue, double upperValue, ClassPosition position) const override
Returns the label for a range.
The class is at the lower bound.
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
QString labelFormat() const
Returns the format of the label for the classes.
QgsClassificationMethod * clone() const override
Returns a clone of the method.
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about &#39;classes&#39; equally spaced round values which cover the range of values fr...
QString name() const override
The readable and translate name of the method.
ClassPosition
Defines the class position.
QgsClassificationMethod is an abstract class for implementations of classification methods...
Implementation of a logarithmic scale method.