KQuickCharts

ChartAxisSource.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 "ChartAxisSource.h"
9
10#include <QDebug>
11#include <QVariant>
12
13#include "XYChart.h"
14
16 : ChartDataSource(parent)
17{
18 connect(this, &ChartAxisSource::itemCountChanged, this, &ChartAxisSource::dataChanged);
19 connect(this, &ChartAxisSource::chartChanged, this, &ChartAxisSource::dataChanged);
20 connect(this, &ChartAxisSource::axisChanged, this, &ChartAxisSource::dataChanged);
21}
22
23QVariant ChartAxisSource::item(int index) const
24{
25 if (!m_chart || index < 0 || index > m_itemCount) {
26 return {};
27 }
28
29 auto range = m_chart->computedRange();
30 if (m_axis == Axis::XAxis) {
31 return range.startX + (range.distanceX / (m_itemCount - 1)) * index;
32 } else {
33 return range.startY + (range.distanceY / (m_itemCount - 1)) * index;
34 }
35}
36
37QVariant ChartAxisSource::minimum() const
38{
39 if (!m_chart) {
40 return {};
41 }
42
43 if (m_axis == Axis::XAxis) {
44 return m_chart->computedRange().startX;
45 } else {
46 return m_chart->computedRange().startY;
47 }
48}
49
50QVariant ChartAxisSource::maximum() const
51{
52 if (!m_chart) {
53 return {};
54 }
55
56 if (m_axis == Axis::XAxis) {
57 return m_chart->computedRange().endX;
58 } else {
59 return m_chart->computedRange().endY;
60 }
61}
62
63XYChart *ChartAxisSource::chart() const
64{
65 return m_chart;
66}
67
68void ChartAxisSource::setChart(XYChart *newChart)
69{
70 if (newChart == m_chart) {
71 return;
72 }
73
74 if (m_chart) {
75 disconnect(m_chart, &XYChart::computedRangeChanged, this, &ChartAxisSource::dataChanged);
76 }
77
78 m_chart = newChart;
79 if (m_chart) {
80 connect(m_chart, &XYChart::computedRangeChanged, this, &ChartAxisSource::dataChanged);
81 }
82 Q_EMIT chartChanged();
83}
84
85ChartAxisSource::Axis ChartAxisSource::axis() const
86{
87 return m_axis;
88}
89
90void ChartAxisSource::setAxis(ChartAxisSource::Axis newAxis)
91{
92 if (newAxis == m_axis) {
93 return;
94 }
95
96 m_axis = newAxis;
97 Q_EMIT axisChanged();
98}
99
100int ChartAxisSource::itemCount() const
101{
102 return m_itemCount;
103}
104
105void ChartAxisSource::setItemCount(int newItemCount)
106{
107 if (newItemCount == m_itemCount) {
108 return;
109 }
110
111 m_itemCount = newItemCount;
112 Q_EMIT itemCountChanged();
113}
114
115#include "moc_ChartAxisSource.cpp"
ChartAxisSource(QObject *parent=nullptr)
Constructor.
Abstract base class for data sources.
A base class for Charts that are based on an X/Y grid.
Definition XYChart.h:33
Q_SIGNAL void computedRangeChanged()
Emitted whenever the complete range is recalculated.
ComputedRange computedRange() const
Get the complete, calculated range for this chart.
Definition XYChart.cpp:69
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
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.