summaryrefslogtreecommitdiffstats
path: root/stats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2021-01-05 13:51:39 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2021-01-06 12:31:22 -0800
commitab324ed769b4d39816020bf649defb61cb4bff41 (patch)
tree066dfab03a5d3a370b7928d53eb0c18d96cda62e /stats
parent8dfa3f6db3eda8bce432afb7407efe2d8822ebbd (diff)
downloadsubsurface-ab324ed769b4d39816020bf649defb61cb4bff41.tar.gz
statistics: paint custom grid
With removal of QtCharts' axes, the grid was lost. Readd it. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats')
-rw-r--r--stats/CMakeLists.txt2
-rw-r--r--stats/statsaxis.cpp9
-rw-r--r--stats/statsaxis.h2
-rw-r--r--stats/statscolors.h1
-rw-r--r--stats/statsgrid.cpp36
-rw-r--r--stats/statsgrid.h22
-rw-r--r--stats/statsview.cpp6
-rw-r--r--stats/statsview.h2
-rw-r--r--stats/zvalues.h1
9 files changed, 81 insertions, 0 deletions
diff --git a/stats/CMakeLists.txt b/stats/CMakeLists.txt
index e0e2dc303..a084dd0b5 100644
--- a/stats/CMakeLists.txt
+++ b/stats/CMakeLists.txt
@@ -23,6 +23,8 @@ set(SUBSURFACE_STATS_SRCS
statsaxis.cpp
statscolors.h
statscolors.cpp
+ statsgrid.h
+ statsgrid.cpp
statsseries.h
statsseries.cpp
statsstate.h
diff --git a/stats/statsaxis.cpp b/stats/statsaxis.cpp
index 23923e7ea..3bdb8e427 100644
--- a/stats/statsaxis.cpp
+++ b/stats/statsaxis.cpp
@@ -135,6 +135,15 @@ void StatsAxis::addTick(double pos)
ticks.emplace_back(pos, chart);
}
+std::vector<double> StatsAxis::ticksPositions() const
+{
+ std::vector<double> res;
+ res.reserve(ticks.size());
+ for (const Tick &tick: ticks)
+ res.push_back(toScreen(tick.pos));
+ return res;
+}
+
// Map x (horizontal) or y (vertical) coordinate to or from screen coordinate
double StatsAxis::toScreen(double pos) const
{
diff --git a/stats/statsaxis.h b/stats/statsaxis.h
index c25ffe692..150deb087 100644
--- a/stats/statsaxis.h
+++ b/stats/statsaxis.h
@@ -30,6 +30,8 @@ public:
// Map x (horizontal) or y (vertical) coordinate to or from screen coordinate
double toScreen(double) const;
double toValue(double) const;
+
+ std::vector<double> ticksPositions() const; // Positions in screen coordinates
protected:
StatsAxis(QtCharts::QChart *chart, const QString &title, bool horizontal, bool labelsBetweenTicks);
QtCharts::QChart *chart;
diff --git a/stats/statscolors.h b/stats/statscolors.h
index 926e2e489..e9775e2a6 100644
--- a/stats/statscolors.h
+++ b/stats/statscolors.h
@@ -12,6 +12,7 @@ inline const QColor highlightedBorderColor(0xaa, 0xaa, 0x22);
inline const QColor darkLabelColor(Qt::black);
inline const QColor lightLabelColor(Qt::white);
inline const QColor axisColor(Qt::black);
+inline const QColor gridColor(0xcc, 0xcc, 0xcc);
QColor binColor(int bin, int numBins);
QColor labelColor(int bin, size_t numBins);
diff --git a/stats/statsgrid.cpp b/stats/statsgrid.cpp
new file mode 100644
index 000000000..fe99b1762
--- /dev/null
+++ b/stats/statsgrid.cpp
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "statsgrid.h"
+#include "statsaxis.h"
+#include "statscolors.h"
+#include "zvalues.h"
+
+#include <QChart>
+#include <QGraphicsLineItem>
+
+static const double gridWidth = 1.0;
+static const Qt::PenStyle gridStyle = Qt::SolidLine;
+
+StatsGrid::StatsGrid(QtCharts::QChart *chart, const StatsAxis &xAxis, const StatsAxis &yAxis)
+ : chart(chart), xAxis(xAxis), yAxis(yAxis)
+{
+}
+
+void StatsGrid::updatePositions()
+{
+ std::vector<double> xtics = xAxis.ticksPositions();
+ std::vector<double> ytics = yAxis.ticksPositions();
+ lines.clear();
+ if (xtics.empty() || ytics.empty())
+ return;
+
+ for (double x: xtics) {
+ lines.emplace_back(new QGraphicsLineItem(x, ytics.front(), x, ytics.back(), chart));
+ lines.back()->setPen(QPen(gridColor, gridWidth, gridStyle));
+ lines.back()->setZValue(ZValues::grid);
+ }
+ for (double y: ytics) {
+ lines.emplace_back(new QGraphicsLineItem(xtics.front(), y, xtics.back(), y, chart));
+ lines.back()->setPen(QPen(gridColor, gridWidth, gridStyle));
+ lines.back()->setZValue(ZValues::grid);
+ }
+}
diff --git a/stats/statsgrid.h b/stats/statsgrid.h
new file mode 100644
index 000000000..0fdb2a188
--- /dev/null
+++ b/stats/statsgrid.h
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+// The background grid of a chart
+
+#include <memory>
+#include <vector>
+#include <QVector>
+#include <QGraphicsLineItem>
+
+class StatsAxis;
+namespace QtCharts {
+ class QChart;
+};
+
+class StatsGrid {
+public:
+ StatsGrid(QtCharts::QChart *chart, const StatsAxis &xAxis, const StatsAxis &yAxis);
+ void updatePositions();
+private:
+ QtCharts::QChart *chart;
+ const StatsAxis &xAxis, &yAxis;
+ std::vector<std::unique_ptr<QGraphicsLineItem>> lines;
+};
diff --git a/stats/statsview.cpp b/stats/statsview.cpp
index 394620d13..d6da093b3 100644
--- a/stats/statsview.cpp
+++ b/stats/statsview.cpp
@@ -6,6 +6,7 @@
#include "pieseries.h"
#include "scatterseries.h"
#include "statsaxis.h"
+#include "statsgrid.h"
#include "statsstate.h"
#include "statstranslations.h"
#include "statsvariables.h"
@@ -118,6 +119,8 @@ void StatsView::plotAreaChanged(const QRectF &r)
xAxis->setPos(QPointF(left, bottom));
}
+ if (grid)
+ grid->updatePositions();
for (auto &series: series)
series->updatePositions();
for (QuartileMarker &marker: quartileMarkers)
@@ -195,6 +198,8 @@ void StatsView::setAxes(StatsAxis *x, StatsAxis *y)
{
xAxis = x;
yAxis = y;
+ if (x && y)
+ grid = std::make_unique<StatsGrid>(chart, *x, *y);
}
void StatsView::reset()
@@ -208,6 +213,7 @@ void StatsView::reset()
quartileMarkers.clear();
lineMarkers.clear();
chart->removeAllSeries();
+ grid.reset();
axes.clear();
title.reset();
}
diff --git a/stats/statsview.h b/stats/statsview.h
index 5df05c15b..92dbedf03 100644
--- a/stats/statsview.h
+++ b/stats/statsview.h
@@ -24,6 +24,7 @@ class CategoryAxis;
class CountAxis;
class HistogramAxis;
class StatsAxis;
+class StatsGrid;
class Legend;
enum class ChartSubType : int;
@@ -121,6 +122,7 @@ private:
QtCharts::QChart *chart;
QFont titleFont;
std::vector<std::unique_ptr<StatsAxis>> axes;
+ std::unique_ptr<StatsGrid> grid;
std::vector<std::unique_ptr<StatsSeries>> series;
std::unique_ptr<Legend> legend;
std::vector<QuartileMarker> quartileMarkers;
diff --git a/stats/zvalues.h b/stats/zvalues.h
index 1b6dc5d22..118c488c0 100644
--- a/stats/zvalues.h
+++ b/stats/zvalues.h
@@ -6,6 +6,7 @@
#ifndef ZVALUES_H
struct ZValues {
+ static constexpr double grid = -1.0;
static constexpr double series = 0.0;
static constexpr double axes = 1.0;
static constexpr double seriesLabels = 2.0;