subsurface/stats/statsaxis.h
Berthold Stoeger 4ab9f1c6b0 statistics: replace QtCharts' axes
Replace by custom implementation, with the ultimate goal to
remove the QtCharts module. This doesn't yet display axis
titles or a grid.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2021-01-06 12:31:22 -08:00

108 lines
2.8 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef STATS_AXIS_H
#define STATS_AXIS_H
#include <memory>
#include <vector>
#include <QBarCategoryAxis>
#include <QCategoryAxis>
#include <QFont>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsLineItem>
#include <QValueAxis>
namespace QtCharts {
class QChart;
}
class StatsAxis : QGraphicsLineItem {
public:
virtual ~StatsAxis();
// Returns minimum and maximum of shown range, not of data points.
std::pair<double, double> minMax() const;
double width() const; // Only supported by vertical axes. Only valid after setSize().
double height() const; // Only supported for horizontal axes. Always valid.
void setSize(double size); // Width for horizontal and height for vertical.
void setPos(QPointF pos); // Must be called after setSize().
void setRange(double, double);
// Map x (horizontal) or y (vertical) coordinate to or from screen coordinate
double toScreen(double) const;
double toValue(double) const;
protected:
StatsAxis(QtCharts::QChart *chart, bool horizontal, bool labelsBetweenTicks);
QtCharts::QChart *chart;
struct Label {
std::unique_ptr<QGraphicsSimpleTextItem> label;
double pos;
Label(const QString &name, double pos, QtCharts::QChart *chart, const QFont &font);
};
std::vector<Label> labels;
void addLabel(const QString &label, double pos);
virtual void updateLabels() = 0;
struct Tick {
std::unique_ptr<QGraphicsLineItem> item;
double pos;
Tick(double pos, QtCharts::QChart *chart);
};
std::vector<Tick> ticks;
void addTick(double pos);
int guessNumTicks(const std::vector<QString> &strings) const;
bool horizontal;
bool labelsBetweenTicks; // When labels are between ticks, they can be moved closer to the axis
QFont labelFont, titleFont;
double size; // width for horizontal, height for vertical
double zeroOnScreen;
double min, max;
};
class ValueAxis : public StatsAxis {
public:
ValueAxis(QtCharts::QChart *chart, double min, double max, int decimals, bool horizontal);
private:
double min, max;
int decimals;
void updateLabels() override;
};
class CountAxis : public ValueAxis {
public:
CountAxis(QtCharts::QChart *chart, int count, bool horizontal);
private:
int count;
void updateLabels() override;
};
class CategoryAxis : public StatsAxis {
public:
CategoryAxis(QtCharts::QChart *chart, const std::vector<QString> &labels, bool horizontal);
private:
void updateLabels();
};
struct HistogramAxisEntry {
QString name;
double value;
bool recommended;
};
class HistogramAxis : public StatsAxis {
public:
HistogramAxis(QtCharts::QChart *chart, std::vector<HistogramAxisEntry> bin_values, bool horizontal);
private:
void updateLabels() override;
std::vector<HistogramAxisEntry> bin_values;
int preferred_step;
};
class DateAxis : public HistogramAxis {
public:
DateAxis(QtCharts::QChart *chart, double from, double to, bool horizontal);
};
#endif