1
/****************************************************************************
2
** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved.
4
** This file is part of the KD Chart library.
6
** Licensees holding valid commercial KD Chart licenses may use this file in
7
** accordance with the KD Chart Commercial License Agreement provided with
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 and version 3 as published by the
13
** Free Software Foundation and appearing in the file LICENSE.GPL included.
15
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
** Contact info@kdab.com if any conditions of this licensing are not
21
**********************************************************************/
23
#include "KDChartStockDiagram_p.h"
25
using namespace KDChart;
28
class StockDiagram::Private::ThreeDPainter
31
struct ThreeDProperties {
37
ThreeDPainter( QPainter *p )
40
QPolygonF drawTwoDLine( const QLineF &line, const QPen &pen,
41
const ThreeDProperties &props );
42
QPolygonF drawThreeDLine( const QLineF &line, const QBrush &brush,
43
const QPen &pen, const ThreeDProperties &props );
44
QPolygonF drawThreeDRect( const QRectF &rect, const QBrush &brush,
45
const QPen &pen, const ThreeDProperties &props );
48
QPointF projectPoint( const QPointF &point, qreal depth, qreal angle ) const;
49
QColor calcShadowColor( const QColor &color, qreal angle ) const;
55
* Projects a point in 3D space
57
* @param depth The distance from the point and the projected point
58
* @param angle The angle the projected point is rotated by around the original point
60
QPointF StockDiagram::Private::ThreeDPainter::projectPoint( const QPointF &point, qreal depth, qreal angle ) const
62
const qreal angleInRad = DEGTORAD( angle );
63
const qreal distX = depth * cos( angleInRad );
64
// Y coordinates are reversed on our coordinate plane
65
const qreal distY = depth * -sin( angleInRad );
67
return QPointF( point.x() + distX, point.y() + distY );
71
* Returns the shadow color for a given color, depending on the angle of rotation
73
* @param color The color to calculate the shadow color for
74
* @param angle The angle that the colored area is rotated by
76
QColor StockDiagram::Private::ThreeDPainter::calcShadowColor( const QColor &color, qreal angle ) const
78
// The shadow factor determines to how many percent the brightness
79
// of the color can be reduced. That is, the darkest shadow color
80
// is color * shadowFactor.
81
const qreal shadowFactor = 0.5;
82
const qreal sinAngle = 1.0 - qAbs( sin( DEGTORAD( angle ) ) ) * shadowFactor;
83
return QColor( qRound( color.red() * sinAngle ),
84
qRound( color.green() * sinAngle ),
85
qRound( color.blue() * sinAngle ) );
89
* Draws a 2D line in 3D space by painting it with a z-coordinate of props.depth / 2.0
91
* @param line The line to draw
92
* @param pen The pen to use to draw the line
93
* @param props The 3D properties to draw the line with
94
* @return The drawn line, but with a width of 2px, as a polygon
96
QPolygonF StockDiagram::Private::ThreeDPainter::drawTwoDLine( const QLineF &line, const QPen &pen,
97
const ThreeDProperties &props )
99
// Restores the painting properties when destroyed
100
PainterSaver painterSaver( painter );
102
// The z coordinate to use (i.e., at what depth to draw the line)
103
const qreal z = props.depth / 2.0;
105
// Projec the 2D points of the line in 3D
106
const QPointF deepP1 = projectPoint( line.p1(), z, props.angle );
107
const QPointF deepP2 = projectPoint( line.p2(), z, props.angle );
109
// The drawn line with a width of 2px
110
QPolygonF threeDArea;
111
// The offset of the line "borders" from the center to each side
112
const QPointF offset( 0.0, 1.0 );
113
threeDArea << deepP1 - offset << deepP2 - offset
114
<< deepP1 + offset << deepP2 + offset << deepP1 - offset;
116
painter->setPen( pen );
117
painter->drawLine( QLineF( deepP1, deepP2 ) );
123
* Draws an ordinary line in 3D by expanding it in the z-axis by the given depth.
125
* @param line The line to draw
126
* @param brush The brush to fill the resulting polygon with
127
* @param pen The pen to paint the borders of the resulting polygon with
128
* @param props The 3D properties to draw the line with
129
* @return The 3D shape drawn
131
QPolygonF StockDiagram::Private::ThreeDPainter::drawThreeDLine( const QLineF &line, const QBrush &brush,
132
const QPen &pen, const ThreeDProperties &props )
134
// Restores the painting properties when destroyed
135
PainterSaver painterSaver( painter );
137
const QPointF p1 = line.p1();
138
const QPointF p2 = line.p2();
140
// Project the 2D points of the line in 3D
141
const QPointF deepP1 = projectPoint( p1, props.depth, props.angle );
142
const QPointF deepP2 = projectPoint( p2, props.depth, props.angle );
144
// The result is a 3D representation of the 2D line
145
QPolygonF threeDArea;
146
threeDArea << p1 << p2 << deepP2 << deepP1 << p1;
148
// Use shadow colors if ThreeDProperties::useShadowColors is set
149
// Note: Setting a new color on a brush or pen does not effect gradients or textures
150
if ( props.useShadowColors ) {
151
QBrush shadowBrush( brush );
152
QPen shadowPen( pen );
153
shadowBrush.setColor( calcShadowColor( brush.color(), props.angle ) );
154
shadowPen.setColor( calcShadowColor( pen.color(), props.angle ) );
155
painter->setBrush( shadowBrush );
156
painter->setPen( shadowPen );
158
painter->setBrush( brush );
159
painter->setPen( pen );
162
painter->drawPolygon( threeDArea );
168
* Draws a 3D cuboid by extending a 2D rectangle in the z-axis
170
* @param rect The rectangle to draw
171
* @param brush The brush fill the surfaces of the cuboid with
172
* @param pen The pen to draw the edges with
173
* @param props The 3D properties to use for drawing the cuboid
174
* @return The drawn cuboid as a polygon
176
QPolygonF StockDiagram::Private::ThreeDPainter::drawThreeDRect( const QRectF &rect, const QBrush &brush,
177
const QPen &pen, const ThreeDProperties &props )
179
// Restores the painting properties when destroyed
180
PainterSaver painterSaver( painter );
182
// Make sure that the top really is the top
183
const QRectF normalizedRect = rect.normalized();
185
// Calculate all the four sides of the rectangle
186
const QLineF topSide = QLineF( normalizedRect.topLeft(), normalizedRect.topRight() );
187
const QLineF bottomSide = QLineF( normalizedRect.bottomLeft(), normalizedRect.bottomRight() );
188
const QLineF leftSide = QLineF( normalizedRect.topLeft(), normalizedRect.bottomLeft() );
189
const QLineF rightSide = QLineF( normalizedRect.topRight(), normalizedRect.bottomRight() );
191
QPolygonF drawnPolygon;
193
// Shorter names are easier on the eyes
194
const qreal angle = props.angle;
196
// Only top and right side is visible
197
if ( angle >= 0.0 && angle < 90.0 ) {
198
drawnPolygon = drawnPolygon.united( drawThreeDLine( topSide, brush, pen, props ) );
199
drawnPolygon = drawnPolygon.united( drawThreeDLine( rightSide, brush, pen, props ) );
200
// Only top and left side is visible
201
} else if ( angle >= 90.0 && angle < 180.0 ) {
202
drawnPolygon = drawnPolygon.united( drawThreeDLine( topSide, brush, pen, props ) );
203
drawnPolygon = drawnPolygon.united( drawThreeDLine( leftSide, brush, pen, props ) );
204
// Only bottom and left side is visible
205
} else if ( angle >= 180.0 && angle < 270.0 ) {
206
drawnPolygon = drawnPolygon.united( drawThreeDLine( bottomSide, brush, pen, props ) );
207
drawnPolygon = drawnPolygon.united( drawThreeDLine( leftSide, brush, pen, props ) );
208
// Only bottom and right side is visible
209
} else if ( angle >= 270.0 && angle <= 360.0 ) {
210
drawnPolygon = drawnPolygon.united( drawThreeDLine( bottomSide, brush, pen, props ) );
211
drawnPolygon = drawnPolygon.united( drawThreeDLine( rightSide, brush, pen, props ) );
214
// Draw the front side
215
painter->setPen( pen );
216
painter->setBrush( brush );
217
painter->drawRect( normalizedRect );
223
StockDiagram::Private::Private()
224
: AbstractCartesianDiagram::Private()
228
StockDiagram::Private::Private( const Private& r )
229
: AbstractCartesianDiagram::Private( r )
233
StockDiagram::Private::~Private()
238
* Projects a point onto the coordinate plane
240
* @param context The context to paint the point in
241
* @point The point to project onto the coordinate plane
242
* @return The projected point
244
QPointF StockDiagram::Private::projectPoint( PaintContext *context, const QPointF &point ) const
246
return context->coordinatePlane()->translate( QPointF( point.x() + 0.5, point.y() ) );
250
* Projects a candlestick onto the coordinate plane
252
* @param context The context to paint the candlestick in
255
QRectF StockDiagram::Private::projectCandlestick( PaintContext *context, const QPointF &open, const QPointF &close, qreal width ) const
257
const QPointF leftHighPoint = context->coordinatePlane()->translate( QPointF( close.x() + 0.5 - width / 2.0, close.y() ) );
258
const QPointF rightLowPoint = context->coordinatePlane()->translate( QPointF( open.x() + 0.5 + width / 2.0, open.y() ) );
259
const QPointF rightHighPoint = context->coordinatePlane()->translate( QPointF( close.x() + 0.5 + width / 2.0, close.y() ) );
261
return QRectF( leftHighPoint, QSizeF( rightHighPoint.x() - leftHighPoint.x(),
262
rightLowPoint.y() - leftHighPoint.y() ) );
265
void StockDiagram::Private::drawOHLCBar( const CartesianDiagramDataCompressor::DataPoint &open,
266
const CartesianDiagramDataCompressor::DataPoint &high,
267
const CartesianDiagramDataCompressor::DataPoint &low,
268
const CartesianDiagramDataCompressor::DataPoint &close,
269
PaintContext *context )
271
// Note: A row in the model is a column in a StockDiagram
272
const int col = low.index.row();
274
StockBarAttributes attr = diagram->stockBarAttributes( col );
275
ThreeDBarAttributes threeDAttr = diagram->threeDBarAttributes( col );
276
const qreal tickLength = attr.tickLength();
278
const QPointF leftOpenPoint( open.key + 0.5 - tickLength, open.value );
279
const QPointF rightOpenPoint( open.key + 0.5, open.value );
280
const QPointF highPoint( high.key + 0.5, high.value );
281
const QPointF lowPoint( low.key + 0.5, low.value );
282
const QPointF leftClosePoint( close.key + 0.5, close.value );
283
const QPointF rightClosePoint( close.key + 0.5 + tickLength, close.value );
285
bool reversedOrder = false;
286
// If 3D mode is enabled, we have to make sure the z-order is right
287
if ( threeDAttr.isEnabled() ) {
288
const int angle = threeDAttr.angle();
289
// Z-order is from right to left
290
if ( ( angle >= 0 && angle < 90 ) || ( angle >= 180 && angle < 270 ) )
291
reversedOrder = true;
292
// Z-order is from left to right
293
if ( ( angle >= 90 && angle < 180 ) || ( angle >= 270 && angle < 0 ) )
294
reversedOrder = false;
297
if ( reversedOrder ) {
299
drawLine( col, leftOpenPoint, rightOpenPoint, context ); // Open marker
300
if ( !low.hidden && !high.hidden )
301
drawLine( col, lowPoint, highPoint, context ); // Low-High line
303
drawLine( col, leftClosePoint, rightClosePoint, context ); // Close marker
306
drawLine( col, leftClosePoint, rightClosePoint, context ); // Close marker
307
if ( !low.hidden && !high.hidden )
308
drawLine( col, lowPoint, highPoint, context ); // Low-High line
310
drawLine( col, leftOpenPoint, rightOpenPoint, context ); // Open marker
313
DataValueTextInfoList list;
315
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( open.index ), 0,
316
PositionPoints( leftOpenPoint ), Position::South, Position::South, open.value );
318
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( high.index ), 0,
319
PositionPoints( highPoint ), Position::South, Position::South, high.value );
321
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( low.index ), 0,
322
PositionPoints( lowPoint ), Position::South, Position::South, low.value );
324
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( close.index ), 0,
325
PositionPoints( rightClosePoint ), Position::South, Position::South, close.value );
326
paintDataValueTextsAndMarkers( diagram, context, list, false );
330
* Draws a line connecting the low and the high value of an OHLC chart
332
* @param low The low data point
333
* @param high The high data point
334
* @param context The context to draw the candlestick in
336
void StockDiagram::Private::drawCandlestick( const CartesianDiagramDataCompressor::DataPoint &open,
337
const CartesianDiagramDataCompressor::DataPoint &high,
338
const CartesianDiagramDataCompressor::DataPoint &low,
339
const CartesianDiagramDataCompressor::DataPoint &close,
340
PaintContext *context )
342
PainterSaver painterSaver( context->painter() );
344
// Note: A row in the model is a column in a StockDiagram, and the other way around
345
const int row = low.index.row();
346
const int col = low.index.column();
348
QPointF bottomCandlestickPoint;
349
QPointF topCandlestickPoint;
353
bool drawCandlestick = !open.hidden && !close.hidden;
356
// Find out if we need to paint a down-trend or up-trend candlestick
357
// and set brush and pen accordingly
358
// Also, determine what the top and bottom points of the candlestick are
359
if ( open.value <= close.value ) {
360
pen = diagram->upTrendCandlestickPen( row );
361
brush = diagram->upTrendCandlestickBrush( row );
362
bottomCandlestickPoint = QPointF( open.key, open.value );
363
topCandlestickPoint = QPointF( close.key, close.value );
364
drawLowerLine = !low.hidden && !open.hidden;
365
drawUpperLine = !low.hidden && !close.hidden;
367
pen = diagram->downTrendCandlestickPen( row );
368
brush = diagram->downTrendCandlestickBrush( row );
369
bottomCandlestickPoint = QPointF( close.key, close.value );
370
topCandlestickPoint = QPointF( open.key, open.value );
371
drawLowerLine = !low.hidden && !close.hidden;
372
drawUpperLine = !low.hidden && !open.hidden;
375
StockBarAttributes attr = diagram->stockBarAttributes( col );
376
ThreeDBarAttributes threeDAttr = diagram->threeDBarAttributes( col );
378
const QPointF lowPoint = projectPoint( context, QPointF( low.key, low.value ) );
379
const QPointF highPoint = projectPoint( context, QPointF( high.key, high.value ) );
380
const QLineF lowerLine = QLineF( lowPoint, projectPoint( context, bottomCandlestickPoint ) );
381
const QLineF upperLine = QLineF( projectPoint( context, topCandlestickPoint ), highPoint );
383
// Convert the data point into coordinates on the coordinate plane
384
QRectF candlestick = projectCandlestick( context, bottomCandlestickPoint,
385
topCandlestickPoint, attr.candlestickWidth() );
387
// Remember the drawn polygon to add it to the ReverseMapper later
388
QPolygonF drawnPolygon;
390
// Use the ThreeDPainter class to draw a 3D candlestick
391
if ( threeDAttr.isEnabled() ) {
392
ThreeDPainter threeDPainter( context->painter() );
394
ThreeDPainter::ThreeDProperties threeDProps;
395
threeDProps.depth = threeDAttr.depth();
396
threeDProps.angle = threeDAttr.angle();
397
threeDProps.useShadowColors = threeDAttr.useShadowColors();
399
// If the perspective angle is within [0,180], we paint from bottom to top,
400
// otherwise from top to bottom to ensure the correct z order
401
if ( threeDProps.angle > 0.0 && threeDProps.angle < 180.0 ) {
403
drawnPolygon = threeDPainter.drawTwoDLine( lowerLine, pen, threeDProps );
404
if ( drawCandlestick )
405
drawnPolygon = threeDPainter.drawThreeDRect( candlestick, brush, pen, threeDProps );
407
drawnPolygon = threeDPainter.drawTwoDLine( upperLine, pen, threeDProps );
410
drawnPolygon = threeDPainter.drawTwoDLine( upperLine, pen, threeDProps );
411
if ( drawCandlestick )
412
drawnPolygon = threeDPainter.drawThreeDRect( candlestick, brush, pen, threeDProps );
414
drawnPolygon = threeDPainter.drawTwoDLine( lowerLine, pen, threeDProps );
417
QPainter *const painter = context->painter();
418
painter->setBrush( brush );
419
painter->setPen( pen );
421
painter->drawLine( lowerLine );
423
painter->drawLine( upperLine );
424
if ( drawCandlestick )
425
painter->drawRect( candlestick );
427
// The 2D representation is the projected candlestick itself
428
drawnPolygon = candlestick;
430
// FIXME: Add lower and upper line to reverse mapper
433
DataValueTextInfoList list;
436
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( low.index ), 0,
437
PositionPoints( lowPoint ), Position::South, Position::South, low.value );
438
if ( drawCandlestick ) {
439
// Both, the open as well as the close value are represented by this candlestick
440
reverseMapper.addPolygon( row, openValueColumn(), drawnPolygon );
441
reverseMapper.addPolygon( row, closeValueColumn(), drawnPolygon );
443
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( open.index ), 0,
444
PositionPoints( candlestick.bottomRight() ), Position::South, Position::South, open.value );
445
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( close.index ), 0,
446
PositionPoints( candlestick.topRight() ), Position::South, Position::South, close.value );
449
appendDataValueTextInfoToList( diagram, list, diagram->attributesModel()->mapToSource( high.index ), 0,
450
PositionPoints( highPoint ), Position::South, Position::South, high.value );
452
paintDataValueTextsAndMarkers( diagram, context, list, false );
456
* Draws a line connecting two points
458
* @param col The column of the diagram to paint the line in
459
* @param point1 The first point
460
* @param point2 The second point
461
* @param context The context to draw the low-high line in
463
void StockDiagram::Private::drawLine( int col, const QPointF &point1, const QPointF &point2, PaintContext *context )
465
PainterSaver painterSaver( context->painter() );
467
// A row in the model is a column in the diagram
468
const int modelRow = col;
469
const int modelCol = 0;
471
const QPen pen = diagram->pen( col );
472
const QBrush brush = diagram->brush( col );
473
const ThreeDBarAttributes threeDBarAttr = diagram->threeDBarAttributes( col );
475
QPointF transP1 = context->coordinatePlane()->translate( point1 );
476
QPointF transP2 = context->coordinatePlane()->translate( point2 );
477
QLineF line = QLineF( transP1, transP2 );
479
if ( threeDBarAttr.isEnabled() ) {
480
ThreeDPainter::ThreeDProperties threeDProps;
481
threeDProps.angle = threeDBarAttr.angle();
482
threeDProps.depth = threeDBarAttr.depth();
483
threeDProps.useShadowColors = threeDBarAttr.useShadowColors();
485
ThreeDPainter painter( context->painter() );
486
reverseMapper.addPolygon( modelCol, modelRow, painter.drawThreeDLine( line, brush, pen, threeDProps ) );
488
context->painter()->setPen( pen );
489
//context->painter()->setBrush( brush );
490
reverseMapper.addLine( modelCol, modelRow, transP1, transP2 );
491
context->painter()->drawLine( line );
496
* Returns the column of the open value in the model
498
* @return The column of the open value
500
int StockDiagram::Private::openValueColumn() const
502
// Return an invalid column if diagram has no open values
503
return type == HighLowClose ? -1 : 0;
507
* Returns the column of the high value in the model
509
* @return The column of the high value
511
int StockDiagram::Private::highValueColumn() const
513
return type == HighLowClose ? 0 : 1;
517
* Returns the column of the low value in the model
519
* @return The column of the low value
521
int StockDiagram::Private::lowValueColumn() const
523
return type == HighLowClose ? 1 : 2;
527
* Returns the column of the close value in the model
529
* @return The column of the close value
531
int StockDiagram::Private::closeValueColumn() const
533
return type == HighLowClose ? 2 : 3;