1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/* -*- Mode: C++ -*-
KDChart - a multi-platform charting engine
*/
/****************************************************************************
** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
**
** This file is part of the KDChart library.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid commercial KDChart licenses may use this file in
** accordance with the KDChart Commercial License Agreement provided with
** the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.klaralvdalens-datakonsult.se/?page=products for
** information about KDChart Commercial License Agreements.
**
** Contact info@klaralvdalens-datakonsult.se if any conditions of this
** licensing are not clear to you.
**
**********************************************************************/
#include "KDChartHiLoPainter.h"
#include <KDChartParams.h>
#include "KDChartTextPiece.h"
#include <tqpainter.h>
#include <stdlib.h>
/**
\class KDChartHiLoPainter KDChartHiLoPainter.h
\brief A chart painter implementation that can paint HiLo charts.
*/
/**
Constructor. Sets up internal data structures as necessary.
\param params the KDChartParams structure that defines the chart
*/
KDChartHiLoPainter::KDChartHiLoPainter( KDChartParams* params ) :
KDChartAxesPainter( params )
{
// This constructor intentionally left blank so far; we cannot setup the
// geometry yet since we do not know the size of the painter.
}
/**
Destructor.
*/
KDChartHiLoPainter::~KDChartHiLoPainter()
{
// intentionally left blank
}
/**
This method is a specialization that returns a fallback legend text
appropriate for HiLo that do not have the same notion of a dataset like
e.g. bars.
This method is only used when automatic legends are used, because
manual and first-column legends do not need fallback texts.
\param uint dataset the dataset number for which to generate a
fallback text
\return the fallback text to use for describing the specified
dataset in the legend
*/
TQString KDChartHiLoPainter::fallbackLegendText( uint dataset ) const
{
return TQObject::tr( "Value " ) + TQString::number( dataset + 1 );
}
/**
This methods returns the number of elements to be shown in the
legend in case fallback texts are used.
This method is only used when automatic legends are used, because
manual and first-column legends do not need fallback texts.
\return the number of fallback texts to use
*/
uint KDChartHiLoPainter::numLegendFallbackTexts( KDChartTableDataBase* data ) const
{
return data->usedRows();
}
bool KDChartHiLoPainter::isNormalMode() const
{
return KDChartParams::HiLoNormal == params()->hiLoChartSubType();
}
int KDChartHiLoPainter::clipShiftUp( bool, double ) const
{
return 0;
}
void KDChartHiLoPainter::specificPaintData( TQPainter* painter,
const TQRect& ourClipRect,
KDChartTableDataBase* data,
KDChartDataRegionList* /*regions*/,
const KDChartAxisParams* axisPara,
bool /*bNormalMode*/,
uint /*chart*/,
double logWidth,
double areaWidthP1000,
double logHeight,
double axisYOffset,
double /*minColumnValue*/,
double /*maxColumnValue*/,
double /*columnValueDistance*/,
uint chartDatasetStart,
uint chartDatasetEnd,
uint datasetStart,
uint datasetEnd )
{
double areaHeightP1000 = logHeight / 1000.0;
double averageValueP1000 = ( areaWidthP1000 + areaHeightP1000 ) / 2.0;
int datasetNum=abs(static_cast<int>(chartDatasetEnd-chartDatasetStart))+1;
painter->setPen( params()->outlineDataColor() );
// Number of values: If -1, use all values, otherwise use the
// specified number of values.
int numValues = 0;
if ( params()->numValues() != -1 )
numValues = params()->numValues();
else
numValues = data->usedCols();
// We need to make sure that we have a certain number of
// cells in the dataset(s), depending on the sub type to display.
if( (numValues < 2) ||
((params()->hiLoChartSubType() == KDChartParams::HiLoClose) && (numValues < 3)) ||
((params()->hiLoChartSubType() == KDChartParams::HiLoOpenClose) && (numValues < 4)) ){
qDebug( "\nNot enough data to display a High/Low Chart!\n" );
qDebug( "type requiring" );
qDebug( "---- ---------" );
qDebug( "High/Low 2 data cells per series" );
qDebug( "High/Low/Close 3 data cells per series" );
qDebug( "High/Low/open/Close 4 data cells per series\n" );
return; // PENDING(kalle) Throw exception?
}
double pixelsPerUnit = 0.0;
if( 0.0 != axisPara->trueAxisHigh() - axisPara->trueAxisLow() )
pixelsPerUnit = logHeight / (axisPara->trueAxisHigh() - axisPara->trueAxisLow());
else
pixelsPerUnit = logHeight / 10;
// Distance between the individual "stocks"
double pointDist = logWidth / (double)datasetNum;
// compute the position of the 0 axis
double zeroXAxisI = axisPara->axisZeroLineStartY() - _dataRect.y();
const int nLineWidth = params()->lineWidth();
// Loop over the datasets, draw one "stock" line for each series.
for ( uint dataset = chartDatasetStart;
dataset <= chartDatasetEnd;
++dataset ) {
// The first and the second col are always high and low; we sort them
// accordingly.
TQVariant valueA;
TQVariant valueB;
if( dataset >= datasetStart &&
dataset <= datasetEnd &&
data->cellCoord( dataset, 0, valueA, 1 ) &&
data->cellCoord( dataset, 1, valueB, 1 ) &&
TQVariant::Double == valueA.type() &&
TQVariant::Double == valueB.type() ){
const double cellValue1 = valueA.toDouble();
const double cellValue2 = valueB.toDouble();
const double lowValue = TQMIN( cellValue1, cellValue2 );
const double highValue = TQMAX( cellValue1, cellValue2 );
const double lowDrawValue = lowValue * pixelsPerUnit;
const double highDrawValue = highValue * pixelsPerUnit;
painter->setPen( TQPen( params()->dataColor( dataset ),
nLineWidth ) );
// draw line from low to high
int xpos = static_cast<int>(
pointDist * ( (double)(dataset-chartDatasetStart) + 0.5 ) );
int lowYPos = static_cast<int>( zeroXAxisI - lowDrawValue );
int highYPos = static_cast<int>( zeroXAxisI - highDrawValue );
painter->drawLine( xpos, lowYPos, xpos, highYPos );
// Find out how long open/close lines need to be in case we
// need them. We make them 1/10 of the space available for one
// "stock".
int openCloseTickLength = static_cast<int>( pointDist * 0.1 );
// we need these here because we might need to consider these
// later when drawing the low and high labels
bool hasOpen = false, hasClose = false;
double openValue = 0.0, openDrawValue = 0.0,
closeValue = 0.0, closeDrawValue = 0.0;
// if we have an open/close chart, show the open value
if( params()->hiLoChartSubType() == KDChartParams::HiLoOpenClose ) {
// Only do this if there is a value in the third col.
if( data->cellCoord( dataset, 2, valueA, 1 ) &&
TQVariant::Double == valueA.type() ) {
hasOpen = true;
openValue = valueA.toDouble();
openDrawValue = openValue * pixelsPerUnit;
painter->drawLine( xpos - openCloseTickLength,
static_cast<int>( zeroXAxisI - openDrawValue ),
xpos,
static_cast<int>( zeroXAxisI - openDrawValue ) );
}
}
// If we have an open/close chart or a close chart, show the
// close value, but only if there is a value in the
// corresponding column (2 for HiLoClose, 3 for
// HiLoOpenClose).
if( ( params()->hiLoChartSubType() == KDChartParams::HiLoClose &&
data->cellCoord( dataset, 2, valueA, 1 ) &&
TQVariant::Double == valueA.type() ) ||
( params()->hiLoChartSubType() == KDChartParams::HiLoOpenClose &&
data->cellCoord( dataset, 3, valueB, 1 ) &&
TQVariant::Double == valueB.type() ) ) {
hasClose = true;
closeValue = ( params()->hiLoChartSubType() == KDChartParams::HiLoClose )
? valueA.toDouble()
: valueB.toDouble();
closeDrawValue = closeValue * pixelsPerUnit;
painter->drawLine( xpos,
static_cast<int>( zeroXAxisI - closeDrawValue ),
xpos + openCloseTickLength,
static_cast<int>( zeroXAxisI - closeDrawValue ) );
}
// Draw the low value, if requested.
if( params()->hiLoChartPrintLowValues() ) {
// PENDING(kalle) Number formatting?
TQFont theFont( params()->hiLoChartLowValuesFont() );
if ( params()->hiLoChartLowValuesUseFontRelSize() ) {
int nTxtHeight =
static_cast < int > ( params()->hiLoChartLowValuesFontRelSize()
* averageValueP1000 );
theFont.setPointSizeFloat( nTxtHeight );
}
KDChartTextPiece lowText( painter, TQString::number( lowValue ),
theFont );
int width = lowText.width();
int height = lowText.height();
// Check whether there is enough space below the data display
int valX = 0, valY = 0;
//qDebug("\nzeroXAxisI %f lowDrawValue %f height %i logHeight %f _dataRect.y() %i axisYOffset %f",zeroXAxisI,highDrawValue,height,logHeight,_dataRect.y(),axisYOffset);
//qDebug("zeroXAxisI - lowDrawValue + height %f < axisYOffset + logHeight %f",
//zeroXAxisI - lowDrawValue + height, axisYOffset+logHeight);
if( zeroXAxisI - lowDrawValue + height < axisYOffset+logHeight ) {
// enough space
valX = xpos - ( width / 2 );
valY = (int)lowDrawValue - lowText.fontLeading();
} else {
// not enough space - move to left
if( !hasOpen || height < openDrawValue ) {
// Either there is no open value or it is high enough
// that we can put the low value to the left.
valX = xpos - width - nLineWidth;
valY = static_cast<int>(zeroXAxisI)
- lowYPos
+ height/2
+ nLineWidth/2;
} else
; // no way to draw it (really?)
}
lowText.draw( painter,
valX, static_cast<int>( zeroXAxisI - valY ),
ourClipRect,
params()->hiLoChartLowValuesColor() );
}
// Draw the high value, if requested.
if( params()->hiLoChartPrintHighValues() ) {
// PENDING(kalle) Number formatting?
TQFont theFont( params()->hiLoChartHighValuesFont() );
if ( params()->hiLoChartHighValuesUseFontRelSize() ) {
int nTxtHeight =
static_cast < int > ( params()->hiLoChartHighValuesFontRelSize()
* averageValueP1000 );
theFont.setPointSizeFloat( nTxtHeight );
}
KDChartTextPiece highText( painter, TQString::number( highValue ),
theFont );
int width = highText.width();
int height = highText.height();
// Check whether there is enough space above the data display
int valX = 0, valY = 0;
if( zeroXAxisI - highDrawValue - height > axisYOffset ) {
// enough space
valX = xpos - ( width / 2 );
valY = (int)highDrawValue + highText.fontLeading() + height;
} else {
// not enough space - move to right
if( !hasClose ||
height < ( _dataRect.height() - closeDrawValue ) ) {
// Either there is no close value or it is low enough
// that we can put the high value to the right.
valX = xpos + nLineWidth;
valY = static_cast<int>(zeroXAxisI)
- highYPos
+ height/2
- nLineWidth/2;
} else
; // no way to draw it (really?)
}
highText.draw( painter,
valX, static_cast<int>( zeroXAxisI - valY ),
ourClipRect,
params()->hiLoChartHighValuesColor() );
}
// Draw the open value, if requested.
if( params()->hiLoChartPrintOpenValues() &&
params()->hiLoChartSubType() == KDChartParams::HiLoOpenClose ) {
// PENDING(kalle) Number formatting?
TQFont theFont( params()->hiLoChartOpenValuesFont() );
if ( params()->hiLoChartOpenValuesUseFontRelSize() ) {
int nTxtHeight =
static_cast < int > ( params()->hiLoChartOpenValuesFontRelSize()
* averageValueP1000 );
theFont.setPointSizeFloat( nTxtHeight );
}
KDChartTextPiece openText( painter, TQString::number( openValue ),
theFont );
int width = openText.width();
int height = openText.height();
// We can pretty safely assume that there is always enough
// space to the left and right of the data display.
int valX = 0, valY = 0;
valX = xpos - openCloseTickLength - width;
valY = (int)openDrawValue + ( height / 2 );
openText.draw( painter,
valX, static_cast<int>( zeroXAxisI - valY ),
ourClipRect,
params()->hiLoChartOpenValuesColor() );
}
// Draw the close value, if requested.
if( params()->hiLoChartPrintCloseValues() &&
( params()->hiLoChartSubType() == KDChartParams::HiLoOpenClose
||
params()->hiLoChartSubType() == KDChartParams::HiLoClose ) ) {
// PENDING(kalle) Number formatting?
TQFont theFont( params()->hiLoChartCloseValuesFont() );
if ( params()->hiLoChartCloseValuesUseFontRelSize() ) {
int nTxtHeight =
static_cast < int > ( params()->hiLoChartCloseValuesFontRelSize()
* averageValueP1000 );
theFont.setPointSizeFloat( nTxtHeight );
}
KDChartTextPiece closeText( painter, TQString::number( closeValue ),
theFont );
//int width = closeText.width();
int height = closeText.height();
// We can pretty safely assume that there is always enough
// space to the left and right of the data display.
int valX = 0, valY = 0;
valX = xpos + openCloseTickLength;
valY = (int)closeDrawValue + ( height / 2 );
closeText.draw( painter,
valX, static_cast<int>( zeroXAxisI - valY ),
ourClipRect,
params()->hiLoChartCloseValuesColor() );
}
} else
continue; // we cannot display this value
}
}
|