statistics: render title

Since we want to get rid of QtCharts, we have to render our own
title. Simply keep around a QGraphicsSimpleTextItem and put in
the center of the chart. Define the borders to the scene as
constants.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Berthold Stoeger 2021-01-04 21:41:30 +01:00 committed by Dirk Hohndel
parent ccc95f938a
commit 90129aa26f
2 changed files with 36 additions and 2 deletions

View file

@ -18,11 +18,14 @@
#include <QAbstractSeries> #include <QAbstractSeries>
#include <QChart> #include <QChart>
#include <QGraphicsSceneHoverEvent> #include <QGraphicsSceneHoverEvent>
#include <QGraphicsSimpleTextItem>
#include <QLocale> #include <QLocale>
// Constants that control the graph layouts // Constants that control the graph layouts
static const QColor quartileMarkerColor(Qt::red); static const QColor quartileMarkerColor(Qt::red);
static const double quartileMarkerSize = 15; static const double quartileMarkerSize = 15.0;
static const double sceneBorder = 5.0; // Border between scene edges and statitistics view
static const double titleBorder = 2.0; // Border between title and chart
static const QUrl urlStatsView = QUrl(QStringLiteral("qrc:/qml/statsview.qml")); static const QUrl urlStatsView = QUrl(QStringLiteral("qrc:/qml/statsview.qml"));
@ -77,6 +80,9 @@ StatsView::StatsView(QWidget *parent) : QQuickWidget(parent),
chart->setAcceptHoverEvents(true); chart->setAcceptHoverEvents(true);
chart->legend()->setVisible(false); chart->legend()->setVisible(false);
} }
QFont font;
titleFont = QFont(font.family(), font.pointSize(), QFont::Light); // Make configurable
} }
StatsView::~StatsView() StatsView::~StatsView()
@ -95,6 +101,7 @@ void StatsView::plotAreaChanged(const QRectF &)
marker.updatePosition(); marker.updatePosition();
if (legend) if (legend)
legend->resize(); legend->resize();
updateTitlePos();
} }
void StatsView::replotIfVisible() void StatsView::replotIfVisible()
@ -136,7 +143,21 @@ T *StatsView::createSeries(Args&&... args)
void StatsView::setTitle(const QString &s) void StatsView::setTitle(const QString &s)
{ {
chart->setTitle(s); if (s.isEmpty()) {
title.reset();
return;
}
title = std::make_unique<QGraphicsSimpleTextItem>(s, chart);
title->setFont(titleFont);
}
void StatsView::updateTitlePos()
{
if (!title)
return;
QRectF rect = chart->plotArea();
title->setPos((rect.width() - title->boundingRect().width()) / 2.0,
sceneBorder);
} }
template <typename T, class... Args> template <typename T, class... Args>
@ -166,11 +187,18 @@ void StatsView::reset()
lineMarkers.clear(); lineMarkers.clear();
chart->removeAllSeries(); chart->removeAllSeries();
axes.clear(); axes.clear();
title.reset();
} }
void StatsView::plot(const StatsState &stateIn) void StatsView::plot(const StatsState &stateIn)
{ {
state = stateIn; state = stateIn;
plotChart();
plotAreaChanged(chart->plotArea());
}
void StatsView::plotChart()
{
if (!chart || !state.var1) if (!chart || !state.var1)
return; return;
reset(); reset();

View file

@ -4,6 +4,7 @@
#include "statsstate.h" #include "statsstate.h"
#include <memory> #include <memory>
#include <QFont>
#include <QQuickWidget> #include <QQuickWidget>
struct dive; struct dive;
@ -17,6 +18,7 @@ namespace QtCharts {
class QChart; class QChart;
} }
class QGraphicsLineItem; class QGraphicsLineItem;
class QGraphicsSimpleTextItem;
class StatsSeries; class StatsSeries;
class CategoryAxis; class CategoryAxis;
class CountAxis; class CountAxis;
@ -74,6 +76,8 @@ private:
const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable); const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable);
void plotScatter(const std::vector<dive *> &dives, const StatsVariable *categoryVariable, const StatsVariable *valueVariable); void plotScatter(const std::vector<dive *> &dives, const StatsVariable *categoryVariable, const StatsVariable *valueVariable);
void setTitle(const QString &); void setTitle(const QString &);
void updateTitlePos(); // After resizing, set title to correct position
void plotChart();
template <typename T, class... Args> template <typename T, class... Args>
T *createSeries(Args&&... args); T *createSeries(Args&&... args);
@ -115,11 +119,13 @@ private:
StatsState state; StatsState state;
QtCharts::QChart *chart; QtCharts::QChart *chart;
QFont titleFont;
std::vector<std::unique_ptr<StatsAxis>> axes; std::vector<std::unique_ptr<StatsAxis>> axes;
std::vector<std::unique_ptr<StatsSeries>> series; std::vector<std::unique_ptr<StatsSeries>> series;
std::unique_ptr<Legend> legend; std::unique_ptr<Legend> legend;
std::vector<QuartileMarker> quartileMarkers; std::vector<QuartileMarker> quartileMarkers;
std::vector<LineMarker> lineMarkers; std::vector<LineMarker> lineMarkers;
std::unique_ptr<QGraphicsSimpleTextItem> title;
StatsSeries *highlightedSeries; StatsSeries *highlightedSeries;
// This is unfortunate: we can't derive from QChart, because the chart is allocated by QML. // This is unfortunate: we can't derive from QChart, because the chart is allocated by QML.