From 3a65b0f37f80b5dd93b9b7ccc8ac5c0653d95e4b Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 1 Jan 2021 17:55:44 +0100 Subject: statistics: implement a legend box For some chart (e.g. pie charts or stacked bar charts), we want to display a legend. QtCharts' legend interface happens to be private and therefore is of no use. This introduces a legend box which is implemented using QGraphicItems, which can be placed on top of QCharts. It's very unfancy, but works for now. If there are too many items, not all are shown. Currently, the legend is configured to fill at most half of the width and half of the height of the chart. This might need some optimization. Signed-off-by: Berthold Stoeger --- stats/legend.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 stats/legend.h (limited to 'stats/legend.h') diff --git a/stats/legend.h b/stats/legend.h new file mode 100644 index 000000000..e6c5af1e0 --- /dev/null +++ b/stats/legend.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +// A legend box, which is shown on the chart. +#ifndef STATS_LEGEND_H +#define STATS_LEGEND_H + +#include +#include +#include + +class QGraphicsSceneMouseEvent; + +class Legend : public QGraphicsRectItem { +public: + Legend(QGraphicsWidget *chart, const std::vector &names); + void hover(QPointF pos); + void resize(); // called when the chart size changes. +private: + // Each entry is a text besides a rectangle showing the color + struct Entry { + std::unique_ptr rect; + std::unique_ptr text; + QPointF pos; + double width; + Entry(const QString &name, int idx, int numBins, QGraphicsItem *parent); + }; + QGraphicsWidget *chart; + int displayedItems; + double width; + double height; + int fontHeight; + std::vector entries; + void updatePosition(); + void hide(); +}; + +#endif -- cgit v1.2.3-70-g09d2