12 #include "KDChartRadarDiagram_p.h"
15 #include "KDChartPainterSaver_p.h"
18 #include <KDABLibFakes>
22 RadarDiagram::Private::Private()
26 RadarDiagram::Private::~Private()
42 void RadarDiagram::init()
53 newDiagram->d->closeDatasets =
d->closeDatasets;
60 return QPair<QPointF, QPointF>(QPointF(0, 0), QPointF(0, 0));
61 const int rowCount = model()->rowCount(rootIndex());
62 const int colCount = model()->columnCount(rootIndex());
64 qreal xMax = colCount;
65 qreal yMin = 0, yMax = 0;
66 for (
int iCol = 0; iCol < colCount; ++iCol) {
67 for (
int iRow = 0; iRow <
rowCount; ++iRow) {
68 qreal value = model()->data(model()->index(iRow, iCol, rootIndex())).toReal();
69 yMax = qMax(yMax, value);
70 yMin = qMin(yMin, value);
73 QPointF bottomLeft(QPointF(xMin, yMin));
74 QPointF topRight(QPointF(xMax, yMax));
75 return QPair<QPointF, QPointF>(bottomLeft, topRight);
80 QPainter painter(viewport());
90 paint(ctx,
true, dummy1, dummy2);
91 paint(ctx,
false, dummy1, dummy2);
97 const qreal origResult = f.pointSizeF();
98 qreal result = origResult;
99 const QSizeF mySize = geometry.size();
103 const QString t = text;
106 const QSizeF textSize =
rotatedRect(fm.boundingRect(t), ta.
rotation()).normalized().size();
108 if (textSize.height() <= mySize.height() && textSize.width() <= mySize.width())
114 f.setPointSizeF(result);
115 fm = QFontMetrics(f);
121 QPointF result = plane.
translate(origin);
122 result -= sourceRect.topLeft();
123 result.setX(result.x() / sourceRect.width() * destRect.width());
124 result.setY(result.y() / sourceRect.height() * destRect.height());
125 result += destRect.topLeft();
131 d->reverseData = val;
135 return d->reverseData;
144 Polygon(
const QPolygonF &polygon,
const QBrush &brush,
const QPen &pen)
153 bool calculateListAndReturnScale,
154 qreal &newZoomX, qreal &newZoomY)
160 d->reverseMapper.clear();
162 const int rowCount = model()->rowCount(rootIndex());
163 const int colCount = model()->columnCount(rootIndex());
174 fontRect.setSize(QSizeF(fontRect.width(), step / 2.0));
176 QFont labelFont = ta.
font();
178 labelFont.setPointSizeF(labelFontSize);
179 const QFontMetricsF metric(labelFont);
180 const qreal labelHeight = metric.height();
183 destRect.setY(destRect.y() + 2 * labelHeight);
184 destRect.setHeight(destRect.height() - 4 * labelHeight);
187 if (calculateListAndReturnScale) {
191 d->labelPaintCache.clear();
193 for (iCol = 0; iCol < colCount; ++iCol) {
194 for (iRow = 0; iRow <
rowCount; ++iRow) {
195 QModelIndex index = model()->index(iRow, iCol, rootIndex());
196 const qreal value = model()->data(index).toReal();
207 if (
d->labelPaintCache.paintReplay.count()) {
209 d->paintDataValueTextsAndMarkers(ctx,
d->labelPaintCache,
true,
true, &txtRectF);
210 const QRect txtRect = txtRectF.toRect();
212 const qreal gapX = qMin(txtRect.left() - curRect.left(), curRect.right() - txtRect.right());
213 const qreal gapY = qMin(txtRect.top() - curRect.top(), curRect.bottom() - txtRect.bottom());
217 newZoomX *= 1.0 + (gapX - 1.0) / curRect.width();
219 newZoomY *= 1.0 + (gapY - 1.0) / curRect.height();
225 QList<Polygon> polygons;
226 for (iCol = 0; iCol < colCount; ++iCol) {
233 for (iRow = 0; iRow <
rowCount; ++iRow) {
234 QModelIndex index = model()->index(iRow, iCol, rootIndex());
235 const qreal value = model()->data(index).toReal();
237 polygon.append(point);
242 polygon.append(point0);
246 if (p.style() != Qt::NoPen) {
252 if (
d->fillAlpha > 0.0) {
253 for (
const Polygon &p : polygons) {
254 PainterSaver painterSaver(ctx->
painter());
255 ctx->
painter()->setRenderHint(QPainter::Antialiasing);
257 QColor c = br.color();
258 c.setAlphaF(
d->fillAlpha);
262 ctx->
painter()->drawPolygon(p.polygon);
267 for (
const Polygon &p : polygons) {
268 PainterSaver painterSaver(ctx->
painter());
269 ctx->
painter()->setRenderHint(QPainter::Antialiasing);
270 ctx->
painter()->setBrush(p.brush);
272 ctx->
painter()->drawPolyline(p.polygon);
275 d->paintDataValueTextsAndMarkers(ctx,
d->labelPaintCache,
true);
286 return model()->rowCount(rootIndex());
292 return model() ? model()->rowCount(rootIndex()) : 0.0;
308 return d->closeDatasets;
318 d->fillAlpha = alphaF;
QRectF rotatedRect(const QRectF &rect, qreal rotation)
static QPointF scaleToRealPosition(const QPointF &origin, const QRectF &sourceRect, const QRectF &destRect, const AbstractCoordinatePlane &plane)
static qreal fitFontSizeToGeometry(const QString &text, const QFont &font, const QRectF &geometry, const TextAttributes &ta)
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane,...
virtual const QPointF translate(const QPointF &diagramPoint) const =0
virtual qreal zoomFactorY() const
virtual qreal zoomFactorX() const
QRect geometry() const override
virtual bool checkInvariants(bool justReturnTheStatus=false) const
const QPair< QPointF, QPointF > dataBoundaries() const
Return the bottom left and top right data point, that the diagram will display (unless the grid adjus...
AbstractCoordinatePlane * coordinatePlane() const
Base class for diagrams based on a polar coordinate system.
Stores information about painting diagrams.
void setPainter(QPainter *painter)
void setRectangle(const QRectF &rect)
const QRectF rectangle() const
AbstractCoordinatePlane * coordinatePlane() const
QPainter * painter() const
Stores the absolute target points of a Position.
static const Position & Center
static QPen scalePen(const QPen &pen)
RadarDiagram defines a common radar diagram.
virtual RadarDiagram * clone() const
void setReverseData(bool val)
qreal numberOfGridRings() const override
void resizeEvent(QResizeEvent *) override
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
void resize(const QSizeF &area) override
qreal numberOfValuesPerDataset() const override
void paintEvent(QPaintEvent *) override
void setFillAlpha(qreal alphaF)
virtual void paint(PaintContext *paintContext, bool calculateListAndReturnScale, qreal &newZoomX, qreal &newZoomY)
qreal valueTotals() const override
bool closeDatasets() const
RadarDiagram(QWidget *parent=nullptr, RadarCoordinatePlane *plane=nullptr)
void setCloseDatasets(bool closeDatasets)
A set of text attributes.