1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "LineChartNode.h"
9
10#include <QtMath>
11
12#include "LineChartMaterial.h"
13#include "LineSegmentNode.h"
14
15static const int MaxPointsInSegment = 6;
16
17qreal calculateNormalizedLineWidth(qreal pixelWidth, const QRectF &rect)
18{
19 if (qFuzzyIsNull(d: pixelWidth)) {
20 return 0.0;
21 }
22
23 // This needs to account for both height and vertical aspect, resulting in
24 // a formula similar to `height / (height / width)` which funnily enough
25 // simplifies to just width.
26 return pixelWidth * 0.5 / rect.width();
27}
28
29LineChartNode::LineChartNode()
30{
31}
32
33LineChartNode::~LineChartNode()
34{
35}
36
37void LineChartNode::setRect(const QRectF &rect, qreal devicePixelRatio)
38{
39 if (rect == m_rect) {
40 return;
41 }
42
43 m_rect = rect;
44 m_aspect = m_rect.height() / m_rect.width();
45
46 auto nativeSize = QSizeF(m_rect.width() * devicePixelRatio, m_rect.height() * devicePixelRatio);
47 auto diagonal = std::sqrt(x: nativeSize.width() * nativeSize.width() + nativeSize.height() * nativeSize.height());
48 m_smoothing = 1.0 / diagonal;
49}
50
51void LineChartNode::setLineWidth(float width)
52{
53 if (qFuzzyCompare(p1: width, p2: m_lineWidth)) {
54 return;
55 }
56
57 m_lineWidth = width;
58}
59
60void LineChartNode::setLineColor(const QColor &color)
61{
62 if (m_lineColor == color) {
63 return;
64 }
65
66 m_lineColor = color;
67}
68
69void LineChartNode::setFillColor(const QColor &color)
70{
71 if (m_fillColor == color) {
72 return;
73 }
74
75 m_fillColor = color;
76}
77
78void LineChartNode::setValues(const QList<QVector2D> &values)
79{
80 m_values = values;
81}
82
83void LineChartNode::updatePoints()
84{
85 if (m_values.isEmpty()) {
86 return;
87 }
88
89 auto segmentCount = qCeil(v: qreal(m_values.count()) / MaxPointsInSegment);
90
91 auto currentX = m_rect.left();
92 auto pointStart = 0;
93 auto pointsPerSegment = MaxPointsInSegment;
94
95 for (int i = 0; i < segmentCount; ++i) {
96 if (i >= childCount()) {
97 appendChildNode(node: new LineSegmentNode{});
98 }
99
100 auto segment = static_cast<LineSegmentNode *>(childAtIndex(i));
101
102 auto segmentPoints = m_values.mid(pos: pointStart, len: pointsPerSegment);
103 pointStart += pointsPerSegment;
104
105 auto segmentWidth = segmentPoints.last().x() - currentX;
106 auto rect = QRectF(currentX, m_rect.top(), segmentWidth, m_rect.height());
107
108 segment->setRect(rect);
109 segment->setAspect(xAspect: segmentWidth / m_rect.width(), yAspect: m_aspect);
110 segment->setSmoothing(m_smoothing);
111 segment->setLineWidth(calculateNormalizedLineWidth(pixelWidth: m_lineWidth, rect: m_rect));
112 segment->setLineColor(m_lineColor);
113 segment->setFillColor(m_fillColor);
114 segment->setValues(segmentPoints);
115 segment->setFarLeft(m_values.at(i: std::max(a: 0, b: pointStart - pointsPerSegment - 1)));
116 segment->setFarRight(m_values.at(i: std::min<int>(a: m_values.count() - 1, b: pointStart + 1)));
117 segment->update();
118
119 currentX += segmentWidth;
120 }
121
122 while (childCount() > segmentCount) {
123 auto child = childAtIndex(i: childCount() - 1);
124 removeChildNode(node: child);
125 delete child;
126 }
127}
128

source code of kquickcharts/src/scenegraph/LineChartNode.cpp