mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
8dfa3f6db3
commit
ab324ed769
9 changed files with 81 additions and 0 deletions
|
@ -23,6 +23,8 @@ set(SUBSURFACE_STATS_SRCS
|
|||
statsaxis.cpp
|
||||
statscolors.h
|
||||
statscolors.cpp
|
||||
statsgrid.h
|
||||
statsgrid.cpp
|
||||
statsseries.h
|
||||
statsseries.cpp
|
||||
statsstate.h
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
36
stats/statsgrid.cpp
Normal file
36
stats/statsgrid.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
22
stats/statsgrid.h
Normal file
22
stats/statsgrid.h
Normal file
|
@ -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;
|
||||
};
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue