summaryrefslogtreecommitdiffstats
path: root/stats/statsaxis.h
blob: 5c77189cc9315234c07cdcc282fb8e181dfa3d89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: GPL-2.0
// Supported chart axes
#ifndef STATS_AXIS_H
#define STATS_AXIS_H

#include <vector>
#include <QBarCategoryAxis>
#include <QCategoryAxis>
#include <QValueAxis>

namespace QtCharts {
	class QChart;
}

class StatsAxis {
public:
	virtual ~StatsAxis();
	virtual void updateLabels(const QtCharts::QChart *chart) = 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;
	bool horizontal;
};

// Small template that derives from a QChart-axis and defines
// the corresponding virtual axis() accessor.
template<typename QAxis>
class StatsAxisTemplate : public StatsAxis, public QAxis
{
	using StatsAxis::StatsAxis;
	QtCharts::QAbstractAxis *qaxis() override final {
		return this;
	}
};

class ValueAxis : public StatsAxisTemplate<QtCharts::QValueAxis> {
public:
	ValueAxis(double min, double max, int decimals, bool horizontal);
private:
	double min, max;
	int decimals;
	void updateLabels(const QtCharts::QChart *chart) override;
	std::pair<double, double> minMax() const override;
};

class CountAxis : public ValueAxis {
public:
	CountAxis(int count, bool horizontal);
private:
	int count;
	void updateLabels(const QtCharts::QChart *chart) override;
};

class CategoryAxis : public StatsAxisTemplate<QtCharts::QBarCategoryAxis> {
public:
	CategoryAxis(const std::vector<QString> &labels, bool horizontal);
private:
	void updateLabels(const QtCharts::QChart *chart);
};

struct HistogramAxisEntry {
	QString name;
	double value;
	bool recommended;
};

class HistogramAxis : public StatsAxisTemplate<QtCharts::QCategoryAxis> {
public:
	HistogramAxis(std::vector<HistogramAxisEntry> bin_values, bool horizontal);
private:
	void updateLabels(const QtCharts::QChart *chart) override;
	std::pair<double, double> minMax() const override;
	std::vector<HistogramAxisEntry> bin_values;
	int preferred_step;
};

class DateAxis : public HistogramAxis {
public:
	DateAxis(double from, double to, bool horizontal);
};

#endif