KD Chart 2  [rev.2.6]
KDChartNormalLineDiagram_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 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  return compressor().dataBoundaries();
51 }
52 
53 void NormalLineDiagram::paint( PaintContext* ctx )
54 {
55  reverseMapper().clear();
56  Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
57  CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
58  const int columnCount = compressor().modelDataColumns();
59  const int rowCount = compressor().modelDataRows();
60  if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
61 
62  // Reverse order of data sets?
63  bool rev = diagram()->reverseDatasetOrder();
64  LabelPaintCache lpc;
65  LineAttributesInfoList lineList;
66 
67  const int step = rev ? -1 : 1;
68  const int end = rev ? -1 : columnCount;
69  for ( int column = rev ? columnCount - 1 : 0; column != end; column += step ) {
70  LineAttributes laPreviousCell;
71  CartesianDiagramDataCompressor::DataPoint lastPoint;
72  qreal lastAreaBoundingValue = 0;
73 
74  // Get min. y value, used as lower or upper bounding for area highlighting
75  const qreal minYValue = qMin(plane->visibleDataRange().bottom(), plane->visibleDataRange().top());
76 
77  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
78  for ( int row = 0; row < rowCount; ++row ) {
79  const CartesianDiagramDataCompressor::CachePosition position( row, column );
80  // get where to draw the line from:
81  CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
82  if ( point.hidden ) {
83  continue;
84  }
85 
86  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
87 
88  const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
90 
91  // lower or upper bounding for the highlighted area
92  qreal areaBoundingValue;
93  if ( laCell.areaBoundingDataset() != -1 ) {
94  const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
95  areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
96  } else {
97  // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
98  areaBoundingValue = minYValue;
99  }
100 
101  if ( ISNAN( point.value ) )
102  {
103  switch ( policy )
104  {
106  // we just bridge both values
107  continue;
109  // set it to zero
110  point.value = 0.0;
111  break;
113  // they're just hidden
114  break;
115  default:
116  break;
117  // hm....
118  }
119  }
120 
121  if ( !ISNAN( point.value ) ) {
122  // area corners, a + b are the line ends:
123  const qreal offset = diagram()->centerDataPoints() ? 0.5 : 0;
124  const QPointF a( plane->translate( QPointF( lastPoint.key + offset, lastPoint.value ) ) );
125  const QPointF b( plane->translate( QPointF( point.key + offset, point.value ) ) );
126  const QPointF c( plane->translate( QPointF( lastPoint.key + offset, lastAreaBoundingValue ) ) );
127  const QPointF d( plane->translate( QPointF( point.key + offset, areaBoundingValue ) ) );
128  const PositionPoints pts = PositionPoints( b, a, d, c );
129 
130  // add label
131  m_private->addLabel( &lpc, sourceIndex, &position, pts, Position::NorthWest,
132  Position::NorthWest, point.value );
133 
134  // add line and area, if switched on and we have a current and previous value
135  if ( !ISNAN( lastPoint.value ) ) {
136  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
137 
138  if ( laCell.displayArea() ) {
139  QList<QPolygonF> areas;
140  areas << ( QPolygonF() << a << b << d << c );
141  PaintingHelpers::paintAreas( m_private, ctx, attributesModel()->mapToSource( lastPoint.index ),
142  areas, laCell.transparency() );
143  }
144  }
145  }
146 
147  previousCellPosition = position;
148  laPreviousCell = laCell;
149  lastAreaBoundingValue = areaBoundingValue;
150  lastPoint = point;
151  }
152  }
153 
154  // paint the lines
155  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
156 }
KDChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
void paintElements(AbstractDiagram::Private *diagramPrivate, PaintContext *ctx, const LabelPaintCache &lpc, const LineAttributesInfoList &lineList)
const QPointF translate(const QPointF &diagramPoint) const
Translate the given point in value space coordinates to a position in pixel space.
AbstractCoordinatePlane * coordinatePlane() const
Set of attributes for changing the appearance of line charts.
void paintAreas(AbstractDiagram::Private *diagramPrivate, PaintContext *ctx, const QModelIndex &index, const QList< QPolygonF > &areas, uint opacity)
static const Position & NorthWest
LineDiagram defines a common line diagram.
Stores information about painting diagrams.
Stores the absolute target points of a Position.
MissingValuesPolicy
MissingValuesPolicy specifies how a missing value will be shown in a line diagram.
QRectF visibleDataRange() const
Returns the currently visible data range.
MissingValuesPolicy missingValuesPolicy() const

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