• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • plotting
kplotaxis.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2  This file is part of the KDE libraries
3  Copyright (C) 2005 Andreas Nicolai <Andreas.Nicolai@gmx.net>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kplotaxis.h"
22 
23 #include <math.h> //for log10(), pow(), modf()
24 #include <kdebug.h>
25 
26 class KPlotAxis::Private
27 {
28  public:
29  Private( KPlotAxis *qq )
30  : q( qq )
31  , m_visible( true )
32  , m_showTickLabels( false )
33  , m_labelFmt( 'g' )
34  , m_labelFieldWidth( 0 )
35  , m_labelPrec( -1 )
36  {
37  }
38 
39  KPlotAxis *q;
40 
41  bool m_visible : 1; // Property "visible" defines if Axis is drawn or not.
42  bool m_showTickLabels : 1;
43  char m_labelFmt; // Number format for number labels, see QString::arg()
44  QString m_label; // The label of the axis.
45  int m_labelFieldWidth; // Field width for number labels, see QString::arg()
46  int m_labelPrec; // Number precision for number labels, see QString::arg()
47  QList<double> m_MajorTickMarks, m_MinorTickMarks;
48 };
49 
50 KPlotAxis::KPlotAxis( const QString &label )
51  : d( new Private( this ) )
52 {
53  d->m_label = label;
54 }
55 
56 KPlotAxis::~KPlotAxis()
57 {
58  delete d;
59 }
60 
61 bool KPlotAxis::isVisible() const
62 {
63  return d->m_visible;
64 }
65 
66 void KPlotAxis::setVisible( bool visible )
67 {
68  d->m_visible = visible;
69 }
70 
71 bool KPlotAxis::areTickLabelsShown() const
72 {
73  return d->m_showTickLabels;
74 }
75 
76 void KPlotAxis::setTickLabelsShown( bool b )
77 {
78  d->m_showTickLabels = b;
79 }
80 
81 void KPlotAxis::setLabel( const QString& label )
82 {
83  d->m_label = label;
84 }
85 
86 QString KPlotAxis::label() const
87 {
88  return d->m_label;
89 }
90 
91 void KPlotAxis::setTickLabelFormat( char format, int fieldWidth, int precision )
92 {
93  d->m_labelFieldWidth = fieldWidth;
94  d->m_labelFmt = format;
95  d->m_labelPrec = precision;
96 }
97 
98 int KPlotAxis::tickLabelWidth() const
99 {
100  return d->m_labelFieldWidth;
101 }
102 
103 char KPlotAxis::tickLabelFormat() const
104 {
105  return d->m_labelFmt;
106 }
107 
108 int KPlotAxis::tickLabelPrecision() const
109 {
110  return d->m_labelPrec;
111 }
112 
113 void KPlotAxis::setTickMarks( double x0, double length ) {
114  d->m_MajorTickMarks.clear();
115  d->m_MinorTickMarks.clear();
116 
117  //s is the power-of-ten factor of length:
118  //length = t * s; s = 10^(pwr). e.g., length=350.0 then t=3.5, s = 100.0; pwr = 2.0
119  double pwr = 0.0;
120  modf( log10( length ), &pwr );
121  double s = pow( 10.0, pwr );
122  double t = length / s;
123 
124  double TickDistance = 0.0; //The distance between major tickmarks
125  int NumMajorTicks = 0; //will be between 3 and 5
126  int NumMinorTicks = 0; //The number of minor ticks between major ticks (will be 4 or 5)
127 
128  //adjust s and t such that t is between 3 and 5:
129  if ( t < 3.0 ) {
130  t *= 10.0;
131  s /= 10.0;
132  // t is now between 3 and 30
133  }
134 
135  if ( t < 6.0 ) { //accept current values
136  TickDistance = s;
137  NumMajorTicks = int( t );
138  NumMinorTicks = 5;
139  } else if ( t < 10.0 ) { // adjust by a factor of 2
140  TickDistance = s * 2.0;
141  NumMajorTicks = int( t / 2.0 );
142  NumMinorTicks = 4;
143  } else if ( t < 20.0 ) { //adjust by a factor of 4
144  TickDistance = s * 4.0;
145  NumMajorTicks = int( t / 4.0 );
146  NumMinorTicks = 4;
147  } else { //adjust by a factor of 5
148  TickDistance = s * 5.0;
149  NumMajorTicks = int( t / 5.0 );
150  NumMinorTicks = 5;
151  }
152 
153  //We have determined the number of tickmarks and their separation
154  //Now we determine their positions in the Data space.
155 
156  //Tick0 is the position of a "virtual" tickmark; the first major tickmark
157  //position beyond the "minimum" edge of the data range.
158  double Tick0 = x0 - fmod( x0, TickDistance );
159  if ( x0 < 0.0 ) {
160  Tick0 -= TickDistance;
161  NumMajorTicks++;
162  }
163 
164  for ( int i=0; i<NumMajorTicks+1; i++ ) {
165  double xmaj = Tick0 + i*TickDistance;
166  if ( xmaj >= x0 && xmaj <= x0 + length ) {
167  d->m_MajorTickMarks.append( xmaj );
168  }
169 
170  for ( int j=1; j<NumMinorTicks; j++ ) {
171  double xmin = xmaj + TickDistance*j/NumMinorTicks;
172  if ( xmin >= x0 && xmin <= x0 + length )
173  d->m_MinorTickMarks.append( xmin );
174  }
175  }
176 }
177 
178 QString KPlotAxis::tickLabel( double val ) const {
179  if ( d->m_labelFmt == 't' ) {
180  while ( val < 0.0 ) val += 24.0;
181  while ( val >= 24.0 ) val -= 24.0;
182 
183  int h = int(val);
184  int m = int( 60.*(val - h) );
185  return QString( "%1:%2" ).arg( h, 2, 10, QLatin1Char('0') ).arg( m, 2, 10, QLatin1Char('0') );
186  }
187 
188  return QString( "%1" ).arg( val, d->m_labelFieldWidth, d->m_labelFmt, d->m_labelPrec );
189 }
190 
191 QList< double > KPlotAxis::majorTickMarks() const
192 {
193  return d->m_MajorTickMarks;
194 }
195 
196 QList< double > KPlotAxis::minorTickMarks() const
197 {
198  return d->m_MinorTickMarks;
199 }
200 
KPlotAxis::~KPlotAxis
~KPlotAxis()
Destructor.
Definition: kplotaxis.cpp:56
KPlotAxis::label
QString label() const
Definition: kplotaxis.cpp:86
KPlotAxis::tickLabelPrecision
int tickLabelPrecision() const
Definition: kplotaxis.cpp:108
kdebug.h
KPlotAxis::isVisible
bool isVisible() const
Definition: kplotaxis.cpp:61
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KPlotAxis::setVisible
void setVisible(bool visible)
Sets the "visible" property of the axis.
Definition: kplotaxis.cpp:66
KPlotAxis::setTickLabelFormat
void setTickLabelFormat(char format= 'g', int fieldWidth=0, int precision=-1)
Set the display format for converting the double value of the tick's position to the QString for the ...
Definition: kplotaxis.cpp:91
QString
KPlotAxis::KPlotAxis
KPlotAxis(const QString &label=QString())
Constructor, constructs an axis with the label label.
Definition: kplotaxis.cpp:50
KPlotAxis::minorTickMarks
QList< double > minorTickMarks() const
Definition: kplotaxis.cpp:196
KPlotAxis::tickLabel
QString tickLabel(double value) const
Definition: kplotaxis.cpp:178
kplotaxis.h
KPlotAxis::tickLabelWidth
int tickLabelWidth() const
Definition: kplotaxis.cpp:98
KPlotAxis::setTickLabelsShown
void setTickLabelsShown(bool b)
Determine whether tick labels will be drawn for this axis.
Definition: kplotaxis.cpp:76
KPlotAxis::areTickLabelsShown
bool areTickLabelsShown() const
Definition: kplotaxis.cpp:71
KPlotAxis
Axis for KPlotWidget.
Definition: kplotaxis.h:37
KPlotAxis::tickLabelFormat
char tickLabelFormat() const
Definition: kplotaxis.cpp:103
KPlotAxis::setLabel
void setLabel(const QString &label)
Sets the axis label.
Definition: kplotaxis.cpp:81
KPlotAxis::setTickMarks
void setTickMarks(double x0, double length)
Determine the positions of major and minor tickmarks for this axis.
Definition: kplotaxis.cpp:113
KPlotAxis::majorTickMarks
QList< double > majorTickMarks() const
Definition: kplotaxis.cpp:191
QList< double >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal