9#include "KChartTernaryGrid.h"
17#include <QApplication>
19#include "TernaryPoint.h"
20#include "TernaryConstants.h"
21#include "KChartPaintContext.h"
22#include "KChartPainterSaver_p.h"
23#include "KChartTernaryCoordinatePlane.h"
24#include "KChartPrintingParameters.h"
28TickInfo::TickInfo( qreal _percentage,
int _depth )
29 : percentage ( _percentage )
34bool KChart::operator==(
const TickInfo& left,
const TickInfo& right)
36 return fabs(
left.percentage -
right.percentage )
37 <= std::numeric_limits<qreal>::epsilon()
41TernaryGrid::TernaryGrid()
46TernaryGrid::~TernaryGrid()
52 static const int GridLineDistanceThreshold = 20;
54 QPainter& painter = *context->painter();
55 PainterSaver s( &painter );
56 TernaryCoordinatePlane* plane =
dynamic_cast<TernaryCoordinatePlane*
>(context->coordinatePlane());
57 Q_ASSERT_X ( plane,
"TernaryGrid::drawGrid",
58 "Bad function call: PaintContext::coodinatePlane() NOT a ternary plane." );
61 const int MaxDepth = 3;
62 qreal xPixels = plane->
translate( TriangleBottomRight ).
x() -
65 if ( xPixels > 10 * GridLineDistanceThreshold ) granularity = 10;
66 if ( xPixels > 20 * GridLineDistanceThreshold ) granularity = 5;
69 for (
int i = granularity; i < 100; i+=granularity )
71 TickInfo tick( ( 1.0 * i ) / 100.0, 2 );
72 if ( i % 10 == 0 ) tick.depth = 1;
73 if ( i % 20 == 0 ) tick.depth = 0;
74 m_tickInfo.append( tick );
77 QVector<QLineF> lines[MaxDepth];
78 {
for (
const TickInfo& tick : std::as_const(m_tickInfo) ) {
79 const qreal& percent = tick.percentage;
81 TernaryPoint ternaryStart( percent, 1.0 - percent );
82 TernaryPoint ternaryEnd( 0.0, 1.0 - percent );
83 QPointF
start( translate( ternaryStart ) );
84 QPointF
end( translate( ternaryEnd ) );
89 TernaryPoint ternaryStart( percent, 0.0 );
90 TernaryPoint ternaryEnd( 0.0, percent );
91 QPointF
start( translate( ternaryStart ) );
92 QPointF
end( translate( ternaryEnd ) );
97 TernaryPoint ternaryStart( percent, 1.0 - percent );
98 TernaryPoint ternaryEnd( percent, 0.0 );
99 QPointF
start( translate( ternaryStart ) );
100 QPointF
end( translate( ternaryEnd ) );
107 painter.
setPen( PrintingParameters::scalePen( QPen( QColor(
"lightgray" ), 1 ) ) );
108 painter.
setBrush( QColor(
"lightgray" ) );
110 painter.
setPen( PrintingParameters::scalePen( QPen( QColor(
"gray" ), 1 ) ) );
111 painter.
setBrush( QColor(
"gray" ) );
113 painter.
setPen( PrintingParameters::scalePen( QPen( QColor(
"darkslategray" ), 1 ) ) );
114 painter.
setBrush( QColor(
"darkslategray" ) );
118 painter.
setPen( PrintingParameters::scalePen( QPen(
Qt::black, 1 ) ) );
123 points << plane->
translate( TriangleBottomLeft )
124 << plane->
translate( TriangleBottomRight )
132 QVector<QLineF> ticks;
134 QVector<TickInfo> percentages( m_tickInfo );
143 percentages.erase( std::unique( percentages.begin(), percentages.end() ),
146 {
for (
const TickInfo& tick : std::as_const(percentages) ) {
147 const qreal& percent = tick.percentage;
149 const QPointF markerDistance( FullMarkerDistanceBC
150 / ( tick.depth + 1 ) );
151 QPointF
start( percent, 0.0 );
156 const QPointF markerDistance( FullMarkerDistanceAC
157 / ( tick.depth + 1 ) );
158 const QPointF
start( TriangleBottomRight + percent * AxisVector_C_A );
159 const QPointF
end(
start + markerDistance );
165 const QPointF markerDistance( FullMarkerDistanceBA
166 / ( tick.depth +1 ) );
167 const QPointF
start( percent * AxisVector_B_A );
168 const QPointF
end(
start + markerDistance );
176DataDimensionsList TernaryGrid::calculateGrid(
const DataDimensionsList& )
const
178 return DataDimensionsList();
181QPair<QSizeF, QSizeF> TernaryGrid::requiredMargins()
const
184 qreal topMargin = 0.0;
185 qreal leftMargin = fabs( FullMarkerDistanceBA.
x() );
186 qreal bottomMargin = fabs( FullMarkerDistanceBC.
y() );
194 return QPair<QSizeF, QSizeF>
195 ( QSizeF( leftMargin, topMargin ),
196 QSizeF( leftMargin, bottomMargin ) );
199const QVector<TickInfo>& TernaryGrid::tickInfo()
const
Abstract base class for grid classes: cartesian, polar, ...
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.
Q_SCRIPTABLE Q_NOREPLY void start()
const QList< QKeySequence > & end()
void append(QList< T > &&value)
void drawLines(const QLine *lines, int lineCount)
void drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule)
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)