KD Chart 2  [rev.2.7]
KDChartStackedBarDiagram_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 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 <QModelIndex>
24 
25 #include "KDChartBarDiagram.h"
26 #include "KDChartTextAttributes.h"
27 #include "KDChartAttributesModel.h"
29 #include "KDChartStackedBarDiagram_p.h"
30 
31 using namespace KDChart;
32 
33 StackedBarDiagram::StackedBarDiagram( BarDiagram* d )
34  : BarDiagramType( d )
35 {
36 }
37 
38 BarDiagram::BarType StackedBarDiagram::type() const
39 {
40  return BarDiagram::Stacked;
41 }
42 
43 const QPair<QPointF, QPointF> StackedBarDiagram::calculateDataBoundaries() const
44 {
45  const int rowCount = compressor().modelDataRows();
46  const int colCount = compressor().modelDataColumns();
47 
48  const qreal xMin = 0.0;
49  const qreal xMax = rowCount;
50  qreal yMin = 0.0;
51  qreal yMax = 0.0;
52 
53  bool isFirst = true;
54  for ( int row = 0; row < rowCount; ++row ) {
55  // calculate sum of values per column - Find out stacked Min/Max
56  qreal stackedValues = 0.0;
57  qreal negativeStackedValues = 0.0;
58  for ( int col = 0; col < colCount; ++col ) {
59  const CartesianDiagramDataCompressor::CachePosition position( row, col );
60  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
61  const double value = ISNAN( point.value ) ? 0.0 : point.value;
62 
63  if ( value > 0.0 ) {
64  stackedValues += value;
65  } else {
66  negativeStackedValues += value;
67  }
68 
69  // this is always true yMin can be 0 in case all values
70  // are the same
71  // same for yMax it can be zero if all values are negative
72  if ( isFirst ) {
73  yMin = negativeStackedValues < 0.0 ? negativeStackedValues : stackedValues;
74  yMax = stackedValues > 0.0 ? stackedValues : negativeStackedValues;
75  isFirst = false;
76  } else {
77  yMin = qMin( qMin( yMin, stackedValues ), negativeStackedValues );
78  yMax = qMax( qMax( yMax, stackedValues ), negativeStackedValues );
79  }
80  }
81  }
82 
83  // special cases
84  if ( yMax == yMin ) {
85  if ( yMin == 0.0 ) {
86  yMax = 0.1; // we need at least a range
87  } else if ( yMax < 0.0 ) {
88  yMax = 0.0; // extend the range to zero
89  } else if ( yMin > 0.0 ) {
90  yMin = 0.0; // dito
91  }
92  }
93 
94  return QPair< QPointF, QPointF >( QPointF( xMin, yMin ), QPointF( xMax, yMax ) );
95 }
96 
97 void StackedBarDiagram::paint( PaintContext* ctx )
98 {
99  reverseMapper().clear();
100 
101  const QPair<QPointF,QPointF> boundaries = diagram()->dataBoundaries(); // cached
102 
103  const QPointF boundLeft = ctx->coordinatePlane()->translate( boundaries.first ) ;
104  const QPointF boundRight = ctx->coordinatePlane()->translate( boundaries.second );
105 
106  const int rowCount = compressor().modelDataRows();
107  const int colCount = compressor().modelDataColumns();
108 
109  BarAttributes ba = diagram()->barAttributes();
110  qreal barWidth = 0;
111  qreal maxDepth = 0;
112  qreal width = boundRight.x() - boundLeft.x();
113  qreal groupWidth = width / rowCount;
114  qreal spaceBetweenBars = 0;
115  qreal spaceBetweenGroups = 0;
116 
117  if ( ba.useFixedBarWidth() ) {
118  barWidth = ba.fixedBarWidth();
119  groupWidth += barWidth;
120 
121  // Pending Michel set a min and max value for the groupWidth
122  // related to the area.width
123  if ( groupWidth < 0 )
124  groupWidth = 0;
125 
126  if ( groupWidth * rowCount > width )
127  groupWidth = width / rowCount;
128  }
129 
130  // maxLimit: allow the space between bars to be larger until area.width()
131  // is covered by the groups.
132  qreal maxLimit = rowCount * (groupWidth + ((colCount-1) * ba.fixedDataValueGap()) );
133 
134 
135  //Pending Michel: FixMe
136  if ( ba.useFixedDataValueGap() ) {
137  if ( width > maxLimit )
138  spaceBetweenBars += ba.fixedDataValueGap();
139  else
140  spaceBetweenBars = ((width/rowCount) - groupWidth)/(colCount-1);
141  }
142 
143  if ( ba.useFixedValueBlockGap() )
144  spaceBetweenGroups += ba.fixedValueBlockGap();
145 
146  calculateValueAndGapWidths( rowCount, colCount,groupWidth,
147  barWidth, spaceBetweenBars, spaceBetweenGroups );
148 
149  LabelPaintCache lpc;
150  for ( int col = 0; col < colCount; ++col )
151  {
152  qreal offset = spaceBetweenGroups;
153  if ( ba.useFixedBarWidth() )
154  offset -= ba.fixedBarWidth();
155 
156  if ( offset < 0 )
157  offset = 0;
158 
159  for ( int row = 0; row < rowCount; ++row )
160  {
161  const CartesianDiagramDataCompressor::CachePosition position( row, col );
162  const CartesianDiagramDataCompressor::DataPoint p = compressor().data( position );
163 
164  const QModelIndex index = attributesModel()->mapToSource( p.index );
165  ThreeDBarAttributes threeDAttrs = diagram()->threeDBarAttributes( index );
166  const qreal value = p.value;
167  qreal stackedValues = 0.0;
168  qreal key = 0.0;
169 
170  if ( threeDAttrs.isEnabled() ) {
171  if ( barWidth > 0 )
172  barWidth = (width - ((offset+(threeDAttrs.depth()))*rowCount))/ rowCount;
173  if ( barWidth <= 0 ) {
174  barWidth = 0;
175  maxDepth = offset - (width/rowCount);
176  }
177  } else {
178  barWidth = (width - (offset*rowCount))/ rowCount ;
179  }
180 
181  for ( int k = col; k >= 0; --k )
182  {
183  const CartesianDiagramDataCompressor::CachePosition position( row, k );
184  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
185  if ( !ISNAN( point.value ) && (( p.value >= 0.0 && point.value >= 0.0 ) || ( p.value < 0.0 && point.value < 0.0 )) )
186  stackedValues += point.value;
187  key = point.key;
188  }
189 
190  if (!ISNAN( value ))
191  {
192  const qreal usedDepth = threeDAttrs.depth();
193 
194  QPointF point = ctx->coordinatePlane()->translate( QPointF( key, stackedValues ) );
195 
196  const qreal dy = point.y() - usedDepth;
197  if ( dy < 0 ) {
198  threeDAttrs.setDepth( point.y() - 1 );
199  diagram()->setThreeDBarAttributes( threeDAttrs );
200  }
201 
202  point.rx() += offset / 2;
203  const QPointF previousPoint = ctx->coordinatePlane()->translate( QPointF( key, stackedValues - value ) );
204  const qreal barHeight = previousPoint.y() - point.y();
205 
206  const QRectF rect( point, QSizeF( barWidth , barHeight ) );
207  m_private->addLabel( &lpc, index, 0, PositionPoints( rect ), Position::North,
208  Position::South, value );
209  paintBars( ctx, index, rect, maxDepth );
210  }
211  }
212  }
213  m_private->paintDataValueTextsAndMarkers( ctx, lpc, false );
214 }
virtual const QPointF translate(const QPointF &diagramPoint) const =0
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 bar charts.
static const Position & South
BarDiagram defines a common bar diagram.
Stores information about painting diagrams.
A set of 3D bar attributes.
Stores the absolute target points of a Position.
static const Position & North

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/