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

KDEUI

  • 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
26class 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
50KPlotAxis::KPlotAxis( const QString &label )
51 : d( new Private( this ) )
52{
53 d->m_label = label;
54}
55
56KPlotAxis::~KPlotAxis()
57{
58 delete d;
59}
60
61bool KPlotAxis::isVisible() const
62{
63 return d->m_visible;
64}
65
66void KPlotAxis::setVisible( bool visible )
67{
68 d->m_visible = visible;
69}
70
71bool KPlotAxis::areTickLabelsShown() const
72{
73 return d->m_showTickLabels;
74}
75
76void KPlotAxis::setTickLabelsShown( bool b )
77{
78 d->m_showTickLabels = b;
79}
80
81void KPlotAxis::setLabel( const QString& label )
82{
83 d->m_label = label;
84}
85
86QString KPlotAxis::label() const
87{
88 return d->m_label;
89}
90
91void 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
98int KPlotAxis::tickLabelWidth() const
99{
100 return d->m_labelFieldWidth;
101}
102
103char KPlotAxis::tickLabelFormat() const
104{
105 return d->m_labelFmt;
106}
107
108int KPlotAxis::tickLabelPrecision() const
109{
110 return d->m_labelPrec;
111}
112
113void 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
178QString 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
191QList< double > KPlotAxis::majorTickMarks() const
192{
193 return d->m_MajorTickMarks;
194}
195
196QList< double > KPlotAxis::minorTickMarks() const
197{
198 return d->m_MinorTickMarks;
199}
200
KPlotAxis
Axis for KPlotWidget.
Definition: kplotaxis.h:37
KPlotAxis::majorTickMarks
QList< double > majorTickMarks() const
Definition: kplotaxis.cpp:191
KPlotAxis::setTickMarks
void setTickMarks(double x0, double length)
Determine the positions of major and minor tickmarks for this axis.
Definition: kplotaxis.cpp:113
KPlotAxis::tickLabelPrecision
int tickLabelPrecision() const
Definition: kplotaxis.cpp:108
KPlotAxis::tickLabelFormat
char tickLabelFormat() const
Definition: kplotaxis.cpp:103
KPlotAxis::isVisible
bool isVisible() const
Definition: kplotaxis.cpp:61
KPlotAxis::setLabel
void setLabel(const QString &label)
Sets the axis label.
Definition: kplotaxis.cpp:81
KPlotAxis::~KPlotAxis
~KPlotAxis()
Destructor.
Definition: kplotaxis.cpp:56
KPlotAxis::label
QString label() const
Definition: kplotaxis.cpp:86
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
KPlotAxis::KPlotAxis
KPlotAxis(const QString &label=QString())
Constructor, constructs an axis with the label label.
Definition: kplotaxis.cpp:50
KPlotAxis::setTickLabelsShown
void setTickLabelsShown(bool b)
Determine whether tick labels will be drawn for this axis.
Definition: kplotaxis.cpp:76
KPlotAxis::setVisible
void setVisible(bool visible)
Sets the "visible" property of the axis.
Definition: kplotaxis.cpp:66
KPlotAxis::areTickLabelsShown
bool areTickLabelsShown() const
Definition: kplotaxis.cpp:71
KPlotAxis::minorTickMarks
QList< double > minorTickMarks() const
Definition: kplotaxis.cpp:196
KPlotAxis::tickLabelWidth
int tickLabelWidth() const
Definition: kplotaxis.cpp:98
KPlotAxis::tickLabel
QString tickLabel(double value) const
Definition: kplotaxis.cpp:178
QList
kdebug.h
kplotaxis.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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