diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-04 17:16:13 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2021-01-06 12:31:22 -0800 |
commit | 23d781debab71d7cdac2fd9213938c33376a8bf1 (patch) | |
tree | 808868c1b9100236fa34717c0e72d24f23ecbeb2 /stats | |
parent | 76136010bf72e6452701dd7dffc24c9eabfaf97d (diff) | |
download | subsurface-23d781debab71d7cdac2fd9213938c33376a8bf1.tar.gz |
statistics: save chart in axis class
The chart was passed as argument to the function recalculating
the axis labels. Instead, pass the chart in the constructor of
the axes and save it. This gains us flexibility for the future:
There will be more functions that need to access the chart (e.g.
resizing of the axes).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats')
-rw-r--r-- | stats/statsaxis.cpp | 34 | ||||
-rw-r--r-- | stats/statsaxis.h | 25 | ||||
-rw-r--r-- | stats/statsview.cpp | 6 |
3 files changed, 35 insertions, 30 deletions
diff --git a/stats/statsaxis.cpp b/stats/statsaxis.cpp index 46351a534..81dd18c10 100644 --- a/stats/statsaxis.cpp +++ b/stats/statsaxis.cpp @@ -10,7 +10,7 @@ #include <QFontMetrics> #include <QLocale> -StatsAxis::StatsAxis(bool horizontal) : horizontal(horizontal) +StatsAxis::StatsAxis(QtCharts::QChart *chart, bool horizontal) : chart(chart), horizontal(horizontal) { } @@ -28,7 +28,7 @@ std::pair<double, double> StatsAxis::minMax() const // maximum-size strings especially, when using proportional fonts or for // categorical data. Therefore, try to err on the safe side by adding enough // margins. -int StatsAxis::guessNumTicks(const QtCharts::QChart *chart, const QtCharts::QAbstractAxis *axis, const std::vector<QString> &strings) const +int StatsAxis::guessNumTicks(const QtCharts::QAbstractAxis *axis, const std::vector<QString> &strings) const { QFont font = axis->labelsFont(); QFontMetrics fm(font); @@ -51,7 +51,8 @@ int StatsAxis::guessNumTicks(const QtCharts::QChart *chart, const QtCharts::QAbs return std::max(numTicks, 2); } -ValueAxis::ValueAxis(double min, double max, int decimals, bool horizontal) : StatsAxisTemplate(horizontal), +ValueAxis::ValueAxis(QtCharts::QChart *chart, double min, double max, int decimals, bool horizontal) : + StatsAxisTemplate(chart, horizontal), min(min), max(max), decimals(decimals) { } @@ -66,7 +67,7 @@ static QString makeFormatString(int decimals) return QStringLiteral("%.%1f").arg(decimals < 0 ? 0 : decimals); } -void ValueAxis::updateLabels(const QtCharts::QChart *chart) +void ValueAxis::updateLabels() { using QtCharts::QValueAxis; @@ -79,7 +80,7 @@ void ValueAxis::updateLabels(const QtCharts::QChart *chart) QLocale loc; QString minString = loc.toString(min, 'f', decimals); QString maxString = loc.toString(max, 'f', decimals); - int numTicks = guessNumTicks(chart, this, { minString, maxString}); + int numTicks = guessNumTicks(this, { minString, maxString}); // Use full decimal increments double height = max - min; @@ -105,16 +106,17 @@ void ValueAxis::updateLabels(const QtCharts::QChart *chart) setTickCount(num + 1); } -CountAxis::CountAxis(int count, bool horizontal) : ValueAxis(0.0, (double)count, 0, horizontal), +CountAxis::CountAxis(QtCharts::QChart *chart, int count, bool horizontal) : + ValueAxis(chart, 0.0, (double)count, 0, horizontal), count(count) { } -void CountAxis::updateLabels(const QtCharts::QChart *chart) +void CountAxis::updateLabels() { QLocale loc; QString countString = loc.toString(count); - int numTicks = guessNumTicks(chart, this, { countString }); + int numTicks = guessNumTicks(this, { countString }); // Get estimate of step size if (count <= 0) @@ -150,13 +152,14 @@ void CountAxis::updateLabels(const QtCharts::QChart *chart) setTickCount(numTicks); } -CategoryAxis::CategoryAxis(const std::vector<QString> &labels, bool horizontal) : StatsAxisTemplate(horizontal) +CategoryAxis::CategoryAxis(QtCharts::QChart *chart, const std::vector<QString> &labels, bool horizontal) : + StatsAxisTemplate(chart, horizontal) { for (const QString &s: labels) append(s); } -void CategoryAxis::updateLabels(const QtCharts::QChart *) +void CategoryAxis::updateLabels() { } @@ -185,7 +188,8 @@ QString LabelDisambiguator::transmogrify(const QString &s) } } -HistogramAxis::HistogramAxis(std::vector<HistogramAxisEntry> bins, bool horizontal) : StatsAxisTemplate(horizontal), +HistogramAxis::HistogramAxis(QtCharts::QChart *chart, std::vector<HistogramAxisEntry> bins, bool horizontal) : + StatsAxisTemplate(chart, horizontal), bin_values(std::move(bins)) { if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category @@ -223,7 +227,7 @@ std::pair<double, double> HistogramAxis::minMax() const // If labels are skipped, try to skip it in such a way that a recommended label is shown. // The one example where this is relevant is the quarterly bins, which are formated as (2019, q1, q2, q3, 2020, ...). // There, we obviously want to show the years and not the quarters. -void HistogramAxis::updateLabels(const QtCharts::QChart *chart) +void HistogramAxis::updateLabels() { if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category return; @@ -238,7 +242,7 @@ void HistogramAxis::updateLabels(const QtCharts::QChart *chart) strings.reserve(bin_values.size()); for (auto &[name, value, recommended]: bin_values) strings.push_back(name); - int maxLabels = guessNumTicks(chart, this, strings); + int maxLabels = guessNumTicks(this, strings); int step = ((int)bin_values.size() - 1) / maxLabels + 1; if (step < preferred_step) { @@ -388,7 +392,7 @@ static std::vector<HistogramAxisEntry> timeRangeToBins(double from, double to) return res; } -DateAxis::DateAxis(double from, double to, bool horizontal) : - HistogramAxis(timeRangeToBins(from, to), horizontal) +DateAxis::DateAxis(QtCharts::QChart *chart, double from, double to, bool horizontal) : + HistogramAxis(chart, timeRangeToBins(from, to), horizontal) { } diff --git a/stats/statsaxis.h b/stats/statsaxis.h index 5c77189cc..468dee1c2 100644 --- a/stats/statsaxis.h +++ b/stats/statsaxis.h @@ -15,13 +15,14 @@ namespace QtCharts { class StatsAxis { public: virtual ~StatsAxis(); - virtual void updateLabels(const QtCharts::QChart *chart) = 0; + virtual void updateLabels() = 0; virtual QtCharts::QAbstractAxis *qaxis() = 0; // Returns minimum and maximum of shown range, not of data points. virtual std::pair<double, double> minMax() const; protected: - StatsAxis(bool horizontal); - int guessNumTicks(const QtCharts::QChart *chart, const QtCharts::QAbstractAxis *axis, const std::vector<QString> &strings) const; + QtCharts::QChart *chart; + StatsAxis(QtCharts::QChart *chart, bool horizontal); + int guessNumTicks(const QtCharts::QAbstractAxis *axis, const std::vector<QString> &strings) const; bool horizontal; }; @@ -38,27 +39,27 @@ class StatsAxisTemplate : public StatsAxis, public QAxis class ValueAxis : public StatsAxisTemplate<QtCharts::QValueAxis> { public: - ValueAxis(double min, double max, int decimals, bool horizontal); + ValueAxis(QtCharts::QChart *chart, double min, double max, int decimals, bool horizontal); private: double min, max; int decimals; - void updateLabels(const QtCharts::QChart *chart) override; + void updateLabels() override; std::pair<double, double> minMax() const override; }; class CountAxis : public ValueAxis { public: - CountAxis(int count, bool horizontal); + CountAxis(QtCharts::QChart *chart, int count, bool horizontal); private: int count; - void updateLabels(const QtCharts::QChart *chart) override; + void updateLabels() override; }; class CategoryAxis : public StatsAxisTemplate<QtCharts::QBarCategoryAxis> { public: - CategoryAxis(const std::vector<QString> &labels, bool horizontal); + CategoryAxis(QtCharts::QChart *chart, const std::vector<QString> &labels, bool horizontal); private: - void updateLabels(const QtCharts::QChart *chart); + void updateLabels(); }; struct HistogramAxisEntry { @@ -69,9 +70,9 @@ struct HistogramAxisEntry { class HistogramAxis : public StatsAxisTemplate<QtCharts::QCategoryAxis> { public: - HistogramAxis(std::vector<HistogramAxisEntry> bin_values, bool horizontal); + HistogramAxis(QtCharts::QChart *chart, std::vector<HistogramAxisEntry> bin_values, bool horizontal); private: - void updateLabels(const QtCharts::QChart *chart) override; + void updateLabels() override; std::pair<double, double> minMax() const override; std::vector<HistogramAxisEntry> bin_values; int preferred_step; @@ -79,7 +80,7 @@ private: class DateAxis : public HistogramAxis { public: - DateAxis(double from, double to, bool horizontal); + DateAxis(QtCharts::QChart *chart, double from, double to, bool horizontal); }; #endif diff --git a/stats/statsview.cpp b/stats/statsview.cpp index 817f8001d..11dbe4f1e 100644 --- a/stats/statsview.cpp +++ b/stats/statsview.cpp @@ -86,7 +86,7 @@ StatsView::~StatsView() void StatsView::plotAreaChanged(const QRectF &) { for (auto &axis: axes) - axis->updateLabels(chart); + axis->updateLabels(); for (auto &series: series) series->updatePositions(); for (QuartileMarker &marker: quartileMarkers) @@ -142,9 +142,9 @@ void StatsView::setTitle(const QString &s) template <typename T, class... Args> T *StatsView::createAxis(const QString &title, Args&&... args) { - T *res = new T(std::forward<Args>(args)...); + T *res = new T(chart, std::forward<Args>(args)...); axes.emplace_back(res); - axes.back()->updateLabels(chart); + axes.back()->updateLabels(); axes.back()->qaxis()->setTitleText(title); return res; } |