File indexing completed on 2024-10-06 03:40:53

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #include "XYChart.h"
0009 
0010 #include "RangeGroup.h"
0011 #include "datasource/ChartDataSource.h"
0012 
0013 bool operator==(const ComputedRange &first, const ComputedRange &second)
0014 {
0015     return first.startX == second.startX && first.endX == second.endX && qFuzzyCompare(first.startY, second.startY) && qFuzzyCompare(first.endY, second.endY);
0016 }
0017 
0018 XYChart::XYChart(QQuickItem *parent)
0019     : Chart(parent)
0020 {
0021     m_xRange = new RangeGroup{this};
0022     connect(m_xRange, &RangeGroup::rangeChanged, this, &XYChart::updateComputedRange);
0023     m_yRange = new RangeGroup{this};
0024     connect(m_yRange, &RangeGroup::rangeChanged, this, &XYChart::updateComputedRange);
0025 }
0026 
0027 RangeGroup *XYChart::xRange() const
0028 {
0029     return m_xRange;
0030 }
0031 
0032 RangeGroup *XYChart::yRange() const
0033 {
0034     return m_yRange;
0035 }
0036 
0037 XYChart::Direction XYChart::direction() const
0038 {
0039     return m_direction;
0040 }
0041 
0042 void XYChart::setDirection(XYChart::Direction newDirection)
0043 {
0044     if (newDirection == m_direction) {
0045         return;
0046     }
0047 
0048     m_direction = newDirection;
0049     onDataChanged();
0050     Q_EMIT directionChanged();
0051 }
0052 
0053 bool XYChart::stacked() const
0054 {
0055     return m_stacked;
0056 }
0057 
0058 void XYChart::setStacked(bool newStacked)
0059 {
0060     if (newStacked == m_stacked) {
0061         return;
0062     }
0063 
0064     m_stacked = newStacked;
0065     onDataChanged();
0066     Q_EMIT stackedChanged();
0067 }
0068 
0069 ComputedRange XYChart::computedRange() const
0070 {
0071     return m_computedRange;
0072 }
0073 
0074 void XYChart::updateComputedRange()
0075 {
0076     if (valueSources().count() == 0) {
0077         return;
0078     }
0079 
0080     ComputedRange result;
0081 
0082     auto xRange = m_xRange->calculateRange(
0083         valueSources(),
0084         [](ChartDataSource *) {
0085             return 0;
0086         },
0087         [](ChartDataSource *source) {
0088             return source->itemCount();
0089         });
0090     result.startX = xRange.start;
0091     result.endX = xRange.end;
0092     result.distanceX = xRange.distance;
0093 
0094     auto maximumY = [this, xRange](ChartDataSource *source) {
0095         if (!m_stacked) {
0096             return source->maximum().toDouble();
0097         } else {
0098             qreal max = std::numeric_limits<qreal>::min();
0099             for (int i = xRange.start; i < xRange.end; ++i) {
0100                 qreal yDistance = 0.0;
0101                 for (auto source : valueSources()) {
0102                     yDistance += source->item(i).toDouble();
0103                 }
0104                 max = std::max(max, yDistance);
0105             }
0106             return max;
0107         }
0108     };
0109 
0110     auto yRange = m_yRange->calculateRange(
0111         valueSources(),
0112         [](ChartDataSource *source) {
0113             return std::min(0.0, source->minimum().toDouble());
0114         },
0115         maximumY);
0116     result.startY = yRange.start;
0117     result.endY = yRange.end;
0118     result.distanceY = yRange.distance;
0119 
0120     setComputedRange(result);
0121 }
0122 
0123 void XYChart::setComputedRange(ComputedRange range)
0124 {
0125     if (range == m_computedRange) {
0126         return;
0127     }
0128 
0129     m_computedRange = range;
0130     Q_EMIT computedRangeChanged();
0131 }
0132 
0133 QDebug operator<<(QDebug debug, const ComputedRange &range)
0134 {
0135     debug << "Range: startX" << range.startX << "endX" << range.endX << "distance" << range.distanceX << "startY" << range.startY << "endY" << range.endY
0136           << "distance" << range.distanceY;
0137     return debug;
0138 }
0139 
0140 #include "moc_XYChart.cpp"