KChart

KChartTernaryLineDiagram.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartTernaryLineDiagram.h"
10#include "KChartTernaryLineDiagram_p.h"
11
12#include <limits>
13
14#include <QPainter>
15
16#include <KChartPaintContext.h>
17
18#include "KChartLineAttributes.h"
20#include "KChartMarkerAttributes.h"
21#include "TernaryPoint.h"
22#include "TernaryConstants.h"
23#include "KChartPainterSaver_p.h"
24
25using namespace KChart;
26
27#define d d_func()
28
29TernaryLineDiagram::Private::Private()
30 : AbstractTernaryDiagram::Private()
31{
32}
33
34TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
36 : AbstractTernaryDiagram( new Private(), parent, plane )
37{
38 init();
39 setDatasetDimensionInternal( 3 ); // the third column is implicit
40
41 DataValueAttributes dataValueAttributes;
42 dataValueAttributes.setVisible( true );
43 MarkerAttributes markerAttributes;
44 markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
45 markerAttributes.setVisible( true );
46 dataValueAttributes.setMarkerAttributes( markerAttributes );
47 attributesModel()->setDefaultForRole(
48 KChart::DataValueLabelAttributesRole,
49 QVariant::fromValue( dataValueAttributes ) );
50}
51
52TernaryLineDiagram::~TernaryLineDiagram()
53{
54}
55
56void TernaryLineDiagram::init()
57{
58}
59
64
66{
67 d->reverseMapper.clear();
68
69 d->paint( paintContext );
70 // sanity checks:
71 if ( model() == nullptr ) return;
72
73 QPainter* p = paintContext->painter();
74 PainterSaver s( p );
75
77 (TernaryCoordinatePlane*) paintContext->coordinatePlane();
78 Q_ASSERT( plane );
79
80 qreal x, y, z;
81
82 // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
84
85 d->forgetAlreadyPaintedDataValues();
86
87 int columnCount = model()->columnCount( rootIndex() );
89 for (int column=0; column<columnCount; column+=datasetDimension() )
90 {
91 int numrows = model()->rowCount( rootIndex() );
92 for ( int row = 0; row < numrows; row++ )
93 {
94 // see if there is data otherwise skip
95 QModelIndex base = model()->index( row, column ); // checked
96 if ( ! model()->data( base ).isNull() )
97 {
98 p->setPen( PrintingParameters::scalePen( pen( base ) ) );
99 p->setBrush( brush( base ) );
100
101 // retrieve data
102 x = qMax<qreal>( model()->data( model()->index( row, column, rootIndex() ) ).toReal(), // checked
103 0.0 );
104 y = qMax<qreal>( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
105 0.0 );
106 z = qMax<qreal>( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
107 0.0 );
108
109 qreal total = x + y + z;
110 if ( fabs( total ) > 3 * std::numeric_limits<qreal>::epsilon() ) {
111 TernaryPoint tPunkt( x / total, y / total );
112 QPointF diagramLocation = translate( tPunkt );
113 QPointF widgetLocation = plane->translate( diagramLocation );
114
115 if ( row > 0 ) {
116 p->drawLine( start, widgetLocation );
117 }
118 paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
119 start = widgetLocation;
120 // retrieve text and data value attributes
121 // FIXME use data model DisplayRole text
122 QString text = tr( "(%1, %2, %3)", "(x, y, z) values of the data point" )
123 .arg( x * 100, 0, 'f', 0 )
124 .arg( y * 100, 0, 'f', 0 )
125 .arg( z * 100, 0, 'f', 0 );
126 d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
127 } else {
128 // ignore and do not paint this point, garbage data
129 qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
130 << x << "/" << y << "/" << z << "ignored, unusable.";
131 }
132 }
133 }
134 }
135}
136
137const QPair< QPointF, QPointF > TernaryLineDiagram::calculateDataBoundaries () const
138{
139 // this is a constant, because we defined it to be one:
140 static QPair<QPointF, QPointF> Boundaries(
141 TriangleBottomLeft,
142 QPointF( TriangleBottomRight.x(), TriangleHeight ) );
143 return Boundaries;
144}
Declaring the class KChart::DataValueAttributes.
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
int datasetDimension() const
The dataset dimension of a diagram determines how many value dimensions it expects each datapoint to ...
Base class for diagrams based on a ternary coordinate plane.
Diagram attributes dealing with data value labels.
void setMarkerAttributes(const MarkerAttributes &a)
Set the marker attributes to use for the data values.
void setVisible(bool visible)
Set whether data value labels should be displayed.
A set of attributes controlling the appearance of data set markers.
void setMarkerStyle(uint style)
Set the marker-style to use.
Stores information about painting diagrams.
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
void resize(const QSizeF &area) override
Called by the widget's sizeEvent.
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
TernaryPoint defines a point within a ternary coordinate plane.
Q_SCRIPTABLE Q_NOREPLY void start()
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
virtual int columnCount(const QModelIndex &parent) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
QAbstractItemModel * model() const const
QModelIndex rootIndex() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void drawLine(const QLine &line)
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
qreal x() const const
QString arg(Args &&... args) const const
QVariant fromValue(T &&value)
void resize(const QSize &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:51:58 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.