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 "numericaggregator.h" 0008 #include "chartutil.h" 0009 0010 #include <model/numericaggregationmodel.h> 0011 #include <model/timeaggregationmodel.h> 0012 0013 #include <QtCharts/QBarCategoryAxis> 0014 #include <QtCharts/QBoxPlotSeries> 0015 #include <QtCharts/QChart> 0016 #include <QtCharts/QHBoxPlotModelMapper> 0017 #include <QtCharts/QValueAxis> 0018 0019 using namespace KUserFeedback::Console; 0020 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0021 using namespace QtCharts; 0022 #endif 0023 0024 NumericAggregator::NumericAggregator() = default; 0025 NumericAggregator::~NumericAggregator() = default; 0026 0027 Aggregator::ChartModes NumericAggregator::chartModes() const 0028 { 0029 Aggregator::ChartModes modes = None; 0030 if (aggregation().elements().size() == 1) 0031 modes |= Timeline; 0032 return modes; 0033 } 0034 0035 QAbstractItemModel* NumericAggregator::timeAggregationModel() 0036 { 0037 if (!m_model && !aggregation().elements().isEmpty()) { 0038 m_model.reset(new NumericAggregationModel); 0039 m_model->setSourceModel(sourceModel()); 0040 const auto e = aggregation().elements().at(0); 0041 m_model->setAggregation(e); 0042 QObject::connect(m_model.get(), &QAbstractItemModel::modelReset, [this]() { 0043 updateTimelineChart(); 0044 }); 0045 } 0046 return m_model.get(); 0047 } 0048 0049 QChart* NumericAggregator::timelineChart() 0050 { 0051 if (!m_timelineChart) { 0052 m_timelineChart.reset(new QChart); 0053 ChartUtil::applyTheme(m_timelineChart.get()); 0054 auto xAxis = new QBarCategoryAxis(m_timelineChart.get()); 0055 auto yAxis = new QValueAxis(m_timelineChart.get()); 0056 yAxis->setMinorTickCount(4); 0057 m_timelineChart->addAxis(xAxis, Qt::AlignBottom); 0058 m_timelineChart->addAxis(yAxis, Qt::AlignLeft); 0059 updateTimelineChart(); 0060 } 0061 0062 return m_timelineChart.get(); 0063 } 0064 0065 void NumericAggregator::updateTimelineChart() 0066 { 0067 if (!m_timelineChart) 0068 return; 0069 m_timelineChart->removeAllSeries(); 0070 0071 auto series = new QBoxPlotSeries(m_timelineChart.get()); 0072 series->setName(displayName()); 0073 auto mapper = new QHBoxPlotModelMapper(series); 0074 mapper->setModel(timeAggregationModel()); 0075 mapper->setFirstColumn(1); 0076 mapper->setFirstBoxSetRow(0); 0077 mapper->setLastBoxSetRow(timeAggregationModel()->rowCount()); 0078 mapper->setSeries(series); 0079 m_timelineChart->addSeries(series); 0080 0081 series->attachAxis(m_timelineChart->axisX()); 0082 series->attachAxis(m_timelineChart->axisY()); 0083 0084 QStringList l; 0085 for (int i = 0; i < m_model->rowCount(); ++i) { 0086 l.push_back(timeAggregationModel()->index(i, 0).data(TimeAggregationModel::DateTimeRole).toDateTime().toString(QStringLiteral("yyyy-MM-dd"))); 0087 } 0088 0089 qobject_cast<QBarCategoryAxis*>(m_timelineChart->axisX())->setCategories(l); 0090 const auto max = timeAggregationModel()->index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(); 0091 m_timelineChart->axisY()->setRange(0, max); 0092 qobject_cast<QValueAxis*>(m_timelineChart->axisY())->applyNiceNumbers(); 0093 }