2021-01-01 21:26:52 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "pieseries.h"
|
|
|
|
#include "informationbox.h"
|
|
|
|
#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"
|
2021-01-01 21:26:52 +00:00
|
|
|
#include "statstranslations.h"
|
2021-01-13 14:17:54 +00:00
|
|
|
#include "statsview.h"
|
2021-01-01 21:26:52 +00:00
|
|
|
#include "zvalues.h"
|
2021-01-17 12:34:18 +00:00
|
|
|
#include "core/selection.h"
|
2021-01-01 21:26:52 +00:00
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
#include <math.h>
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
|
|
static const double pieSize = 0.9; // 1.0 = occupy full width of chart
|
|
|
|
static const double pieBorderWidth = 1.0;
|
|
|
|
static const double innerLabelRadius = 0.75; // 1.0 = at outer border of pie
|
|
|
|
static const double outerLabelRadius = 1.01; // 1.0 = at outer border of pie
|
|
|
|
|
2021-01-20 13:36:59 +00:00
|
|
|
PieSeries::Item::Item(StatsView &view, const QString &name, int from, std::vector<dive *> divesIn, int totalCount,
|
2021-02-16 16:05:39 +00:00
|
|
|
int bin_nr, int numBins, const StatsTheme &theme) :
|
2021-01-01 21:26:52 +00:00
|
|
|
name(name),
|
2021-02-07 19:48:43 +00:00
|
|
|
dives(std::move(divesIn)),
|
|
|
|
selected(allDivesSelected(dives))
|
2021-01-01 21:26:52 +00:00
|
|
|
{
|
2021-02-21 16:51:44 +00:00
|
|
|
const QFont &f = theme.labelFont;
|
2021-01-01 21:26:52 +00:00
|
|
|
QLocale loc;
|
|
|
|
|
2021-01-20 13:36:59 +00:00
|
|
|
int count = (int)dives.size();
|
2021-03-05 08:35:13 +00:00
|
|
|
|
|
|
|
// totalCount = 0 shouldn't happen, but for robustness' sake, let's not divide by zero.
|
|
|
|
if (totalCount <= 0)
|
|
|
|
totalCount = count = 1;
|
|
|
|
|
2021-01-18 11:08:46 +00:00
|
|
|
angleFrom = static_cast<double>(from) / totalCount;
|
2021-01-01 21:26:52 +00:00
|
|
|
angleTo = static_cast<double>(from + count) / totalCount;
|
|
|
|
double meanAngle = M_PI / 2.0 - (from + count / 2.0) / totalCount * M_PI * 2.0; // Note: "-" because we go CW.
|
|
|
|
innerLabelPos = QPointF(cos(meanAngle) * innerLabelRadius, -sin(meanAngle) * innerLabelRadius);
|
|
|
|
outerLabelPos = QPointF(cos(meanAngle) * outerLabelRadius, -sin(meanAngle) * outerLabelRadius);
|
|
|
|
|
2021-01-19 08:54:39 +00:00
|
|
|
double percentage = count * 100.0 / totalCount;
|
|
|
|
QString innerLabelText = QStringLiteral("%1\%").arg(loc.toString(percentage, 'f', 1));
|
|
|
|
innerLabel = view.createChartItem<ChartTextItem>(ChartZValue::SeriesLabels, f, innerLabelText);
|
2021-01-01 21:26:52 +00:00
|
|
|
|
2021-01-19 08:54:39 +00:00
|
|
|
outerLabel = view.createChartItem<ChartTextItem>(ChartZValue::SeriesLabels, f, name);
|
2021-02-16 16:05:39 +00:00
|
|
|
outerLabel->setColor(theme.darkLabelColor);
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:08:46 +00:00
|
|
|
void PieSeries::Item::updatePositions(const QPointF ¢er, double radius)
|
2021-01-01 21:26:52 +00:00
|
|
|
{
|
2021-01-18 11:08:46 +00:00
|
|
|
// Note: the positions in this functions are rounded to integer values,
|
|
|
|
// because half-integer values gives horrible aliasing artifacts.
|
2021-01-01 21:26:52 +00:00
|
|
|
if (innerLabel) {
|
2021-01-18 11:08:46 +00:00
|
|
|
QRectF labelRect = innerLabel->getRect();
|
2021-02-06 11:26:54 +00:00
|
|
|
QPointF pos(center.x() + innerLabelPos.x() * radius - labelRect.width() / 2.0,
|
|
|
|
center.y() + innerLabelPos.y() * radius - labelRect.height() / 2.0);
|
|
|
|
innerLabel->setPos(roundPos(pos));
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
if (outerLabel) {
|
2021-01-18 11:08:46 +00:00
|
|
|
QRectF labelRect = outerLabel->getRect();
|
2021-01-01 21:26:52 +00:00
|
|
|
QPointF pos(center.x() + outerLabelPos.x() * radius, center.y() + outerLabelPos.y() * radius);
|
|
|
|
if (outerLabelPos.x() < 0.0) {
|
|
|
|
if (outerLabelPos.y() < 0.0)
|
|
|
|
pos -= QPointF(labelRect.width(), labelRect.height());
|
|
|
|
else
|
|
|
|
pos.rx() -= labelRect.width();
|
|
|
|
} else if (outerLabelPos.y() < 0.0) {
|
|
|
|
pos.ry() -= labelRect.height();
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:26:54 +00:00
|
|
|
outerLabel->setPos(roundPos(pos));
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:05:39 +00:00
|
|
|
void PieSeries::Item::highlight(ChartPieItem &item, int bin_nr, bool highlight, int numBins, const StatsTheme &theme)
|
2021-01-01 21:26:52 +00:00
|
|
|
{
|
2021-02-16 16:05:39 +00:00
|
|
|
QColor fill = highlight ? theme.highlightedColor : theme.binColor(bin_nr, numBins);
|
|
|
|
QColor border = highlight ? theme.highlightedBorderColor : theme.borderColor;
|
2021-01-18 11:08:46 +00:00
|
|
|
if (innerLabel)
|
2021-02-16 16:05:39 +00:00
|
|
|
innerLabel->setColor(highlight ? theme.darkLabelColor : theme.labelColor(bin_nr, numBins), fill);
|
|
|
|
item.drawSegment(angleFrom, angleTo, fill, border, selected, theme);
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
PieSeries::PieSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis, const QString &categoryName,
|
2021-12-31 17:29:06 +00:00
|
|
|
std::vector<std::pair<QString, std::vector<dive *>>> data, ChartSortMode sortMode) :
|
2021-01-18 11:35:22 +00:00
|
|
|
StatsSeries(view, xAxis, yAxis),
|
2021-01-18 11:08:46 +00:00
|
|
|
item(view.createChartItem<ChartPieItem>(ChartZValue::Series, pieBorderWidth)),
|
2021-01-01 21:26:52 +00:00
|
|
|
categoryName(categoryName),
|
2021-03-05 07:22:19 +00:00
|
|
|
radius(0),
|
2021-02-10 19:59:34 +00:00
|
|
|
highlighted(-1),
|
|
|
|
lastClicked(-1)
|
2021-01-01 21:26:52 +00:00
|
|
|
{
|
|
|
|
// Pie charts with many slices are unreadable. Therefore, subsume slices under
|
|
|
|
// a certain percentage as "other". But draw a minimum number of slices
|
|
|
|
// until we reach 50% so that we never get a pie only of "other".
|
|
|
|
// This is heuristics, which might have to be optimized.
|
|
|
|
const int smallest_slice_percentage = 5; // Smaller than 5% = others. That makes at most 20 slices.
|
|
|
|
const int min_slices = 5; // Try to draw at least 5 slices until we reach 50%
|
|
|
|
|
|
|
|
// Easier to read than std::accumulate
|
|
|
|
totalCount = 0;
|
2021-01-20 13:36:59 +00:00
|
|
|
for (const auto &[name, dives]: data)
|
|
|
|
totalCount += (int)dives.size();
|
2021-01-01 21:26:52 +00:00
|
|
|
|
2021-12-17 07:08:11 +00:00
|
|
|
// First of all, sort from largest to smallest slice. Instead
|
2021-01-01 21:26:52 +00:00
|
|
|
// of sorting the initial array, sort a list of indices, so that
|
|
|
|
// the original order can be easily reconstructed later.
|
|
|
|
std::vector<int> sorted(data.size());
|
|
|
|
std::iota(sorted.begin(), sorted.end(), 0); // Fill with 0..size-1.
|
|
|
|
// Two notes:
|
|
|
|
// - by negating the counts in the sort below, count is sorted descending.
|
|
|
|
// - do a lexicographic sort by (count, idx) so that for equal counts the order is preserved.
|
|
|
|
std::sort(sorted.begin(), sorted.end(),
|
|
|
|
[&data](int idx1, int idx2)
|
2021-01-20 13:36:59 +00:00
|
|
|
{ return std::make_tuple(-data[idx1].second.size(), idx1) <
|
|
|
|
std::make_tuple(-data[idx2].second.size(), idx2); });
|
2021-01-01 21:26:52 +00:00
|
|
|
auto it = std::find_if(sorted.begin(), sorted.end(),
|
2021-01-11 12:22:39 +00:00
|
|
|
[count=totalCount, &data](int idx)
|
2021-01-20 13:36:59 +00:00
|
|
|
{ return (int)data[idx].second.size() * 100 / count < smallest_slice_percentage; });
|
2021-01-01 21:26:52 +00:00
|
|
|
if (it - sorted.begin() < min_slices) {
|
|
|
|
// Take minimum amount of slices below 50%...
|
|
|
|
int sum = 0;
|
|
|
|
for (auto it2 = sorted.begin(); it2 != it; ++it2)
|
2021-01-20 13:36:59 +00:00
|
|
|
sum += (int)data[*it2].second.size();
|
2021-01-01 21:26:52 +00:00
|
|
|
|
|
|
|
while(it != sorted.end() && sum * 2 < totalCount && it - sorted.begin() < min_slices) {
|
2021-01-20 13:36:59 +00:00
|
|
|
sum += (int)data[*it].second.size();
|
2021-01-01 21:26:52 +00:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do a single "other" group
|
|
|
|
if (sorted.end() - it == 1)
|
|
|
|
++it;
|
|
|
|
|
|
|
|
// Sort the main groups and the other groups back, if requested
|
2021-12-31 17:29:06 +00:00
|
|
|
if (sortMode == ChartSortMode::Bin) {
|
2021-01-01 21:26:52 +00:00
|
|
|
std::sort(sorted.begin(), it);
|
|
|
|
std::sort(it, sorted.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
int numBins = it - sorted.begin();
|
|
|
|
if (it != sorted.end())
|
|
|
|
++numBins;
|
|
|
|
items.reserve(numBins);
|
|
|
|
int act = 0;
|
|
|
|
for (auto it2 = sorted.begin(); it2 != it; ++it2) {
|
2021-01-20 13:36:59 +00:00
|
|
|
int count = (int)data[*it2].second.size();
|
|
|
|
items.emplace_back(view, data[*it2].first, act, std::move(data[*it2].second),
|
2021-02-16 16:05:39 +00:00
|
|
|
totalCount, (int)items.size(), numBins, theme);
|
2021-01-01 21:26:52 +00:00
|
|
|
act += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the items of the "other" group.
|
|
|
|
if (it != sorted.end()) {
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<dive *> otherDives;
|
|
|
|
otherDives.reserve(totalCount - act);
|
2021-01-01 21:26:52 +00:00
|
|
|
other.reserve(sorted.end() - it);
|
2021-01-20 13:36:59 +00:00
|
|
|
for (auto it2 = it; it2 != sorted.end(); ++it2) {
|
|
|
|
other.push_back({ data[*it2].first, (int)data[*it2].second.size() });
|
|
|
|
for (dive *d: data[*it2].second)
|
|
|
|
otherDives.push_back(d);
|
|
|
|
}
|
2021-01-01 21:26:52 +00:00
|
|
|
QString name = StatsTranslations::tr("other (%1 items)").arg(other.size());
|
2021-02-16 16:05:39 +00:00
|
|
|
items.emplace_back(view, name, act, std::move(otherDives), totalCount, (int)items.size(), numBins, theme);
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PieSeries::~PieSeries()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PieSeries::updatePositions()
|
|
|
|
{
|
2021-01-18 11:08:46 +00:00
|
|
|
QRectF plotRect = view.plotArea();
|
2021-01-01 21:26:52 +00:00
|
|
|
center = plotRect.center();
|
2021-01-18 11:08:46 +00:00
|
|
|
radius = ceil(std::min(plotRect.width(), plotRect.height()) * pieSize / 2.0);
|
|
|
|
QRectF rect(round(center.x() - radius), round(center.y() - radius), ceil(2.0 * radius), ceil(2.0 * radius));
|
|
|
|
item->resize(rect.size());
|
|
|
|
item->setPos(rect.topLeft());
|
|
|
|
int i = 0;
|
|
|
|
for (Item &segment: items) {
|
|
|
|
segment.updatePositions(center, radius);
|
2021-02-16 16:05:39 +00:00
|
|
|
segment.highlight(*item, i, i == highlighted, (int)items.size(), theme); // Draw segment
|
2021-01-18 11:08:46 +00:00
|
|
|
++i;
|
|
|
|
}
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> PieSeries::binNames()
|
|
|
|
{
|
|
|
|
std::vector<QString> res;
|
|
|
|
res.reserve(items.size());
|
|
|
|
for (Item &item: items)
|
|
|
|
res.push_back(item.name);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PieSeries::getItemUnderMouse(const QPointF &f) const
|
|
|
|
{
|
|
|
|
QPointF delta = f - center;
|
|
|
|
double len = sqrt(QPointF::dotProduct(delta, delta));
|
|
|
|
if (len > radius)
|
|
|
|
return -1;
|
|
|
|
delta /= len;
|
|
|
|
double angle = 0.25 - atan2(-delta.y(), delta.x()) / 2.0 / M_PI;
|
|
|
|
while (angle < 0.0)
|
|
|
|
angle += 1.0;
|
|
|
|
auto it = std::lower_bound(items.begin(), items.end(), angle,
|
|
|
|
[](const Item &item, double angle) { return item.angleTo < angle; });
|
|
|
|
if (it == items.end())
|
|
|
|
return -1; // Floating point rounding issues?
|
|
|
|
return it - items.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString makePercentageLine(int count, int total)
|
|
|
|
{
|
|
|
|
double percentage = count * 100.0 / total;
|
|
|
|
QString countString = QString("%L1").arg(count);
|
|
|
|
QString percentageString = QString("%L1%").arg(percentage, 0, 'f', 1);
|
|
|
|
QString totalString = QString("%L1").arg(total);
|
|
|
|
return StatsTranslations::tr("%1 (%2 of %3) dives").arg(countString, percentageString, totalString);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> PieSeries::makeInfo(int idx) const
|
|
|
|
{
|
|
|
|
std::vector<QString> res;
|
|
|
|
if (idx + 1 == (int)items.size() && !other.empty()) {
|
|
|
|
// This is the "other" bin. Format all these items and an overview item.
|
|
|
|
res.reserve(other.size() + 1);
|
|
|
|
res.push_back(QString("%1: %2").arg(StatsTranslations::tr("other"),
|
2021-01-20 13:36:59 +00:00
|
|
|
makePercentageLine((int)items[idx].dives.size(), totalCount)));
|
2021-01-01 21:26:52 +00:00
|
|
|
for (const OtherItem &item: other)
|
|
|
|
res.push_back(QString("%1: %2").arg(item.name,
|
2021-01-20 13:36:59 +00:00
|
|
|
makePercentageLine((int)item.count, totalCount)));
|
2021-01-01 21:26:52 +00:00
|
|
|
} else {
|
|
|
|
// A "normal" item.
|
|
|
|
res.reserve(2);
|
|
|
|
res.push_back(QStringLiteral("%1: %2").arg(categoryName, items[idx].name));
|
2021-01-20 13:36:59 +00:00
|
|
|
res.push_back(makePercentageLine((int)items[idx].dives.size(), totalCount));
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PieSeries::hover(QPointF pos)
|
|
|
|
{
|
|
|
|
int index = getItemUnderMouse(pos);
|
|
|
|
if (index == highlighted) {
|
|
|
|
if (information)
|
|
|
|
information->setPos(pos);
|
|
|
|
return index >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unhighlight();
|
|
|
|
highlighted = index;
|
|
|
|
|
|
|
|
// Highlight new item (if any)
|
|
|
|
if (highlighted >= 0 && highlighted < (int)items.size()) {
|
2021-02-16 16:05:39 +00:00
|
|
|
items[highlighted].highlight(*item, highlighted, true, (int)items.size(), theme);
|
2021-01-01 21:26:52 +00:00
|
|
|
if (!information)
|
2021-01-13 14:17:54 +00:00
|
|
|
information = view.createChartItem<InformationBox>();
|
2021-01-01 21:26:52 +00:00
|
|
|
information->setText(makeInfo(highlighted), pos);
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(true);
|
2021-01-01 21:26:52 +00:00
|
|
|
} else {
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(false);
|
2021-01-01 21:26:52 +00:00
|
|
|
}
|
|
|
|
return highlighted >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PieSeries::unhighlight()
|
|
|
|
{
|
|
|
|
if (highlighted >= 0 && highlighted < (int)items.size())
|
2021-02-16 16:05:39 +00:00
|
|
|
items[highlighted].highlight(*item, highlighted, false, (int)items.size(), theme);
|
2021-01-01 21:26:52 +00:00
|
|
|
highlighted = -1;
|
|
|
|
}
|
2021-01-17 12:34:18 +00:00
|
|
|
|
2021-02-10 11:15:14 +00:00
|
|
|
bool PieSeries::selectItemsUnderMouse(const QPointF &pos, SelectionModifier modifier)
|
2021-01-17 12:34:18 +00:00
|
|
|
{
|
|
|
|
int index = getItemUnderMouse(pos);
|
|
|
|
|
2021-02-10 19:59:34 +00:00
|
|
|
if (modifier.shift && index < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!modifier.shift || lastClicked < 0)
|
|
|
|
lastClicked = index;
|
|
|
|
|
2021-02-10 11:15:14 +00:00
|
|
|
std::vector<dive *> divesUnderMouse;
|
2021-02-10 19:59:34 +00:00
|
|
|
if (modifier.shift && lastClicked >= 0 && index >= 0) {
|
|
|
|
// Selecting a range in a pie plot is a bit special due to its cyclic nature.
|
|
|
|
// One way would be to always select the "shorter" path, but that would restrict the user.
|
|
|
|
// Thus, always select in the "positive" direction, i.e. clockwise.
|
|
|
|
int idx = lastClicked;
|
|
|
|
int last = index;
|
|
|
|
for (;;) {
|
|
|
|
const std::vector<dive *> &dives = items[idx].dives;
|
|
|
|
divesUnderMouse.insert(divesUnderMouse.end(), dives.begin(), dives.end());
|
|
|
|
if (idx == last)
|
|
|
|
break;
|
|
|
|
if (++idx >= (int)items.size())
|
|
|
|
idx = 0;
|
|
|
|
}
|
|
|
|
} else if (index >= 0) {
|
2021-02-10 11:15:14 +00:00
|
|
|
divesUnderMouse = items[index].dives;
|
2021-02-10 19:59:34 +00:00
|
|
|
}
|
2021-02-10 11:15:14 +00:00
|
|
|
processSelection(std::move(divesUnderMouse), modifier);
|
|
|
|
|
|
|
|
return index >= 0;
|
2021-01-17 12:34:18 +00:00
|
|
|
}
|
2021-02-07 19:48:43 +00:00
|
|
|
|
|
|
|
void PieSeries::divesSelected(const QVector<dive *> &)
|
|
|
|
{
|
|
|
|
for (Item &segment: items) {
|
|
|
|
bool selected = allDivesSelected(segment.dives);
|
|
|
|
if (segment.selected != selected) {
|
|
|
|
segment.selected = selected;
|
|
|
|
|
|
|
|
int idx = &segment - &items[0];
|
2021-02-16 16:05:39 +00:00
|
|
|
segment.highlight(*item, idx, idx == highlighted, (int)items.size(), theme);
|
2021-02-07 19:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|