24 #include "KDChartRadarDiagram_p.h" 28 #include "KDChartPainterSaver_p.h" 30 #include <KDABLibFakes> 34 RadarDiagram::Private::Private() :
35 closeDatasets( false ),
41 RadarDiagram::Private::~Private() {}
56 void RadarDiagram::init()
75 const int rowCount = model()->rowCount(rootIndex());
76 const int colCount = model()->columnCount(rootIndex());
78 qreal xMax = colCount;
79 qreal yMin = 0, yMax = 0;
80 for (
int iCol=0; iCol<colCount; ++iCol ) {
81 for (
int iRow=0; iRow<
rowCount; ++iRow ) {
82 qreal value = model()->data( model()->index( iRow, iCol, rootIndex() ) ).toReal();
83 yMax = qMax( yMax, value );
84 yMin = qMin( yMin, value );
87 QPointF bottomLeft ( QPointF( xMin, yMin ) );
88 QPointF topRight ( QPointF( xMax, yMax ) );
96 QPainter painter ( viewport() );
105 qreal dummy1, dummy2;
106 paint( ctx,
true, dummy1, dummy2 );
107 paint( ctx,
false, dummy1, dummy2 );
113 const qreal origResult = f.pointSizeF();
114 qreal result = origResult;
115 const QSizeF mySize = geometry.size();
116 if ( mySize.isNull() )
119 const QString t = text;
120 QFontMetrics fm( f );
123 const QSizeF textSize =
rotatedRect( fm.boundingRect( t ), ta.
rotation() ).normalized().size();
125 if ( textSize.height() <= mySize.height() && textSize.width() <= mySize.width() )
131 f.setPointSizeF( result );
132 fm = QFontMetrics( f );
138 QPointF result = plane.
translate( origin );
139 result -= sourceRect.topLeft();
140 result.setX( result.x() / sourceRect.width() * destRect.width() );
141 result.setY( result.y() / sourceRect.height() * destRect.height() );
142 result += destRect.topLeft();
148 d->reverseData = val;
152 return d->reverseData;
160 Polygon(
const QPolygonF &polygon,
const QBrush &brush,
const QPen &pen) : polygon(polygon),
brush(brush),
pen(pen) {}
164 bool calculateListAndReturnScale,
165 qreal& newZoomX, qreal& newZoomY )
171 d->reverseMapper.clear();
173 const int rowCount = model()->rowCount( rootIndex() );
174 const int colCount = model()->columnCount( rootIndex() );
185 fontRect.setSize( QSizeF( fontRect.width(), step / 2.0 ) );
187 QFont labelFont = ta.
font();
189 labelFont.setPointSizeF( labelFontSize );
190 const QFontMetricsF metric( labelFont );
191 const qreal labelHeight = metric.height();
196 destRect.setY( destRect.y() + 2 * labelHeight );
197 destRect.setHeight( destRect.height() - 4 * labelHeight );
200 if ( calculateListAndReturnScale ) {
204 d->labelPaintCache.clear();
206 for ( iCol=0; iCol < colCount; ++iCol ) {
207 for ( iRow=0; iRow <
rowCount; ++iRow ) {
208 QModelIndex index = model()->index( iRow, iCol, rootIndex() );
209 const qreal value = model()->data( index ).toReal();
220 if (
d->labelPaintCache.paintReplay.count() ) {
222 d->paintDataValueTextsAndMarkers( ctx,
d->labelPaintCache,
true,
true, &txtRectF );
223 const QRect txtRect = txtRectF.toRect();
225 const qreal gapX = qMin( txtRect.left() - curRect.left(), curRect.right() - txtRect.right() );
226 const qreal gapY = qMin( txtRect.top() - curRect.top(), curRect.bottom() - txtRect.bottom() );
230 newZoomX *= 1.0 + (gapX-1.0) / curRect.width();
232 newZoomY *= 1.0 + (gapY-1.0) / curRect.height();
239 for ( iCol=0; iCol < colCount; ++iCol ) {
246 for ( iRow=0; iRow <
rowCount; ++iRow ) {
247 QModelIndex index = model()->index( iRow, iCol, rootIndex() );
248 const qreal value = model()->data( index ).toReal();
250 polygon.append( point );
255 polygon.append( point0 );
259 if ( p.style() != Qt::NoPen )
266 if (
d->fillAlpha > 0.0) {
267 Q_FOREACH(
const Polygon& p, polygons) {
268 PainterSaver painterSaver( ctx->
painter() );
269 ctx->
painter()->setRenderHint ( QPainter::Antialiasing );
271 QColor c = br.color();
272 c.setAlphaF(
d->fillAlpha);
274 ctx->
painter()->setBrush( br );
275 ctx->
painter()->setPen( p.pen );
276 ctx->
painter()->drawPolygon( p.polygon );
281 Q_FOREACH(
const Polygon& p, polygons) {
282 PainterSaver painterSaver( ctx->
painter() );
283 ctx->
painter()->setRenderHint ( QPainter::Antialiasing );
284 ctx->
painter()->setBrush( p.brush );
285 ctx->
painter()->setPen( p.pen );
286 ctx->
painter()->drawPolyline( p.polygon );
289 d->paintDataValueTextsAndMarkers( ctx,
d->labelPaintCache,
true );
300 return model()->rowCount(rootIndex());
306 return model() ? model()->rowCount(rootIndex()) : 0.0;
322 return d->closeDatasets;
332 d->fillAlpha = alphaF;
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane, TernaryCoordinatePlane.
const QRectF rectangle() const
void setPainter(QPainter *painter)
void paintEvent(QPaintEvent *) override
virtual bool checkInvariants(bool justReturnTheStatus=false) const
void setCloseDatasets(bool closeDatasets)
Close each of the data series by connecting the last point to its respective start point...
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
QRect geometry() const override
pure virtual in QLayoutItem
QPainter * painter() const
static const Position & Center
AbstractCoordinatePlane * coordinatePlane() const
The coordinate plane associated with the diagram.
static QPointF scaleToRealPosition(const QPointF &origin, const QRectF &sourceRect, const QRectF &destRect, const AbstractCoordinatePlane &plane)
virtual RadarDiagram * clone() const
Creates an exact copy of this diagram.
Base class for diagrams based on a polar coordinate system.
QRectF rotatedRect(const QRectF &rect, qreal rotation)
RadarDiagram defines a common radar diagram.
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
const QPair< QPointF, QPointF > dataBoundaries() const
Return the bottom left and top right data point, that the diagram will display (unless the grid adjus...
virtual void paint(PaintContext *paintContext, bool calculateListAndReturnScale, qreal &newZoomX, qreal &newZoomY)
qreal numberOfGridRings() const override
[reimplemented]
void setReverseData(bool val)
if val is true the diagram will mirror the diagram datapoints
qreal valueTotals() const override
[reimplemented]
bool closeDatasets() const
Stores information about painting diagrams.
void resize(const QSizeF &area) override
[reimplemented]
Stores the absolute target points of a Position.
virtual qreal zoomFactorY() const
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
qreal fillAlpha() const
Fill the areas of the radar chart with there respective color defined via KDChart::DatasetBrushRole.
qreal numberOfValuesPerDataset() const override
[reimplemented]
static QPen scalePen(const QPen &pen)
void setRectangle(const QRectF &rect)
RadarDiagram(QWidget *parent=0, RadarCoordinatePlane *plane=0)
static qreal fitFontSizeToGeometry(const QString &text, const QFont &font, const QRectF &geometry, const TextAttributes &ta)
void setFillAlpha(qreal alphaF)
const TextAttributes textAttributes() const
virtual qreal zoomFactorX() const
void resizeEvent(QResizeEvent *) override
A set of text attributes.
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
[reimplemented]