KQuickCharts

XYChart.cpp
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 "XYChart.h"
9
10#include "RangeGroup.h"
11#include "datasource/ChartDataSource.h"
12
13bool operator==(const ComputedRange &first, const ComputedRange &second)
14{
15 return first.startX == second.startX && first.endX == second.endX && qFuzzyCompare(first.startY, second.startY) && qFuzzyCompare(first.endY, second.endY);
16}
17
19 : Chart(parent)
20{
21 m_xRange = new RangeGroup{this};
22 connect(m_xRange, &RangeGroup::rangeChanged, this, &XYChart::updateComputedRange);
23 m_yRange = new RangeGroup{this};
24 connect(m_yRange, &RangeGroup::rangeChanged, this, &XYChart::updateComputedRange);
25}
26
28{
29 return m_xRange;
30}
31
33{
34 return m_yRange;
35}
36
38{
39 return m_direction;
40}
41
42void XYChart::setDirection(XYChart::Direction newDirection)
43{
44 if (newDirection == m_direction) {
45 return;
46 }
47
48 m_direction = newDirection;
50 Q_EMIT directionChanged();
51}
52
53bool XYChart::stacked() const
54{
55 return m_stacked;
56}
57
58void XYChart::setStacked(bool newStacked)
59{
60 if (newStacked == m_stacked) {
61 return;
62 }
63
64 m_stacked = newStacked;
66 Q_EMIT stackedChanged();
67}
68
70{
71 return m_computedRange;
72}
73
75{
76 if (valueSources().count() == 0) {
77 return;
78 }
79
80 ComputedRange result;
81
82 auto xRange = m_xRange->calculateRange(
84 [](ChartDataSource *) {
85 return 0;
86 },
87 [](ChartDataSource *source) {
88 return source->itemCount();
89 });
90 result.startX = xRange.start;
91 result.endX = xRange.end;
92 result.distanceX = xRange.distance;
93
94 auto maximumY = [this, xRange](ChartDataSource *source) {
95 if (!m_stacked) {
96 return source->maximum().toDouble();
97 } else {
98 qreal max = std::numeric_limits<qreal>::min();
99 for (int i = xRange.start; i < xRange.end; ++i) {
100 qreal yDistance = 0.0;
101 for (auto source : valueSources()) {
102 yDistance += source->item(i).toDouble();
103 }
104 max = std::max(max, yDistance);
105 }
106 return max;
107 }
108 };
109
110 auto yRange = m_yRange->calculateRange(
111 valueSources(),
112 [](ChartDataSource *source) {
113 return std::min(0.0, source->minimum().toDouble());
114 },
115 maximumY);
116 result.startY = yRange.start;
117 result.endY = yRange.end;
118 result.distanceY = yRange.distance;
119
120 setComputedRange(result);
121}
122
124{
125 if (range == m_computedRange) {
126 return;
127 }
128
129 m_computedRange = range;
131}
132
133QDebug operator<<(QDebug debug, const ComputedRange &range)
134{
135 debug << "Range: startX" << range.startX << "endX" << range.endX << "distance" << range.distanceX << "startY" << range.startY << "endY" << range.endY
136 << "distance" << range.distanceY;
137 return debug;
138}
139
140#include "moc_XYChart.cpp"
Abstract base class for data sources.
Abstract base class for all charts.
Definition Chart.h:22
QQmlListProperty< ChartDataSource > valueSources
The data sources providing the data this chart needs to render.
Definition Chart.h:70
virtual void onDataChanged()=0
Called when the data of a value source changes.
An object that can be used as a grouped property to provide a value range for charts.
Definition RangeGroup.h:25
qreal distance
The distance between from and to.
Definition RangeGroup.h:70
XYChart(QQuickItem *parent=nullptr)
Constructor.
Definition XYChart.cpp:18
virtual void updateComputedRange()
Re-calculate the chart's range.
Definition XYChart.cpp:74
RangeGroup * yRange
The range of values on the Y axis.
Definition XYChart.h:72
Direction direction
Which direction this chart's X axis runs.
Definition XYChart.h:77
Q_SIGNAL void computedRangeChanged()
Emitted whenever the complete range is recalculated.
bool stacked
Whether the values of each value source should be stacked.
Definition XYChart.h:88
void setComputedRange(ComputedRange range)
Set the computed range.
Definition XYChart.cpp:123
RangeGroup * xRange
The range of values on the X axis.
Definition XYChart.h:67
ComputedRange computedRange() const
Get the complete, calculated range for this chart.
Definition XYChart.cpp:69
Direction
The direction of values on the X axis.
Definition XYChart.h:46
bool operator==(const StyleDelim &l, const StyleDelim &r)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
double toDouble(bool *ok) const const
A helper containing the calculated X and Y ranges of a chart.
Definition XYChart.h:18
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.