2021-01-01 21:18:51 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "barseries.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:18:51 +00:00
|
|
|
#include "statstranslations.h"
|
2021-01-13 14:17:54 +00:00
|
|
|
#include "statsview.h"
|
2021-01-01 21:18:51 +00:00
|
|
|
#include "zvalues.h"
|
2021-01-17 12:34:18 +00:00
|
|
|
#include "core/selection.h"
|
2021-01-01 21:18:51 +00:00
|
|
|
|
|
|
|
#include <math.h> // for lrint()
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
|
|
// Constants that control the bar layout
|
|
|
|
static const double barWidth = 0.8; // 1.0 = full width of category
|
|
|
|
static const double subBarWidth = 0.9; // For grouped bar charts
|
2021-01-15 21:48:32 +00:00
|
|
|
static const double barBorderWidth = 1.0;
|
2021-01-01 21:18:51 +00:00
|
|
|
|
|
|
|
// Default constructor: invalid index.
|
|
|
|
BarSeries::Index::Index() : bar(-1), subitem(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BarSeries::Index::Index(int bar, int subitem) : bar(bar), subitem(subitem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BarSeries::Index::operator==(const Index &i2) const
|
|
|
|
{
|
|
|
|
return std::tie(bar, subitem) == std::tie(i2.bar, i2.subitem);
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries::BarSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:18:51 +00:00
|
|
|
bool horizontal, bool stacked, const QString &categoryName,
|
|
|
|
const StatsVariable *valueVariable, std::vector<QString> valueBinNames) :
|
2021-01-18 11:35:22 +00:00
|
|
|
StatsSeries(view, xAxis, yAxis),
|
2021-01-01 21:18:51 +00:00
|
|
|
horizontal(horizontal), stacked(stacked), categoryName(categoryName),
|
|
|
|
valueVariable(valueVariable), valueBinNames(std::move(valueBinNames))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries::BarSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:18:51 +00:00
|
|
|
bool horizontal, const QString &categoryName,
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<CountItem> items) :
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries(view, xAxis, yAxis, horizontal, false, categoryName, nullptr, std::vector<QString>())
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-20 13:36:59 +00:00
|
|
|
for (CountItem &item: items) {
|
2021-01-01 21:18:51 +00:00
|
|
|
StatsOperationResults res;
|
2021-01-20 13:36:59 +00:00
|
|
|
double value = (double)item.dives.size();
|
|
|
|
add_item(item.lowerBound, item.upperBound, makeSubItems({ value, std::move(item.dives), std::move(item.label) }),
|
2021-01-01 21:18:51 +00:00
|
|
|
item.binName, res, item.total, horizontal, stacked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries::BarSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:18:51 +00:00
|
|
|
bool horizontal, const QString &categoryName, const StatsVariable *valueVariable,
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<ValueItem> items) :
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries(view, xAxis, yAxis, horizontal, false, categoryName, valueVariable, std::vector<QString>())
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-20 13:36:59 +00:00
|
|
|
for (ValueItem &item: items) {
|
|
|
|
add_item(item.lowerBound, item.upperBound, makeSubItems({ item.value, std::move(item.res.dives), std::move(item.label) }),
|
2021-01-01 21:18:51 +00:00
|
|
|
item.binName, item.res, -1, horizontal, stacked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries::BarSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:18:51 +00:00
|
|
|
bool horizontal, bool stacked, const QString &categoryName, const StatsVariable *valueVariable,
|
|
|
|
std::vector<QString> valueBinNames,
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<MultiItem> items) :
|
2021-01-18 11:35:22 +00:00
|
|
|
BarSeries(view, xAxis, yAxis, horizontal, stacked, categoryName, valueVariable, std::move(valueBinNames))
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-20 13:36:59 +00:00
|
|
|
for (MultiItem &item: items) {
|
2021-01-01 21:18:51 +00:00
|
|
|
StatsOperationResults res;
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<SubItemDesc> subitems;
|
|
|
|
subitems.reserve(item.items.size());
|
2021-01-01 21:18:51 +00:00
|
|
|
int total = 0;
|
2021-01-20 13:36:59 +00:00
|
|
|
for (auto &[dives, label]: item.items) {
|
|
|
|
total += (int)dives.size();
|
|
|
|
subitems.push_back({ static_cast<double>(dives.size()), std::move(dives), std::move(label) });
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
2021-01-20 13:36:59 +00:00
|
|
|
add_item(item.lowerBound, item.upperBound, makeSubItems(std::move(subitems)),
|
2021-01-01 21:18:51 +00:00
|
|
|
item.binName, res, total, horizontal, stacked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BarSeries::~BarSeries()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
BarSeries::BarLabel::BarLabel(StatsView &view, const std::vector<QString> &labels, int bin_nr, int binCount) :
|
|
|
|
isOutside(false)
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-15 21:48:32 +00:00
|
|
|
QFont f; // make configurable
|
|
|
|
item = view.createChartItem<ChartTextItem>(ChartZValue::SeriesLabels, f, labels, true);
|
2021-01-19 15:09:56 +00:00
|
|
|
//highlight(false, bin_nr, binCount);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::BarLabel::setVisible(bool visible)
|
|
|
|
{
|
2021-01-17 17:29:54 +00:00
|
|
|
item->setVisible(visible);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 15:09:56 +00:00
|
|
|
void BarSeries::BarLabel::highlight(bool highlight, int bin_nr, int binCount, const QColor &background)
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-19 15:09:56 +00:00
|
|
|
// For labels that are on top of a bar, use the corresponding bar color
|
|
|
|
// as background. Rendering on a transparent background gives ugly artifacts.
|
|
|
|
item->setColor(highlight || isOutside ? darkLabelColor : labelColor(bin_nr, binCount),
|
|
|
|
isOutside ? Qt::transparent : background);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
void BarSeries::BarLabel::updatePosition(bool horizontal, bool center, const QRectF &rect,
|
2021-01-19 15:09:56 +00:00
|
|
|
int bin_nr, int binCount, const QColor &background)
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-15 21:48:32 +00:00
|
|
|
QSizeF itemSize = item->getRect().size();
|
2021-01-01 21:18:51 +00:00
|
|
|
if (!horizontal) {
|
2021-01-15 21:48:32 +00:00
|
|
|
if (itemSize.width() > rect.width()) {
|
2021-01-01 21:18:51 +00:00
|
|
|
setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPointF pos = rect.center();
|
2021-02-06 11:26:54 +00:00
|
|
|
pos.rx() -= itemSize.width() / 2.0;
|
2021-01-01 21:18:51 +00:00
|
|
|
|
|
|
|
// Heuristics: if the label fits nicely into the bar (bar height is at least twice the label height),
|
|
|
|
// then put the label in the middle of the bar. Otherwise, put it at the top of the bar.
|
2021-01-15 21:48:32 +00:00
|
|
|
isOutside = !center && rect.height() < 2.0 * itemSize.height();
|
2021-01-01 21:18:51 +00:00
|
|
|
if (isOutside) {
|
2021-01-15 21:48:32 +00:00
|
|
|
pos.ry() = rect.top() - (itemSize.height() + 2.0); // Leave two pixels(?) space
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-15 21:48:32 +00:00
|
|
|
if (itemSize.height() > rect.height()) {
|
2021-01-01 21:18:51 +00:00
|
|
|
setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-06 11:26:54 +00:00
|
|
|
pos.ry() -= itemSize.height() / 2.0;
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
2021-02-06 11:26:54 +00:00
|
|
|
item->setPos(roundPos(pos)); // Round to integer to avoid ugly artifacts.
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-15 21:48:32 +00:00
|
|
|
if (itemSize.height() > rect.height()) {
|
2021-01-01 21:18:51 +00:00
|
|
|
setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPointF pos = rect.center();
|
2021-02-06 11:26:54 +00:00
|
|
|
pos.ry() -= itemSize.height() / 2.0;
|
2021-01-01 21:18:51 +00:00
|
|
|
|
|
|
|
// Heuristics: if the label fits nicely into the bar (bar width is at least twice the label height),
|
|
|
|
// then put the label in the middle of the bar. Otherwise, put it to the right of the bar.
|
2021-01-15 21:48:32 +00:00
|
|
|
isOutside = !center && rect.width() < 2.0 * itemSize.width();
|
2021-01-01 21:18:51 +00:00
|
|
|
if (isOutside) {
|
2021-02-06 11:26:54 +00:00
|
|
|
pos.rx() = rect.right() + 2.0; // Leave two pixels(?) space
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-15 21:48:32 +00:00
|
|
|
if (itemSize.width() > rect.width()) {
|
2021-01-01 21:18:51 +00:00
|
|
|
setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-07 16:46:47 +00:00
|
|
|
pos.rx() -= itemSize.width() / 2.0;
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
2021-02-06 11:26:54 +00:00
|
|
|
item->setPos(roundPos(pos)); // Round to integer to avoid ugly artifacts.
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
setVisible(true);
|
|
|
|
// If label changed from inside to outside, or vice-versa, the color might change.
|
2021-01-19 15:09:56 +00:00
|
|
|
highlight(false, bin_nr, binCount, background);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
BarSeries::Item::Item(BarSeries *series, double lowerBound, double upperBound,
|
2021-01-01 21:18:51 +00:00
|
|
|
std::vector<SubItem> subitemsIn,
|
|
|
|
const QString &binName, const StatsOperationResults &res, int total,
|
|
|
|
bool horizontal, bool stacked, int binCount) :
|
|
|
|
lowerBound(lowerBound),
|
|
|
|
upperBound(upperBound),
|
|
|
|
subitems(std::move(subitemsIn)),
|
|
|
|
binName(binName),
|
|
|
|
res(res),
|
|
|
|
total(total)
|
|
|
|
{
|
2021-01-15 21:48:32 +00:00
|
|
|
for (SubItem &item: subitems)
|
2021-01-01 21:18:51 +00:00
|
|
|
item.highlight(false, binCount);
|
2021-01-05 11:11:46 +00:00
|
|
|
updatePosition(series, horizontal, stacked, binCount);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::Item::highlight(int subitem, bool highlight, int binCount)
|
|
|
|
{
|
|
|
|
if (subitem < 0 || subitem >= (int)subitems.size())
|
|
|
|
return;
|
|
|
|
subitems[subitem].highlight(highlight, binCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::SubItem::highlight(bool highlight, int binCount)
|
|
|
|
{
|
2021-01-19 15:09:56 +00:00
|
|
|
fill = highlight ? highlightedColor : binColor(bin_nr, binCount);
|
|
|
|
QColor border = highlight ? highlightedBorderColor : ::borderColor;
|
|
|
|
item->setColor(fill, border);
|
2021-01-01 21:18:51 +00:00
|
|
|
if (label)
|
2021-01-19 15:09:56 +00:00
|
|
|
label->highlight(highlight, bin_nr, binCount, fill);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
void BarSeries::Item::updatePosition(BarSeries *series, bool horizontal, bool stacked, int binCount)
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
|
|
|
if (subitems.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int num = stacked ? 1 : binCount;
|
|
|
|
|
|
|
|
// barWidth gives the total width of the rod or group of rods.
|
|
|
|
// subBarWidth gives the the width of each bar in the case of grouped bar charts.
|
|
|
|
// calculated the group width such that after applying the latter, the former is obtained.
|
|
|
|
double groupWidth = (upperBound - lowerBound) * barWidth * num / (num - 1 + subBarWidth);
|
|
|
|
double from = (lowerBound + upperBound - groupWidth) / 2.0;
|
|
|
|
double to = (lowerBound + upperBound + groupWidth) / 2.0;
|
|
|
|
|
|
|
|
double fullSubWidth = (to - from) / num; // width including gap
|
|
|
|
double subWidth = fullSubWidth * subBarWidth; // width without gap
|
|
|
|
for (SubItem &item: subitems) {
|
|
|
|
int idx = stacked ? 0 : item.bin_nr;
|
|
|
|
double center = (idx + 0.5) * fullSubWidth + from;
|
2021-01-05 11:11:46 +00:00
|
|
|
item.updatePosition(series, horizontal, stacked, center - subWidth / 2.0, center + subWidth / 2.0, binCount);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
2021-01-15 21:48:32 +00:00
|
|
|
rect = subitems[0].item->getRect();
|
2021-01-01 21:18:51 +00:00
|
|
|
for (auto it = std::next(subitems.begin()); it != subitems.end(); ++it)
|
2021-01-15 21:48:32 +00:00
|
|
|
rect = rect.united(it->item->getRect());
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
void BarSeries::SubItem::updatePosition(BarSeries *series, bool horizontal, bool stacked,
|
2021-01-01 21:18:51 +00:00
|
|
|
double from, double to, int binCount)
|
|
|
|
{
|
|
|
|
QPointF topLeft, bottomRight;
|
|
|
|
if (horizontal) {
|
2021-01-05 11:11:46 +00:00
|
|
|
topLeft = series->toScreen(QPointF(value_from, to));
|
|
|
|
bottomRight = series->toScreen(QPointF(value_to, from));
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-05 11:11:46 +00:00
|
|
|
topLeft = series->toScreen(QPointF(from, value_to));
|
|
|
|
bottomRight = series->toScreen(QPointF(to, value_from));
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
QRectF rect(topLeft, bottomRight);
|
|
|
|
item->setRect(rect);
|
|
|
|
if (label)
|
2021-01-19 15:09:56 +00:00
|
|
|
label->updatePosition(horizontal, stacked, rect, bin_nr, binCount, fill);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<BarSeries::SubItem> BarSeries::makeSubItems(std::vector<SubItemDesc> items) const
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
|
|
|
std::vector<SubItem> res;
|
2021-01-20 13:36:59 +00:00
|
|
|
res.reserve(items.size());
|
2021-01-01 21:18:51 +00:00
|
|
|
double from = 0.0;
|
|
|
|
int bin_nr = 0;
|
2021-01-20 13:36:59 +00:00
|
|
|
for (auto &[v, dives, label]: items) {
|
2021-01-01 21:18:51 +00:00
|
|
|
if (v > 0.0) {
|
2021-01-15 21:48:32 +00:00
|
|
|
res.push_back({ view.createChartItem<ChartBarItem>(ChartZValue::Series, barBorderWidth, horizontal),
|
2021-01-20 13:36:59 +00:00
|
|
|
std::move(dives),
|
2021-01-15 21:48:32 +00:00
|
|
|
{}, from, from + v, bin_nr });
|
2021-01-01 21:18:51 +00:00
|
|
|
if (!label.empty())
|
2021-01-15 21:48:32 +00:00
|
|
|
res.back().label = std::make_unique<BarLabel>(view, label, bin_nr, binCount());
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
if (stacked)
|
|
|
|
from += v;
|
|
|
|
++bin_nr;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:36:59 +00:00
|
|
|
std::vector<BarSeries::SubItem> BarSeries::makeSubItems(SubItemDesc item) const
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
2021-01-20 13:36:59 +00:00
|
|
|
return makeSubItems(std::vector<SubItemDesc>{ std::move(item) });
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BarSeries::binCount() const
|
|
|
|
{
|
|
|
|
return std::max(1, (int)valueBinNames.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::add_item(double lowerBound, double upperBound, std::vector<SubItem> subitems,
|
|
|
|
const QString &binName, const StatsOperationResults &res, int total, bool horizontal,
|
|
|
|
bool stacked)
|
|
|
|
{
|
|
|
|
// Don't add empty items, as that messes with the "find item under mouse" routine.
|
|
|
|
if (subitems.empty())
|
|
|
|
return;
|
2021-01-15 21:48:32 +00:00
|
|
|
items.emplace_back(this, lowerBound, upperBound, std::move(subitems), binName, res,
|
2021-01-01 21:18:51 +00:00
|
|
|
total, horizontal, stacked, binCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::updatePositions()
|
|
|
|
{
|
|
|
|
for (Item &item: items)
|
2021-01-05 11:11:46 +00:00
|
|
|
item.updatePosition(this, horizontal, stacked, binCount());
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attention: this supposes that items are sorted by position and no bar is inside another bar!
|
|
|
|
BarSeries::Index BarSeries::getItemUnderMouse(const QPointF &point) const
|
|
|
|
{
|
|
|
|
// Search the first item whose "end" position is greater than the cursor position.
|
|
|
|
auto it = horizontal ? std::lower_bound(items.begin(), items.end(), point.y(),
|
|
|
|
[] (const Item &item, double y) { return item.rect.top() > y; })
|
|
|
|
: std::lower_bound(items.begin(), items.end(), point.x(),
|
|
|
|
[] (const Item &item, double x) { return item.rect.right() < x; });
|
|
|
|
if (it == items.end() || !it->rect.contains(point))
|
|
|
|
return Index();
|
|
|
|
int subitem = it->getSubItemUnderMouse(point, horizontal, stacked);
|
|
|
|
return subitem >= 0 ? Index{(int)(it - items.begin()), subitem} : Index();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attention: this supposes that sub items are sorted by position and no subitem is inside another bar!
|
|
|
|
int BarSeries::Item::getSubItemUnderMouse(const QPointF &point, bool horizontal, bool stacked) const
|
|
|
|
{
|
|
|
|
// Search the first item whose "end" position is greater than the cursor position.
|
|
|
|
bool search_x = horizontal == stacked;
|
|
|
|
auto it = search_x ? std::lower_bound(subitems.begin(), subitems.end(), point.x(),
|
2021-01-15 21:48:32 +00:00
|
|
|
[] (const SubItem &item, double x) { return item.item->getRect().right() < x; })
|
2021-01-01 21:18:51 +00:00
|
|
|
: std::lower_bound(subitems.begin(), subitems.end(), point.y(),
|
2021-01-15 21:48:32 +00:00
|
|
|
[] (const SubItem &item, double y) { return item.item->getRect().top() > y; });
|
|
|
|
return it != subitems.end() && it->item->getRect().contains(point) ? it - subitems.begin() : -1;
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format information in a count-based bar chart.
|
|
|
|
// Essentially, the name of the bin and the number and percentages of dives.
|
|
|
|
static std::vector<QString> makeCountInfo(const QString &binName, const QString &axisName,
|
|
|
|
const QString &valueBinName, const QString &valueAxisName,
|
|
|
|
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);
|
|
|
|
std::vector<QString> res;
|
|
|
|
res.reserve(3);
|
|
|
|
res.push_back(QStringLiteral("%1: %2").arg(axisName, binName));
|
|
|
|
if (!valueAxisName.isEmpty())
|
|
|
|
res.push_back(QStringLiteral("%1: %2").arg(valueAxisName, valueBinName));
|
|
|
|
res.push_back(StatsTranslations::tr("%1 (%2 of %3) dives").arg(countString, percentageString, totalString));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format information in a value bar chart: the name of the bin and the value with unit.
|
|
|
|
static std::vector<QString> makeValueInfo(const QString &binName, const QString &axisName,
|
2021-01-20 13:36:59 +00:00
|
|
|
const StatsVariable &valueVariable, const StatsOperationResults &values,
|
|
|
|
int count)
|
2021-01-01 21:18:51 +00:00
|
|
|
{
|
|
|
|
QLocale loc;
|
|
|
|
int decimals = valueVariable.decimals();
|
|
|
|
QString unit = valueVariable.unitSymbol();
|
|
|
|
std::vector<StatsOperation> operations = valueVariable.supportedOperations();
|
|
|
|
std::vector<QString> res;
|
|
|
|
res.reserve(operations.size() + 3);
|
|
|
|
res.push_back(QStringLiteral("%1: %2").arg(axisName, binName));
|
2021-01-20 13:36:59 +00:00
|
|
|
res.push_back(QStringLiteral("%1: %2").arg(StatsTranslations::tr("Count"), loc.toString(count)));
|
2021-01-01 21:18:51 +00:00
|
|
|
res.push_back(QStringLiteral("%1: ").arg(valueVariable.name()));
|
|
|
|
for (StatsOperation op: operations) {
|
|
|
|
QString valueFormatted = loc.toString(values.get(op), 'f', decimals);
|
|
|
|
res.push_back(QString(" %1: %2 %3").arg(StatsVariable::operationName(op), valueFormatted, unit));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> BarSeries::makeInfo(const Item &item, int subitem_idx) const
|
|
|
|
{
|
2021-01-20 13:36:59 +00:00
|
|
|
if (item.subitems.empty())
|
|
|
|
return {};
|
2021-01-01 21:18:51 +00:00
|
|
|
if (!valueBinNames.empty() && valueVariable) {
|
|
|
|
if (subitem_idx < 0 || subitem_idx >= (int)item.subitems.size())
|
|
|
|
return {};
|
|
|
|
const SubItem &subitem = item.subitems[subitem_idx];
|
|
|
|
if (subitem.bin_nr < 0 || subitem.bin_nr >= (int)valueBinNames.size())
|
|
|
|
return {};
|
2021-01-20 13:36:59 +00:00
|
|
|
int count = (int)subitem.dives.size();
|
2021-01-01 21:18:51 +00:00
|
|
|
return makeCountInfo(item.binName, categoryName, valueBinNames[subitem.bin_nr],
|
|
|
|
valueVariable->name(), count, item.total);
|
|
|
|
} else if (valueVariable) {
|
2021-01-20 13:36:59 +00:00
|
|
|
int count = (int)item.subitems[0].dives.size();
|
|
|
|
return makeValueInfo(item.binName, categoryName, *valueVariable, item.res, count);
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-20 13:36:59 +00:00
|
|
|
int count = (int)item.subitems[0].dives.size();
|
|
|
|
return makeCountInfo(item.binName, categoryName, QString(), QString(), count, item.total);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Highlight item when hovering over item
|
|
|
|
bool BarSeries::hover(QPointF pos)
|
|
|
|
{
|
|
|
|
Index index = getItemUnderMouse(pos);
|
|
|
|
if (index == highlighted) {
|
|
|
|
if (information)
|
|
|
|
information->setPos(pos);
|
|
|
|
return index.bar >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unhighlight();
|
|
|
|
highlighted = index;
|
|
|
|
|
|
|
|
// Highlight new item (if any)
|
|
|
|
if (highlighted.bar >= 0 && highlighted.bar < (int)items.size()) {
|
|
|
|
Item &item = items[highlighted.bar];
|
|
|
|
item.highlight(index.subitem, true, binCount());
|
|
|
|
if (!information)
|
2021-01-13 14:17:54 +00:00
|
|
|
information = view.createChartItem<InformationBox>();
|
2021-01-01 21:18:51 +00:00
|
|
|
information->setText(makeInfo(item, highlighted.subitem), pos);
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(true);
|
2021-01-01 21:18:51 +00:00
|
|
|
} else {
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(false);
|
2021-01-01 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return highlighted.bar >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarSeries::unhighlight()
|
|
|
|
{
|
|
|
|
if (highlighted.bar >= 0 && highlighted.bar < (int)items.size())
|
|
|
|
items[highlighted.bar].highlight(highlighted.subitem, false, binCount());
|
|
|
|
highlighted = Index();
|
|
|
|
}
|
2021-01-17 12:34:18 +00:00
|
|
|
|
2021-01-31 20:30:51 +00:00
|
|
|
void BarSeries::selectItemsUnderMouse(const QPointF &pos, bool)
|
2021-01-17 12:34:18 +00:00
|
|
|
{
|
|
|
|
Index index = getItemUnderMouse(pos);
|
|
|
|
if (index.bar < 0)
|
|
|
|
return setSelection({}, nullptr);
|
|
|
|
|
|
|
|
const std::vector<dive *> &dives = items[index.bar].subitems[index.subitem].dives;
|
|
|
|
setSelection(dives, dives.empty() ? nullptr : dives.front());
|
|
|
|
}
|