summaryrefslogtreecommitdiffstats
path: root/stats/boxseries.h
blob: 175ad771e5192c87b62f48b40d8ad08012276c5a (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
// SPDX-License-Identifier: GPL-2.0
// A small custom box plot series, which displays quartiles.
// The QtCharts bar series were  too inflexible with respect
// to placement of the bars.
#ifndef BOX_SERIES_H
#define BOX_SERIES_H

#include "statsseries.h"
#include "statsvariables.h" // for StatsQuartiles

#include <memory>
#include <vector>
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>

class InformationBox;
class QGraphicsScene;

class BoxSeries : public StatsSeries {
public:
	BoxSeries(QGraphicsScene *scene, StatsAxis *xAxis, StatsAxis *yAxis,
		  const QString &variable, const QString &unit, int decimals);
	~BoxSeries();

	void updatePositions() override;
	bool hover(QPointF pos) override;
	void unhighlight() override;

	// Note: this expects that all items are added with increasing pos
	// and that no bar is inside another bar, i.e. lowerBound and upperBound
	// are ordered identically.
	void append(double lowerBound, double upperBound, const StatsQuartiles &q, const QString &binName);

private:
	// Get item under mouse pointer, or -1 if none
	int getItemUnderMouse(const QPointF &f);

	struct Item {
		QGraphicsRectItem box;
		QGraphicsLineItem topWhisker, bottomWhisker;
		QGraphicsLineItem topBar, bottomBar;
		QGraphicsLineItem center;
		QRectF bounding; // bounding box in screen coordinates
		~Item();
		double lowerBound, upperBound;
		StatsQuartiles q;
		QString binName;
		Item(QGraphicsScene *scene, BoxSeries *series, double lowerBound, double upperBound, const StatsQuartiles &q, const QString &binName);
		void updatePosition(BoxSeries *series);
		void highlight(bool highlight);
	};

	QString variable, unit;
	int decimals;

	std::vector<QString> formatInformation(const Item &item) const;
	std::unique_ptr<InformationBox> information;
	std::vector<std::unique_ptr<Item>> items;
	int highlighted; // -1: no item highlighted
};

#endif