KD Chart 2  [rev.2.7]
KDChartPercentLyingBarDiagram_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 "KDChartPercentLyingBarDiagram_p.h"
24 
25 #include <QModelIndex>
26 
27 #include "KDChartBarDiagram.h"
28 #include "KDChartTextAttributes.h"
29 #include "KDChartAttributesModel.h"
31 
32 using namespace KDChart;
33 
34 PercentLyingBarDiagram::PercentLyingBarDiagram( BarDiagram* d )
35  : BarDiagramType( d )
36 {
37 }
38 
39 BarDiagram::BarType PercentLyingBarDiagram::type() const
40 {
41  return BarDiagram::Percent;
42 }
43 
44 const QPair<QPointF, QPointF> PercentLyingBarDiagram::calculateDataBoundaries() const
45 {
46  //const int rowCount = compressor().modelDataRows();
47  //const int colCount = compressor().modelDataColumns();
48 
49  const qreal xMin = 0;
50  const qreal xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) : 0;
51  qreal yMin = 0.0, yMax = 100.0;
52  /*for ( int col = 0; col < colCount; ++col )
53  {
54  for ( int row = 0; row < rowCount; ++row )
55  {
56  // Ordinate should begin at 0 the max value being the 100% pos
57  const QModelIndex idx = diagram()->model()->index( row, col, diagram()->rootIndex() );
58  // only positive values are handled
59  qreal value = diagram()->model()->data( idx ).toReal();
60  if ( value > 0 )
61  yMax = qMax( yMax, value );
62  }
63  }*/
64  // special cases
65  if ( yMax == yMin ) {
66  if ( yMin == 0.0 )
67  yMax = 0.1; //we need at least a range
68  else
69  yMax = 0.0; // they are the same but negative
70  }
71  const QPointF bottomLeft( QPointF( yMin, xMin ) );
72  const QPointF topRight( QPointF( yMax, xMax ) );
73 
74  //qDebug() << "BarDiagram::calculateDataBoundaries () returns ( " << bottomLeft << topRight <<")";
75  return QPair< QPointF, QPointF >( bottomLeft, topRight );
76 }
77 
78 void PercentLyingBarDiagram::paint( PaintContext* ctx )
79 {
80  reverseMapper().clear();
81 
82  const QPair<QPointF,QPointF> boundaries = diagram()->dataBoundaries(); // cached
83 
84  const QPointF boundLeft = ctx->coordinatePlane()->translate( boundaries.first ) ;
85  const QPointF boundRight = ctx->coordinatePlane()->translate( boundaries.second );
86 
87  const int rowCount = compressor().modelDataRows();
88  const int colCount = compressor().modelDataColumns();
89 
90  BarAttributes ba = diagram()->barAttributes();
91  qreal barWidth = 0;
92  qreal maxDepth = 0;
93  qreal width = boundLeft.y() - boundRight.y();
94  qreal groupWidth = width / rowCount;
95  qreal spaceBetweenBars = 0;
96  qreal spaceBetweenGroups = 0;
97 
98  if ( ba.useFixedBarWidth() ) {
99  barWidth = ba.fixedBarWidth();
100  groupWidth += barWidth;
101 
102  // Pending Michel set a min and max value for the groupWidth
103  // related to the area.width
104  if ( groupWidth < 0 )
105  groupWidth = 0;
106 
107  if ( groupWidth * rowCount > width )
108  groupWidth = width / rowCount;
109  }
110 
111  // maxLimit: allow the space between bars to be larger until area.width()
112  // is covered by the groups.
113  qreal maxLimit = rowCount * (groupWidth + ((colCount-1) * ba.fixedDataValueGap()) );
114 
115 
116  //Pending Michel: FixMe
117  if ( ba.useFixedDataValueGap() ) {
118  if ( width > maxLimit )
119  spaceBetweenBars += ba.fixedDataValueGap();
120  else
121  spaceBetweenBars = ((ctx->rectangle().width()/rowCount) - groupWidth)/(colCount-1);
122  }
123 
124  if ( ba.useFixedValueBlockGap() )
125  spaceBetweenGroups += ba.fixedValueBlockGap();
126 
127  calculateValueAndGapWidths( rowCount, colCount,groupWidth,
128  barWidth, spaceBetweenBars, spaceBetweenGroups );
129 
130  LabelPaintCache lpc;
131  const qreal maxValue = 100.0; // always 100 %
132  qreal sumValues = 0;
133  QVector <qreal > sumValuesVector;
134 
135  //calculate sum of values for each column and store
136  for ( int row = 0; row < rowCount; ++row )
137  {
138  for ( int col = 0; col < colCount; ++col )
139  {
140  const CartesianDiagramDataCompressor::CachePosition position( row, col );
141  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
142  //if ( point.value > 0 )
143  sumValues += qMax( point.value, -point.value );
144  if ( col == colCount - 1 ) {
145  sumValuesVector << sumValues ;
146  sumValues = 0;
147  }
148  }
149  }
150 
151  // calculate stacked percent value
152  for ( int curRow = rowCount - 1; curRow >= 0; --curRow )
153  {
154  qreal offset = spaceBetweenGroups;
155  if ( ba.useFixedBarWidth() )
156  offset -= ba.fixedBarWidth();
157 
158  if ( offset < 0 )
159  offset = 0;
160 
161  for ( int col = 0; col < colCount ; ++col )
162  {
163  qreal threeDOffset = 0.0;
164  const CartesianDiagramDataCompressor::CachePosition position( curRow, col );
165  const CartesianDiagramDataCompressor::DataPoint p = compressor().data( position );
166  QModelIndex sourceIndex = attributesModel()->mapToSource( p.index );
167  ThreeDBarAttributes threeDAttrs = diagram()->threeDBarAttributes( sourceIndex );
168 
169  if ( threeDAttrs.isEnabled() ) {
170  if ( barWidth > 0 ) {
171  barWidth = (width - ((offset+(threeDAttrs.depth()))*rowCount))/ rowCount;
172  threeDOffset = threeDAttrs.depth();
173  }
174  if ( barWidth <= 0 ) {
175  barWidth = 0.1;
176  threeDOffset = (width - (offset*rowCount))/ rowCount;
177  }
178  } else {
179  barWidth = (width - (offset*rowCount))/ rowCount;
180  }
181 
182  const qreal value = qMax( p.value, -p.value );
183  qreal stackedValues = 0.0;
184  qreal key = 0.0;
185 
186  // calculate stacked percent value
187  // we only take in account positives values for now.
188  for ( int k = col; k >= 0 ; --k )
189  {
190  const CartesianDiagramDataCompressor::CachePosition position( curRow, k );
191  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
192  stackedValues += qMax( point.value, -point.value );
193  key = point.key;
194  }
195 
196  QPointF point, previousPoint;
197  if ( sumValuesVector.at( curRow ) != 0 && value > 0 ) {
198  QPointF dataPoint( ( stackedValues / sumValuesVector.at( curRow ) * maxValue ), key );
199  point = ctx->coordinatePlane()->translate( dataPoint );
200  point.ry() -= offset + threeDOffset;
201 
202  previousPoint = ctx->coordinatePlane()->translate( QPointF( ( ( stackedValues - value) / sumValuesVector.at( curRow ) * maxValue ), key ) );
203  }
204 
205  const qreal barHeight = point.x() - previousPoint.x();
206 
207  point.setX( point.x() - barHeight );
208 
209  const QRectF rect = QRectF( point, QSizeF( barHeight, barWidth ) ).translated( 1, 0 );
210  m_private->addLabel( &lpc, sourceIndex, 0, PositionPoints( rect ), Position::North,
211  Position::South, value );
212  paintBars( ctx, sourceIndex, rect, maxDepth );
213  }
214  }
215  m_private->paintDataValueTextsAndMarkers( ctx, lpc, false );
216 }
const QRectF rectangle() const
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/