KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartNormalLineDiagram_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
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.txt included.
14 **
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.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include <limits>
24 
25 #include <QAbstractItemModel>
26 
27 #include "KDChartBarDiagram.h"
28 #include "KDChartLineDiagram.h"
29 #include "KDChartTextAttributes.h"
30 #include "KDChartAttributesModel.h"
32 #include "KDChartNormalLineDiagram_p.h"
33 #include "PaintingHelpers_p.h"
34 
35 using namespace KDChart;
36 using namespace std;
37 
38 NormalLineDiagram::NormalLineDiagram( LineDiagram* d )
39  : LineDiagramType( d )
40 {
41 }
42 
43 LineDiagram::LineType NormalLineDiagram::type() const
44 {
45  return LineDiagram::Normal;
46 }
47 
48 const QPair< QPointF, QPointF > NormalLineDiagram::calculateDataBoundaries() const
49 {
50  const int rowCount = compressor().modelDataRows();
51  const int colCount = compressor().modelDataColumns();
52  const qreal xMin = 0.0;
53  qreal xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) : 0;
54  if ( !diagram()->centerDataPoints() && diagram()->model() )
55  xMax -= 1;
56  qreal yMin = std::numeric_limits< qreal >::quiet_NaN();
57  qreal yMax = std::numeric_limits< qreal >::quiet_NaN();
58 
59  for ( int column = 0; column < colCount; ++column )
60  {
61  for ( int row = 0; row < rowCount; ++row )
62  {
63  const CartesianDiagramDataCompressor::CachePosition position( row, column );
64  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
65  const qreal value = ISNAN( point.value ) ? 0.0 : point.value;
66 
67  if ( ISNAN( yMin ) ) {
68  yMin = value;
69  yMax = value;
70  } else {
71  yMin = qMin( yMin, value );
72  yMax = qMax( yMax, value );
73  }
74  }
75  }
76 
77  // NOTE: calculateDataBoundaries must return the *real* data boundaries!
78  // i.e. we may NOT fake yMin to be qMin( 0.0, yMin )
79  // (khz, 2008-01-24)
80  const QPointF bottomLeft( QPointF( xMin, yMin ) );
81  const QPointF topRight( QPointF( xMax, yMax ) );
82  return QPair< QPointF, QPointF >( bottomLeft, topRight );
83 }
84 
85 void NormalLineDiagram::paint( PaintContext* ctx )
86 {
87  reverseMapper().clear();
88  Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
89  CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
90  const int columnCount = compressor().modelDataColumns();
91  const int rowCount = compressor().modelDataRows();
92  if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
93 
94  // Reverse order of data sets?
95  bool rev = diagram()->reverseDatasetOrder();
96  LabelPaintCache lpc;
97  LineAttributesInfoList lineList;
98  for ( int column = rev ? columnCount - 1 : 0;
99  rev ? (column >= 0) : (column < columnCount);
100  rev ? --column : ++column ) {
101  LineAttributes laPreviousCell;
102  CartesianDiagramDataCompressor::DataPoint lastPoint;
103  qreal lastAreaBoundingValue = 0;
104 
105  // Get min. y value, used as lower or upper bounding for area highlighting
106  const qreal minYValue = qMin(plane->visibleDataRange().bottom(), plane->visibleDataRange().top());
107 
108  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
109  for ( int row = 0; row < rowCount; ++row ) {
110  const CartesianDiagramDataCompressor::CachePosition position( row, column );
111  // get where to draw the line from:
112  CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
113  if ( point.hidden ) {
114  continue;
115  }
116 
117  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
118 
119  const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
121 
122  // lower or upper bounding for the highlighted area
123  qreal areaBoundingValue;
124  if ( laCell.areaBoundingDataset() != -1 ) {
125  const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
126  areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
127  } else {
128  // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
129  areaBoundingValue = minYValue;
130  }
131 
132  if ( ISNAN( point.value ) )
133  {
134  switch ( policy )
135  {
137  // we just bridge both values
138  continue;
140  // set it to zero
141  point.value = 0.0;
142  break;
144  // they're just hidden
145  break;
146  default:
147  break;
148  // hm....
149  }
150  }
151 
152  if ( !ISNAN( point.value ) ) {
153  // area corners, a + b are the line ends:
154  const qreal offset = diagram()->centerDataPoints() ? 0.5 : 0;
155  const QPointF a( plane->translate( QPointF( lastPoint.key + offset, lastPoint.value ) ) );
156  const QPointF b( plane->translate( QPointF( point.key + offset, point.value ) ) );
157  const QPointF c( plane->translate( QPointF( lastPoint.key + offset, lastAreaBoundingValue ) ) );
158  const QPointF d( plane->translate( QPointF( point.key + offset, areaBoundingValue ) ) );
159  const PositionPoints pts = PositionPoints( b, a, d, c );
160 
161  // add label
162  m_private->addLabel( &lpc, sourceIndex, &position, pts, Position::NorthWest,
163  Position::NorthWest, point.value );
164 
165  // add line and area, if switched on and we have a current and previous value
166  if ( !ISNAN( lastPoint.value ) ) {
167  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
168 
169  if ( laCell.displayArea() ) {
170  QList<QPolygonF> areas;
171  areas << ( QPolygonF() << a << b << d << c );
172  PaintingHelpers::paintAreas( m_private, ctx, attributesModel()->mapToSource( lastPoint.index ),
173  areas, laCell.transparency() );
174  }
175  }
176  }
177 
178  previousCellPosition = position;
179  laPreviousCell = laCell;
180  lastAreaBoundingValue = areaBoundingValue;
181  lastPoint = point;
182  }
183  }
184 
185  // paint the lines
186  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
187 }

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/