File indexing completed on 2024-12-15 03:44:59
0001 /* 0002 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "totalaggregator.h" 0008 #include "chartutil.h" 0009 0010 #include <model/timeaggregationmodel.h> 0011 0012 #include <QtCharts/QChart> 0013 #include <QtCharts/QDateTimeAxis> 0014 #include <QtCharts/QLineSeries> 0015 #include <QtCharts/QValueAxis> 0016 #include <QtCharts/QVXYModelMapper> 0017 0018 #include <QAbstractItemModel> 0019 0020 #include <numeric> 0021 0022 using namespace KUserFeedback::Console; 0023 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0024 using namespace QtCharts; 0025 #endif 0026 0027 TotalAggregator::TotalAggregator() = default; 0028 TotalAggregator::~TotalAggregator() = default; 0029 0030 Aggregator::ChartModes TotalAggregator::chartModes() const 0031 { 0032 return Timeline; 0033 } 0034 0035 QString TotalAggregator::displayName() const 0036 { 0037 return tr("Samples"); 0038 } 0039 0040 QAbstractItemModel* TotalAggregator::timeAggregationModel() 0041 { 0042 return sourceModel(); 0043 } 0044 0045 QChart* TotalAggregator::timelineChart() 0046 { 0047 if (!m_timelineChart) { 0048 m_timelineChart.reset(new QChart); 0049 ChartUtil::applyTheme(m_timelineChart.get()); 0050 auto xAxis = new QDateTimeAxis(m_timelineChart.get()); 0051 xAxis->setFormat(QStringLiteral("yyyy-MM-dd")); // TODO, follow aggregation mode 0052 auto yAxis = new QValueAxis(m_timelineChart.get()); 0053 yAxis->setMinorTickCount(4); 0054 m_timelineChart->addAxis(xAxis, Qt::AlignBottom); 0055 m_timelineChart->addAxis(yAxis, Qt::AlignLeft); 0056 updateTimelineChart(); 0057 0058 QObject::connect(sourceModel(), &QAbstractItemModel::modelReset, m_timelineChart.get(), [this]() { 0059 updateTimelineChart(); 0060 }); 0061 } 0062 0063 return m_timelineChart.get(); 0064 } 0065 0066 void TotalAggregator::updateTimelineChart() 0067 { 0068 if (!m_timelineChart) 0069 return; 0070 m_timelineChart->removeAllSeries(); 0071 if (timeAggregationModel()->rowCount() <= 0) 0072 return; 0073 0074 auto series = new QLineSeries(m_timelineChart.get()); 0075 series->setName(displayName()); 0076 auto mapper = new QVXYModelMapper(series); 0077 mapper->setModel(timeAggregationModel()); 0078 mapper->setXColumn(0); 0079 mapper->setYColumn(1); 0080 mapper->setFirstRow(0); 0081 mapper->setSeries(series); 0082 m_timelineChart->addSeries(series); 0083 0084 series->attachAxis(m_timelineChart->axisX()); 0085 series->attachAxis(m_timelineChart->axisY()); 0086 0087 const auto beginDt = timeAggregationModel()->index(0, 0).data(TimeAggregationModel::DateTimeRole).toDateTime(); 0088 const auto endDt = timeAggregationModel()->index(timeAggregationModel()->rowCount() - 1, 0).data(TimeAggregationModel::DateTimeRole).toDateTime(); 0089 m_timelineChart->axisX()->setRange(beginDt, endDt); 0090 qobject_cast<QDateTimeAxis*>(m_timelineChart->axisX())->setTickCount(std::min(timeAggregationModel()->rowCount(), 12)); 0091 const auto max = timeAggregationModel()->index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(); 0092 m_timelineChart->axisY()->setRange(0, max); 0093 qobject_cast<QValueAxis*>(m_timelineChart->axisY())->applyNiceNumbers(); 0094 }