statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
#include "statsaxis.h"
|
2021-01-05 11:11:46 +00:00
|
|
|
|
#include "statscolors.h"
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
#include "statshelper.h"
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
#include "statstranslations.h"
|
|
|
|
|
#include "statsvariables.h"
|
2021-01-14 22:22:24 +00:00
|
|
|
|
#include "statsview.h"
|
2021-01-05 11:11:46 +00:00
|
|
|
|
#include "zvalues.h"
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
#include "core/pref.h"
|
|
|
|
|
#include "core/subsurface-time.h"
|
|
|
|
|
#include <math.h> // for lrint
|
|
|
|
|
#include <numeric>
|
2022-04-27 04:15:37 +00:00
|
|
|
|
#include <array> // for std::array
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
#include <QFontMetrics>
|
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
// Define most constants for horizontal and vertical axes for more flexibility.
|
|
|
|
|
// Note: *Horizontal means that this is for the horizontal axis, so a vertical space.
|
|
|
|
|
static const double axisWidth = 0.5;
|
|
|
|
|
static const double axisTickWidth = 0.3;
|
|
|
|
|
static const double axisTickSizeHorizontal = 6.0;
|
|
|
|
|
static const double axisTickSizeVertical = 6.0;
|
|
|
|
|
static const double axisLabelSpaceHorizontal = 2.0; // Space between axis or ticks and labels
|
|
|
|
|
static const double axisLabelSpaceVertical = 2.0; // Space between axis or ticks and labels
|
|
|
|
|
static const double axisTitleSpaceHorizontal = 2.0; // Space between labels and title
|
|
|
|
|
static const double axisTitleSpaceVertical = 2.0; // Space between labels and title
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
StatsAxis::StatsAxis(StatsView &view, const QString &title, bool horizontal, bool labelsBetweenTicks) :
|
|
|
|
|
ChartPixmapItem(view, ChartZValue::Axes),
|
2021-02-16 16:05:39 +00:00
|
|
|
|
theme(view.getCurrentTheme()),
|
|
|
|
|
line(view.createChartItem<ChartLineItem>(ChartZValue::Axes, theme.axisColor, axisWidth)),
|
2021-01-14 22:22:24 +00:00
|
|
|
|
title(title), horizontal(horizontal), labelsBetweenTicks(labelsBetweenTicks),
|
2021-01-05 12:04:53 +00:00
|
|
|
|
size(1.0), zeroOnScreen(0.0), min(0.0), max(1.0), labelWidth(0.0)
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisTitleFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
titleWidth = title.isEmpty() ? 0.0
|
|
|
|
|
: static_cast<double>(fm.size(Qt::TextSingleLine, title).width());
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StatsAxis::~StatsAxis()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<double, double> StatsAxis::minMax() const
|
|
|
|
|
{
|
2021-01-05 11:11:46 +00:00
|
|
|
|
return { min, max };
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 13:17:51 +00:00
|
|
|
|
std::pair<double, double> StatsAxis::minMaxScreen() const
|
|
|
|
|
{
|
|
|
|
|
return horizontal ? std::make_pair(zeroOnScreen, zeroOnScreen + size)
|
|
|
|
|
: std::make_pair(zeroOnScreen, zeroOnScreen - size);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 11:59:17 +00:00
|
|
|
|
std::pair<double, double> StatsAxis::horizontalOverhang() const
|
|
|
|
|
{
|
|
|
|
|
// If the labels are between ticks, they cannot peak out
|
|
|
|
|
if (!horizontal || labelsBetweenTicks)
|
|
|
|
|
return { 0.0, 0.0 };
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
2021-01-11 11:59:17 +00:00
|
|
|
|
auto [firstLabel, lastLabel] = getFirstLastLabel();
|
|
|
|
|
return { fm.size(Qt::TextSingleLine, firstLabel).width() / 2.0,
|
|
|
|
|
fm.size(Qt::TextSingleLine, lastLabel).width() / 2.0 };
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
void StatsAxis::setRange(double minIn, double maxIn)
|
|
|
|
|
{
|
|
|
|
|
min = minIn;
|
|
|
|
|
max = maxIn;
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Guess the number of tick marks based on example strings.
|
|
|
|
|
// We will use minimum and maximum values, which are not necessarily the
|
|
|
|
|
// maximum-size strings especially, when using proportional fonts or for
|
|
|
|
|
// categorical data. Therefore, try to err on the safe side by adding enough
|
|
|
|
|
// margins.
|
2021-01-05 11:11:46 +00:00
|
|
|
|
int StatsAxis::guessNumTicks(const std::vector<QString> &strings) const
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
int minSize = fm.height();
|
|
|
|
|
for (const QString &s: strings) {
|
2021-01-05 11:11:46 +00:00
|
|
|
|
QSize labelSize = fm.size(Qt::TextSingleLine, s);
|
|
|
|
|
int needed = horizontal ? labelSize.width() : labelSize.height();
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
if (needed > minSize)
|
|
|
|
|
minSize = needed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add space between labels
|
|
|
|
|
if (horizontal)
|
|
|
|
|
minSize = minSize * 3 / 2;
|
|
|
|
|
else
|
|
|
|
|
minSize *= 2;
|
2021-01-05 11:11:46 +00:00
|
|
|
|
int numTicks = lrint(size / minSize);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
return std::max(numTicks, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 12:04:53 +00:00
|
|
|
|
double StatsAxis::titleSpace() const
|
|
|
|
|
{
|
2021-01-14 22:22:24 +00:00
|
|
|
|
if (title.isEmpty())
|
2021-01-05 12:04:53 +00:00
|
|
|
|
return 0.0;
|
2021-02-21 16:51:44 +00:00
|
|
|
|
return horizontal ? QFontMetrics(theme.axisTitleFont).height() + axisTitleSpaceHorizontal
|
|
|
|
|
: QFontMetrics(theme.axisTitleFont).height() + axisTitleSpaceVertical;
|
2021-01-05 12:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
double StatsAxis::width() const
|
|
|
|
|
{
|
|
|
|
|
if (horizontal)
|
|
|
|
|
return 0.0; // Only supported for vertical axes
|
2021-01-05 12:04:53 +00:00
|
|
|
|
return labelWidth + axisLabelSpaceVertical + titleSpace() +
|
2021-01-05 11:11:46 +00:00
|
|
|
|
(labelsBetweenTicks ? 0.0 : axisTickSizeVertical);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double StatsAxis::height() const
|
|
|
|
|
{
|
|
|
|
|
if (!horizontal)
|
|
|
|
|
return 0.0; // Only supported for horizontal axes
|
2021-02-21 16:51:44 +00:00
|
|
|
|
return QFontMetrics(theme.axisLabelFont).height() + axisLabelSpaceHorizontal +
|
2021-01-05 12:04:53 +00:00
|
|
|
|
titleSpace() +
|
2021-01-05 11:11:46 +00:00
|
|
|
|
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
void StatsAxis::addLabel(const QFontMetrics &fm, const QString &label, double pos)
|
2021-01-05 11:11:46 +00:00
|
|
|
|
{
|
2021-01-14 22:22:24 +00:00
|
|
|
|
labels.push_back({ label, fm.size(Qt::TextSingleLine, label).width(), pos });
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StatsAxis::addTick(double pos)
|
|
|
|
|
{
|
2021-02-16 16:05:39 +00:00
|
|
|
|
ticks.push_back({ view.createChartItem<ChartLineItem>(ChartZValue::Axes, theme.axisColor, axisTickWidth), pos });
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 12:51:39 +00:00
|
|
|
|
std::vector<double> StatsAxis::ticksPositions() const
|
|
|
|
|
{
|
|
|
|
|
std::vector<double> res;
|
|
|
|
|
res.reserve(ticks.size());
|
|
|
|
|
for (const Tick &tick: ticks)
|
|
|
|
|
res.push_back(toScreen(tick.pos));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
// Map x (horizontal) or y (vertical) coordinate to or from screen coordinate
|
|
|
|
|
double StatsAxis::toScreen(double pos) const
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-01-05 11:11:46 +00:00
|
|
|
|
// Vertical is bottom-up
|
|
|
|
|
return horizontal ? (pos - min) / (max - min) * size + zeroOnScreen
|
|
|
|
|
: (min - pos) / (max - min) * size + zeroOnScreen;
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
double StatsAxis::toValue(double pos) const
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-01-05 11:11:46 +00:00
|
|
|
|
// Vertical is bottom-up
|
|
|
|
|
return horizontal ? (pos - zeroOnScreen) / size * (max - min) + min
|
|
|
|
|
: (zeroOnScreen - pos) / size * (max - min) + zeroOnScreen;
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
void StatsAxis::setSize(double sizeIn)
|
|
|
|
|
{
|
|
|
|
|
size = sizeIn;
|
2021-01-14 22:22:24 +00:00
|
|
|
|
|
2021-01-18 21:29:34 +00:00
|
|
|
|
// Ticks (and labels) should probably be reused. For now, clear them.
|
|
|
|
|
for (Tick &tick: ticks)
|
|
|
|
|
view.deleteChartItem(tick.item);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
labels.clear();
|
|
|
|
|
ticks.clear();
|
2021-01-05 11:11:46 +00:00
|
|
|
|
updateLabels();
|
2021-01-14 22:22:24 +00:00
|
|
|
|
|
2021-01-05 12:04:53 +00:00
|
|
|
|
labelWidth = 0.0;
|
|
|
|
|
for (const Label &label: labels) {
|
2021-01-14 22:22:24 +00:00
|
|
|
|
if (label.width > labelWidth)
|
|
|
|
|
labelWidth = label.width;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
int fontHeight = fm.height();
|
|
|
|
|
if (horizontal) {
|
|
|
|
|
double pixmapWidth = size;
|
|
|
|
|
double offsetX = 0.0;
|
|
|
|
|
if (!labels.empty() && !labelsBetweenTicks) {
|
|
|
|
|
pixmapWidth += labels.front().width / 2.0 + labels.back().width / 2.0;
|
|
|
|
|
offsetX += labels.front().width / 2.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double pixmapHeight = fontHeight + titleSpace();
|
|
|
|
|
double offsetY = -axisWidth / 2.0 - axisLabelSpaceHorizontal -
|
|
|
|
|
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
|
|
|
|
|
|
|
|
|
ChartPixmapItem::resize(QSizeF(pixmapWidth, pixmapHeight)); // Note: this rounds the dimensions up
|
|
|
|
|
offset = QPointF(round(offsetX), round(offsetY));
|
|
|
|
|
img->fill(Qt::transparent);
|
|
|
|
|
|
2021-02-16 16:05:39 +00:00
|
|
|
|
painter->setPen(QPen(theme.darkLabelColor));
|
2021-02-21 16:51:44 +00:00
|
|
|
|
painter->setFont(theme.axisLabelFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
for (const Label &label: labels) {
|
|
|
|
|
double x = (label.pos - min) / (max - min) * size + offset.x() - round(label.width / 2.0);
|
|
|
|
|
QRectF rect(x, 0.0, label.width, fontHeight);
|
|
|
|
|
painter->drawText(rect, label.label);
|
|
|
|
|
}
|
|
|
|
|
if (!title.isEmpty()) {
|
|
|
|
|
QRectF rect(offset.x() + round((size - titleWidth) / 2.0),
|
|
|
|
|
fontHeight + axisTitleSpaceHorizontal,
|
|
|
|
|
titleWidth, fontHeight);
|
2021-02-21 16:51:44 +00:00
|
|
|
|
painter->setFont(theme.axisTitleFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
painter->drawText(rect, title);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
double pixmapWidth = labelWidth + titleSpace();
|
|
|
|
|
double offsetX = pixmapWidth + axisLabelSpaceVertical + (labelsBetweenTicks ? 0.0 : axisTickSizeVertical);
|
|
|
|
|
|
|
|
|
|
double pixmapHeight = ceil(size + axisTickWidth);
|
|
|
|
|
double offsetY = size;
|
|
|
|
|
if (!labels.empty() && !labelsBetweenTicks) {
|
|
|
|
|
pixmapHeight += fontHeight;
|
|
|
|
|
offsetY += fontHeight / 2.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChartPixmapItem::resize(QSizeF(pixmapWidth, pixmapHeight)); // Note: this rounds the dimensions up
|
|
|
|
|
offset = QPointF(round(offsetX), round(offsetY));
|
|
|
|
|
img->fill(Qt::transparent);
|
|
|
|
|
|
2021-02-16 16:05:39 +00:00
|
|
|
|
painter->setPen(QPen(theme.darkLabelColor));
|
2021-02-21 16:51:44 +00:00
|
|
|
|
painter->setFont(theme.axisLabelFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
for (const Label &label: labels) {
|
|
|
|
|
double y = (min - label.pos) / (max - min) * size + offset.y() - round(fontHeight / 2.0);
|
|
|
|
|
QRectF rect(pixmapWidth - label.width, y, label.width, fontHeight);
|
|
|
|
|
painter->drawText(rect, label.label);
|
|
|
|
|
}
|
|
|
|
|
if (!title.isEmpty()) {
|
|
|
|
|
painter->rotate(-90.0);
|
|
|
|
|
QRectF rect(round(-(offsetY + titleWidth) / 2.0), 0.0, titleWidth, fontHeight);
|
2021-02-21 16:51:44 +00:00
|
|
|
|
painter->setFont(theme.axisTitleFont);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
painter->drawText(rect, title);
|
|
|
|
|
painter->resetTransform();
|
|
|
|
|
}
|
2021-01-05 12:04:53 +00:00
|
|
|
|
}
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StatsAxis::setPos(QPointF pos)
|
|
|
|
|
{
|
2021-01-14 22:22:24 +00:00
|
|
|
|
zeroOnScreen = horizontal ? pos.x() : pos.y();
|
|
|
|
|
ChartPixmapItem::setPos(pos - offset);
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
if (horizontal) {
|
|
|
|
|
double y = pos.y();
|
2021-01-14 22:22:24 +00:00
|
|
|
|
for (const Tick &tick: ticks) {
|
2021-01-05 11:11:46 +00:00
|
|
|
|
double x = toScreen(tick.pos);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
tick.item->setLine(QPointF(x, y), QPointF(x, y + axisTickSizeHorizontal));
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
2021-01-14 22:22:24 +00:00
|
|
|
|
line->setLine(QPointF(zeroOnScreen, y), QPointF(zeroOnScreen + size, y));
|
2021-01-05 11:11:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
double x = pos.x();
|
2021-01-14 22:22:24 +00:00
|
|
|
|
for (const Tick &tick: ticks) {
|
2021-01-05 11:11:46 +00:00
|
|
|
|
double y = toScreen(tick.pos);
|
2021-01-14 22:22:24 +00:00
|
|
|
|
tick.item->setLine(QPointF(x, y), QPointF(x - axisTickSizeVertical, y));
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
2021-01-14 22:22:24 +00:00
|
|
|
|
line->setLine(QPointF(x, zeroOnScreen), QPointF(x, zeroOnScreen - size));
|
2021-01-05 11:11:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
ValueAxis::ValueAxis(StatsView &view, const QString &title, double min, double max, int decimals, bool horizontal) :
|
|
|
|
|
StatsAxis(view, title, horizontal, false),
|
2021-01-05 11:11:46 +00:00
|
|
|
|
min(min), max(max), decimals(decimals)
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-01-11 11:59:17 +00:00
|
|
|
|
// Avoid degenerate cases
|
|
|
|
|
if (max - min < 0.0001) {
|
|
|
|
|
max += 0.5;
|
|
|
|
|
min -= 0.5;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attention: this is only heuristics. Before setting the actual size, we
|
|
|
|
|
// don't know the actual numbers of the minimum and maximum value.
|
|
|
|
|
std::pair<QString, QString> ValueAxis::getFirstLastLabel() const
|
|
|
|
|
{
|
|
|
|
|
QLocale loc;
|
|
|
|
|
return { loc.toString(min, 'f', decimals), loc.toString(max, 'f', decimals) };
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 16:16:13 +00:00
|
|
|
|
void ValueAxis::updateLabels()
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
|
|
|
|
QLocale loc;
|
2021-01-11 11:59:17 +00:00
|
|
|
|
auto [minString, maxString] = getFirstLastLabel();
|
2021-01-05 11:11:46 +00:00
|
|
|
|
int numTicks = guessNumTicks({ minString, maxString});
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
|
|
|
|
|
// Use full decimal increments
|
|
|
|
|
double height = max - min;
|
|
|
|
|
double inc = height / numTicks;
|
|
|
|
|
double digits = floor(log10(inc));
|
|
|
|
|
int digits_int = lrint(digits);
|
|
|
|
|
double digits_factor = pow(10.0, digits);
|
|
|
|
|
int inc_int = std::max((int)ceil(inc / digits_factor), 1);
|
|
|
|
|
// Do "nice" increments of the leading digit: 1, 2, 4, 5.
|
|
|
|
|
if (inc_int > 5)
|
|
|
|
|
inc_int = 10;
|
|
|
|
|
if (inc_int == 3)
|
|
|
|
|
inc_int = 4;
|
|
|
|
|
inc = inc_int * digits_factor;
|
|
|
|
|
if (-digits_int > decimals)
|
|
|
|
|
decimals = -digits_int;
|
|
|
|
|
|
|
|
|
|
double actMin = floor(min / inc) * inc;
|
|
|
|
|
double actMax = ceil(max / inc) * inc;
|
|
|
|
|
int num = lrint((actMax - actMin) / inc);
|
|
|
|
|
setRange(actMin, actMax);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
|
|
|
|
|
double actStep = (actMax - actMin) / static_cast<double>(num);
|
|
|
|
|
double act = actMin;
|
|
|
|
|
labels.reserve(num + 1);
|
|
|
|
|
ticks.reserve(num + 1);
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
for (int i = 0; i <= num; ++i) {
|
2021-01-14 22:22:24 +00:00
|
|
|
|
addLabel(fm, loc.toString(act, 'f', decimals), act);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
addTick(act);
|
|
|
|
|
act += actStep;
|
|
|
|
|
}
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
CountAxis::CountAxis(StatsView &view, const QString &title, int count, bool horizontal) :
|
|
|
|
|
ValueAxis(view, title, 0.0, (double)count, 0, horizontal),
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
count(count)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 11:59:17 +00:00
|
|
|
|
// Attention: this is only heuristics. Before setting the actual size, we
|
|
|
|
|
// don't know the actual numbers of the minimum and maximum value.
|
|
|
|
|
std::pair<QString, QString> CountAxis::getFirstLastLabel() const
|
|
|
|
|
{
|
|
|
|
|
QLocale loc;
|
|
|
|
|
return { QString("0"), loc.toString(count) };
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 16:16:13 +00:00
|
|
|
|
void CountAxis::updateLabels()
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
|
|
|
|
QLocale loc;
|
|
|
|
|
QString countString = loc.toString(count);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
int numTicks = guessNumTicks({ countString });
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
|
|
|
|
|
// Get estimate of step size
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
count = 1;
|
|
|
|
|
// When determining the step size, make sure to round up
|
|
|
|
|
int step = (count + numTicks - 1) / numTicks;
|
|
|
|
|
if (step <= 0)
|
|
|
|
|
step = 1;
|
|
|
|
|
|
|
|
|
|
// Get the significant first or first two digits
|
|
|
|
|
int scale = 1;
|
|
|
|
|
int significant = step;
|
|
|
|
|
while (significant > 25) {
|
|
|
|
|
significant /= 10;
|
|
|
|
|
scale *= 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int increment: { 1, 2, 4, 5, 10, 15, 20, 25 }) {
|
|
|
|
|
if (increment >= significant) {
|
|
|
|
|
significant = increment;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
step = significant * scale;
|
|
|
|
|
|
|
|
|
|
// Make maximum an integer number of steps, equal or greater than the needed counts
|
|
|
|
|
int num = (count - 1) / step + 1;
|
|
|
|
|
int max = num * step;
|
|
|
|
|
|
|
|
|
|
setRange(0, max);
|
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
|
labels.reserve(max + 1);
|
|
|
|
|
ticks.reserve(max + 1);
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
for (int i = 0; i <= max; i += step) {
|
2021-01-14 22:22:24 +00:00
|
|
|
|
addLabel(fm, loc.toString(i), static_cast<double>(i));
|
2021-01-05 11:11:46 +00:00
|
|
|
|
addTick(static_cast<double>(i));
|
|
|
|
|
}
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
CategoryAxis::CategoryAxis(StatsView &view, const QString &title, const std::vector<QString> &labels, bool horizontal) :
|
|
|
|
|
StatsAxis(view, title, horizontal, true),
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
labelsText(labels)
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
2021-01-18 21:41:43 +00:00
|
|
|
|
if (!labels.empty())
|
|
|
|
|
setRange(-0.5, static_cast<double>(labels.size()) - 0.5);
|
|
|
|
|
else
|
|
|
|
|
setRange(-1.0, 1.0);
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 11:59:17 +00:00
|
|
|
|
// No implementation because the labels are inside ticks and this
|
|
|
|
|
// is only used to calculate the "overhang" of labels under ticks.
|
|
|
|
|
std::pair<QString, QString> CategoryAxis::getFirstLastLabel() const
|
|
|
|
|
{
|
|
|
|
|
return { QString(), QString() };
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 12:29:15 +00:00
|
|
|
|
// Get ellipses for a given label size
|
|
|
|
|
static QString ellipsis1 = QStringLiteral("․");
|
|
|
|
|
static QString ellipsis2 = QStringLiteral("‥");
|
|
|
|
|
static QString ellipsis3 = QStringLiteral("…");
|
|
|
|
|
static QString getEllipsis(const QFontMetrics &fm, double widthIn)
|
|
|
|
|
{
|
|
|
|
|
int width = static_cast<int>(floor(widthIn));
|
|
|
|
|
if (fm.size(Qt::TextSingleLine, ellipsis3).width() < width)
|
|
|
|
|
return ellipsis3;
|
|
|
|
|
if (fm.size(Qt::TextSingleLine, ellipsis2).width() < width)
|
|
|
|
|
return ellipsis2;
|
|
|
|
|
if (fm.size(Qt::TextSingleLine, ellipsis1).width() < width)
|
|
|
|
|
return ellipsis1;
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
void CategoryAxis::updateLabels()
|
|
|
|
|
{
|
2021-01-11 12:29:15 +00:00
|
|
|
|
if (labelsText.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
2021-01-11 12:29:15 +00:00
|
|
|
|
double size_per_label = size / static_cast<double>(labelsText.size()) - axisTickWidth;
|
|
|
|
|
double fontHeight = fm.height();
|
|
|
|
|
|
|
|
|
|
QString ellipsis = horizontal ? getEllipsis(fm, size_per_label) : QString();
|
|
|
|
|
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
labels.reserve(labelsText.size());
|
|
|
|
|
ticks.reserve(labelsText.size() + 1);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
double pos = 0.0;
|
|
|
|
|
addTick(-0.5);
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
|
for (const QString &s: labelsText) {
|
2021-01-11 12:29:15 +00:00
|
|
|
|
if (horizontal) {
|
|
|
|
|
double width = static_cast<double>(fm.size(Qt::TextSingleLine, s).width());
|
2021-01-14 22:22:24 +00:00
|
|
|
|
addLabel(fm, width < size_per_label ? s : ellipsis, pos);
|
2021-01-11 12:29:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (fontHeight < size_per_label)
|
2021-01-14 22:22:24 +00:00
|
|
|
|
addLabel(fm, s, pos);
|
2021-01-11 12:29:15 +00:00
|
|
|
|
}
|
2021-01-05 11:11:46 +00:00
|
|
|
|
addTick(pos + 0.5);
|
|
|
|
|
pos += 1.0;
|
|
|
|
|
}
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
HistogramAxis::HistogramAxis(StatsView &view, const QString &title, std::vector<HistogramAxisEntry> bins, bool horizontal) :
|
|
|
|
|
StatsAxis(view, title, horizontal, false),
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
bin_values(std::move(bins))
|
|
|
|
|
{
|
|
|
|
|
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// The caller can declare some bin labels as preferred, when there are
|
|
|
|
|
// too many labels to show all. Try to infer the preferred step size
|
|
|
|
|
// by finding two consecutive preferred labels. This supposes that
|
|
|
|
|
// the preferred labels are equi-distant and that the caller does not
|
|
|
|
|
// use large prime (or nearly prime) steps.
|
|
|
|
|
auto it1 = std::find_if(bin_values.begin(), bin_values.end(),
|
|
|
|
|
[](const HistogramAxisEntry &e) { return e.recommended; });
|
|
|
|
|
auto next_it = it1 == bin_values.end() ? it1 : std::next(it1);
|
|
|
|
|
auto it2 = std::find_if(next_it, bin_values.end(),
|
|
|
|
|
[](const HistogramAxisEntry &e) { return e.recommended; });
|
|
|
|
|
preferred_step = it2 == bin_values.end() ? 1 : it2 - it1;
|
2021-01-05 11:11:46 +00:00
|
|
|
|
setRange(bin_values.front().value, bin_values.back().value);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 11:59:17 +00:00
|
|
|
|
std::pair<QString, QString> HistogramAxis::getFirstLastLabel() const
|
|
|
|
|
{
|
|
|
|
|
if (bin_values.empty())
|
|
|
|
|
return { QString(), QString() };
|
|
|
|
|
else
|
|
|
|
|
return { bin_values.front().name, bin_values.back().name };
|
|
|
|
|
}
|
|
|
|
|
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
// Initialize a histogram axis with the given labels. Labels are specified as (name, value, recommended) triplets.
|
|
|
|
|
// If labels are skipped, try to skip it in such a way that a recommended label is shown.
|
|
|
|
|
// The one example where this is relevant is the quarterly bins, which are formated as (2019, q1, q2, q3, 2020, ...).
|
|
|
|
|
// There, we obviously want to show the years and not the quarters.
|
2021-01-04 16:16:13 +00:00
|
|
|
|
void HistogramAxis::updateLabels()
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::vector<QString> strings;
|
|
|
|
|
strings.reserve(bin_values.size());
|
|
|
|
|
for (auto &[name, value, recommended]: bin_values)
|
|
|
|
|
strings.push_back(name);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
int maxLabels = guessNumTicks(strings);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
|
|
|
|
|
int step = ((int)bin_values.size() - 1) / maxLabels + 1;
|
|
|
|
|
if (step < preferred_step) {
|
|
|
|
|
if (step * 2 > preferred_step) {
|
|
|
|
|
step = preferred_step;
|
|
|
|
|
} else {
|
|
|
|
|
int gcd = std::gcd(step, preferred_step);
|
|
|
|
|
while (preferred_step % step != 0)
|
|
|
|
|
step += gcd;
|
|
|
|
|
}
|
|
|
|
|
} else if (step > preferred_step) {
|
|
|
|
|
int remainder = (step + preferred_step) % preferred_step;
|
|
|
|
|
if (remainder != 0)
|
|
|
|
|
step = step + preferred_step - remainder;
|
|
|
|
|
}
|
|
|
|
|
int first = 0;
|
|
|
|
|
if (step > 1) {
|
|
|
|
|
for (int i = 0; i < (int)bin_values.size(); ++i) {
|
|
|
|
|
const auto &[name, value, recommended] = bin_values[i];
|
|
|
|
|
if (recommended) {
|
|
|
|
|
first = i % step;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-05 11:11:46 +00:00
|
|
|
|
labels.reserve((bin_values.size() - first) / step + 1);
|
2021-01-07 13:59:47 +00:00
|
|
|
|
// Always add a tick at the beginning of the axis - this is
|
|
|
|
|
// important for the grid, which uses the ticks.
|
|
|
|
|
if (first != 0)
|
|
|
|
|
addTick(bin_values.front().value);
|
|
|
|
|
int last = first;
|
2021-02-21 16:51:44 +00:00
|
|
|
|
QFontMetrics fm(theme.axisLabelFont);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
for (int i = first; i < (int)bin_values.size(); i += step) {
|
|
|
|
|
const auto &[name, value, recommended] = bin_values[i];
|
2021-01-14 22:22:24 +00:00
|
|
|
|
addLabel(fm, name, value);
|
2021-01-05 11:11:46 +00:00
|
|
|
|
addTick(value);
|
2021-01-07 13:59:47 +00:00
|
|
|
|
last = i;
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
2021-01-07 13:59:47 +00:00
|
|
|
|
// Always add a tick at the end of the axis (see above).
|
|
|
|
|
if (last != (int)bin_values.size() - 1)
|
|
|
|
|
addTick(bin_values.back().value);
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper function to turn days since "Unix epoch" into a timestamp_t
|
|
|
|
|
static const double seconds_in_day = 86400.0;
|
|
|
|
|
static timestamp_t double_to_timestamp(double d)
|
|
|
|
|
{
|
|
|
|
|
return timestamp_t{ lrint(d * seconds_in_day) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Turn double to (year, month) pair
|
|
|
|
|
static std::pair<int, int> double_to_month(double d)
|
|
|
|
|
{
|
|
|
|
|
struct tm tm;
|
|
|
|
|
utc_mkdate(double_to_timestamp(d), &tm);
|
|
|
|
|
return { tm.tm_year, tm.tm_mon };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increase (year, month) pair by one month
|
|
|
|
|
static void inc(std::pair<int, int> &ym)
|
|
|
|
|
{
|
|
|
|
|
if (++ym.second >= 12) {
|
|
|
|
|
++ym.first;
|
|
|
|
|
ym.second = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::array<int, 3> double_to_day(double d)
|
|
|
|
|
{
|
|
|
|
|
struct tm tm;
|
|
|
|
|
utc_mkdate(double_to_timestamp(d), &tm);
|
|
|
|
|
return { tm.tm_year, tm.tm_mon, tm.tm_mday };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is trashy: to increase a day, turn into timestamp and back.
|
|
|
|
|
// This surely can be done better.
|
|
|
|
|
static void inc(std::array<int, 3> &ymd)
|
|
|
|
|
{
|
|
|
|
|
struct tm tm = { 0 };
|
|
|
|
|
tm.tm_year = ymd[0];
|
|
|
|
|
tm.tm_mon = ymd[1];
|
|
|
|
|
tm.tm_mday = ymd[2] + 1;
|
|
|
|
|
timestamp_t t = utc_mktime(&tm);
|
|
|
|
|
utc_mkdate(t, &tm);
|
|
|
|
|
ymd = { tm.tm_year, tm.tm_mon, tm.tm_mday };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use heuristics to determine the preferred day/month format:
|
|
|
|
|
// Try to see whether day or month comes first and try to extract
|
|
|
|
|
// the separator character. Returns a (day_first, separator) pair.
|
|
|
|
|
static std::pair<bool, char> day_format()
|
|
|
|
|
{
|
|
|
|
|
const char *fmt = prefs.date_format;
|
|
|
|
|
const char *d, *m, *sep;
|
|
|
|
|
for (d = fmt; *d && *d != 'd' && *d != 'D'; ++d)
|
|
|
|
|
;
|
|
|
|
|
for (m = fmt; *m && *m != 'm' && *m != 'M'; ++m)
|
|
|
|
|
;
|
|
|
|
|
for(sep = std::min(m, d); *sep == 'm' || *sep == 'M' || *sep == 'd' || *sep == 'D'; ++sep)
|
|
|
|
|
;
|
|
|
|
|
return { d < m, *sep ? *sep : '.' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For now, misuse the histogram axis for creating a time axis. Depending on the range,
|
|
|
|
|
// create year, month or day-based bins. This is certainly not efficient and may need
|
|
|
|
|
// some tuning. However, it should ensure that no crazy number of bins is generated.
|
|
|
|
|
// Ultimately, this should be replaced by a better and dynamic scheme
|
|
|
|
|
// From and to are given in seconds since "epoch".
|
|
|
|
|
static std::vector<HistogramAxisEntry> timeRangeToBins(double from, double to)
|
|
|
|
|
{
|
|
|
|
|
// from and two are given in days since the "Unix epoch".
|
|
|
|
|
// The lowest precision we do is two days.
|
|
|
|
|
if (to - from < 2.0) {
|
|
|
|
|
double center = (from + to) / 2.0;
|
|
|
|
|
from = center + 1.0;
|
|
|
|
|
to = center - 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<HistogramAxisEntry> res;
|
|
|
|
|
if (to - from > 2.0 * 356.0) {
|
|
|
|
|
// For two years or more, do year based bins
|
|
|
|
|
int year_from = utc_year(double_to_timestamp(from));
|
|
|
|
|
int year_to = utc_year(double_to_timestamp(to)) + 1;
|
|
|
|
|
for (int year = year_from; year <= year_to; ++year)
|
|
|
|
|
res.push_back({ QString::number(year), date_to_double(year, 0, 0), true });
|
|
|
|
|
} else if (to - from > 2.0 * 30.0) {
|
|
|
|
|
// For two months or more, do month based bins
|
|
|
|
|
auto year_month_from = double_to_month(from);
|
|
|
|
|
auto year_month_to = double_to_month(to);
|
|
|
|
|
inc(year_month_to);
|
|
|
|
|
for (auto act = year_month_from; act <= year_month_to; inc(act)) {
|
|
|
|
|
double val = date_to_double(act.first, act.second, 0);
|
|
|
|
|
if (act.second == 0)
|
|
|
|
|
res.push_back({ QString::number(act.first), val, true });
|
|
|
|
|
else
|
|
|
|
|
res.push_back({ monthname(act.second), val, false });
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// For less than two months, do date based bins
|
|
|
|
|
auto day_from = double_to_day(from);
|
|
|
|
|
auto day_to = double_to_day(to);
|
|
|
|
|
inc(day_to);
|
|
|
|
|
auto [day_before_month, separator] = day_format();
|
|
|
|
|
QString format = day_before_month ? QStringLiteral("%1%2%3")
|
|
|
|
|
: QStringLiteral("%3%2%1");
|
|
|
|
|
QString sep = QString(separator);
|
|
|
|
|
for (auto act = day_from; act < day_to; inc(act)) {
|
|
|
|
|
double val = date_to_double(act[0], act[1], act[2]);
|
|
|
|
|
if (act[1] == 0) {
|
|
|
|
|
res.push_back({ QString::number(act[0]), val, true });
|
|
|
|
|
} else if (act[2] == 0) {
|
|
|
|
|
res.push_back({ monthname(act[1]), val, true });
|
|
|
|
|
} else {
|
|
|
|
|
QString s = format.arg(QString::number(act[2]), sep, QString::number(act[1]));
|
|
|
|
|
res.push_back({s, val, true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 22:22:24 +00:00
|
|
|
|
DateAxis::DateAxis(StatsView &view, const QString &title, double from, double to, bool horizontal) :
|
|
|
|
|
HistogramAxis(view, title, timeRangeToBins(from, to), horizontal)
|
statistics: implement axes
Implement five kinds of axes:
- ValueAxis: a standard axis for plotting numerical linear data.
- CountAxis: a ValueAxis for plotting counts of dives.
- CategoryAxis: an axis for plotting discrete variables without
any notion of distance.
- HistogramAxis: an axis for plotting bins with a numeric value.
- DateAxis: a HistogramAxis that formats dates.
The axes derive from a common virtual base class that defines
a small interface, notably, returning the minimum and maximum
displayed value and redrawing the axis.
The mapping and painting is performed by QtCharts' axes. On
the one hand, using QtCharts turned out to be too inflexible.
On the other hand it allowed us to quickly prototype the charts.
Ultimately, we should do our own drawing of the axis.
As a testament to the inflexibility, QtCharts' axes do not
allow for repeated labels is needed for quarter-based date
charts (year, Q2, Q3, Q4, year, Q2, Q3, ...). Therefore the
code disambiguates labels by adding unicode zero-width spaces.
Wonderful.
When omitting labels due to space reasons, the histogram
axis attempts to show "preferred" labels. In the quarter
example above, it tries to show full years.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-01 20:35:56 +00:00
|
|
|
|
{
|
|
|
|
|
}
|