2023-09-12 08:35:09 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "statistics/view/stack_linear_chart_view.h"
|
|
|
|
|
|
|
|
#include "data/data_statistics.h"
|
2023-09-15 09:41:14 +00:00
|
|
|
#include "statistics/point_details_widget.h"
|
2023-09-18 18:07:02 +00:00
|
|
|
#include "statistics/view/stack_chart_common.h"
|
2023-09-15 09:41:14 +00:00
|
|
|
#include "ui/effects/animation_value_f.h"
|
2023-09-12 08:35:09 +00:00
|
|
|
#include "ui/painter.h"
|
2023-09-12 14:24:46 +00:00
|
|
|
#include "ui/rect.h"
|
2023-09-12 08:35:09 +00:00
|
|
|
#include "styles/style_statistics.h"
|
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
#include <QtCore/QtMath>
|
|
|
|
|
2023-09-12 08:35:09 +00:00
|
|
|
namespace Statistic {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kAlphaDuration = float64(200);
|
2023-09-14 13:16:51 +00:00
|
|
|
constexpr auto kCircleSizeRatio = 0.42;
|
|
|
|
constexpr auto kMinTextScaleRatio = 0.3;
|
2023-09-14 14:14:15 +00:00
|
|
|
constexpr auto kPieAngleOffset = 90;
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
constexpr auto kRightTop = short(0);
|
|
|
|
constexpr auto kRightBottom = short(1);
|
|
|
|
constexpr auto kLeftBottom = short(2);
|
|
|
|
constexpr auto kLeftTop = short(3);
|
|
|
|
|
|
|
|
[[nodiscard]] short QuarterForPoint(const QRect &r, const QPointF &p) {
|
|
|
|
if (p.x() >= r.center().x() && p.y() <= r.center().y()) {
|
|
|
|
return kRightTop;
|
|
|
|
} else if (p.x() >= r.center().x() && p.y() >= r.center().y()) {
|
|
|
|
return kRightBottom;
|
|
|
|
} else if (p.x() < r.center().x() && p.y() >= r.center().y()) {
|
|
|
|
return kLeftBottom;
|
|
|
|
} else {
|
|
|
|
return kLeftTop;
|
|
|
|
}
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
inline float64 InterpolationRatio(float64 from, float64 to, float64 result) {
|
|
|
|
return (result - from) / (to - from);
|
|
|
|
};
|
|
|
|
|
2023-09-12 08:35:09 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
StackLinearChartView::StackLinearChartView() = default;
|
|
|
|
|
|
|
|
StackLinearChartView::~StackLinearChartView() = default;
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
void StackLinearChartView::paint(QPainter &p, const PaintContext &c) {
|
2023-09-25 15:14:28 +00:00
|
|
|
if (!_transition.progress && !c.footer) {
|
2023-09-25 12:20:43 +00:00
|
|
|
prepareZoom(c, TransitionStep::ZoomedOut);
|
|
|
|
}
|
2023-09-25 15:14:28 +00:00
|
|
|
if (_transition.pendingPrepareToZoomIn) {
|
|
|
|
_transition.pendingPrepareToZoomIn = false;
|
2023-09-25 12:20:43 +00:00
|
|
|
prepareZoom(c, TransitionStep::PrepareToZoomIn);
|
|
|
|
}
|
2023-09-12 14:24:46 +00:00
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
StackLinearChartView::paintChartOrZoomAnimation(p, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::prepareZoom(
|
|
|
|
const PaintContext &c,
|
|
|
|
TransitionStep step) {
|
|
|
|
if (step == TransitionStep::ZoomedOut) {
|
|
|
|
constexpr auto kOffset = float64(2);
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedOutXIndices = {
|
2023-09-25 12:20:43 +00:00
|
|
|
float64(std::max(0., c.xIndices.min - kOffset)),
|
|
|
|
float64(std::min(
|
|
|
|
float64(c.chartData.xPercentage.size() - 1),
|
|
|
|
c.xIndices.max + kOffset)),
|
|
|
|
};
|
|
|
|
} else if (step == TransitionStep::PrepareToZoomIn) {
|
|
|
|
const auto &[zoomedStart, zoomedEnd] =
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedOutXIndices;
|
|
|
|
_transition.lines = std::vector<Transition::TransitionLine>(
|
2023-09-19 10:02:50 +00:00
|
|
|
c.chartData.lines.size(),
|
2023-09-12 14:24:46 +00:00
|
|
|
Transition::TransitionLine());
|
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto xPercentageLimits = Limits{
|
2023-09-25 15:14:28 +00:00
|
|
|
c.chartData.xPercentage[_transition.zoomedOutXIndices.min],
|
|
|
|
c.chartData.xPercentage[_transition.zoomedOutXIndices.max],
|
2023-09-25 12:20:43 +00:00
|
|
|
};
|
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
for (auto j = 0; j < 2; j++) {
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto i = int((j == 1) ? zoomedEnd : zoomedStart);
|
2023-09-12 14:24:46 +00:00
|
|
|
auto stackOffset = 0;
|
|
|
|
auto sum = 0.;
|
|
|
|
auto drawingLinesCount = 0;
|
2023-09-19 10:02:50 +00:00
|
|
|
for (const auto &line : c.chartData.lines) {
|
2023-09-12 14:24:46 +00:00
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.y[i] > 0) {
|
|
|
|
sum += line.y[i] * alpha(line.id);
|
|
|
|
drawingLinesCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
for (auto k = 0; k < c.chartData.lines.size(); k++) {
|
2023-09-12 14:24:46 +00:00
|
|
|
auto &linePoint = (j
|
2023-09-25 15:14:28 +00:00
|
|
|
? _transition.lines[k].end
|
|
|
|
: _transition.lines[k].start);
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto &line = c.chartData.lines[k];
|
2023-09-12 14:24:46 +00:00
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto yPercentage = (drawingLinesCount == 1)
|
|
|
|
? (line.y[i] ? alpha(line.id) : 0)
|
|
|
|
: (sum ? (line.y[i] * alpha(line.id) / sum) : 0);
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto xPoint = c.rect.width()
|
2023-09-25 12:20:43 +00:00
|
|
|
* ((c.chartData.xPercentage[i] - xPercentageLimits.min)
|
|
|
|
/ (xPercentageLimits.max - xPercentageLimits.min));
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto height = yPercentage * c.rect.height();
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto yPoint = rect::bottom(c.rect)
|
|
|
|
- height
|
|
|
|
- stackOffset;
|
2023-09-12 14:24:46 +00:00
|
|
|
linePoint = { xPoint, yPoint };
|
|
|
|
stackOffset += height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
savePieTextParts(c);
|
2023-09-25 15:14:28 +00:00
|
|
|
applyParts(_transition.textParts);
|
2023-09-12 14:24:46 +00:00
|
|
|
}
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
void StackLinearChartView::applyParts(const std::vector<PiePartData> &parts) {
|
|
|
|
for (auto k = 0; k < parts.size(); k++) {
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.lines[k].angle = parts[k].stackedAngle;
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::saveZoomRange(const PaintContext &c) {
|
2023-09-26 00:06:48 +00:00
|
|
|
_transition.zoomedInRangeXIndices = FindStackXIndicesFromRawXPercentages(
|
|
|
|
c.chartData,
|
|
|
|
c.xPercentageLimits,
|
|
|
|
_transition.zoomedInLimitXIndices);
|
|
|
|
_transition.zoomedInRange = {
|
|
|
|
c.chartData.xPercentage[_transition.zoomedInRangeXIndices.min],
|
|
|
|
c.chartData.xPercentage[_transition.zoomedInRangeXIndices.max],
|
2023-09-25 12:20:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::savePieTextParts(const PaintContext &c) {
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.textParts = partsPercentage(
|
2023-09-25 12:20:43 +00:00
|
|
|
c.chartData,
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInRangeXIndices);
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto StackLinearChartView::partsPercentage(
|
|
|
|
const Data::StatisticalChart &chartData,
|
|
|
|
const Limits &xIndices) -> std::vector<PiePartData> {
|
|
|
|
auto result = std::vector<PiePartData>();
|
|
|
|
result.reserve(chartData.lines.size());
|
|
|
|
auto sums = std::vector<float64>();
|
|
|
|
sums.reserve(chartData.lines.size());
|
2023-09-25 13:20:28 +00:00
|
|
|
auto totalSum = 0.;
|
2023-09-25 12:20:43 +00:00
|
|
|
for (const auto &line : chartData.lines) {
|
|
|
|
auto sum = 0;
|
|
|
|
for (auto i = xIndices.min; i <= xIndices.max; i++) {
|
|
|
|
sum += line.y[i];
|
|
|
|
}
|
|
|
|
sum *= alpha(line.id);
|
|
|
|
totalSum += sum;
|
|
|
|
sums.push_back(sum);
|
|
|
|
}
|
|
|
|
auto stackedPercentage = 0.;
|
2023-09-25 13:20:28 +00:00
|
|
|
|
|
|
|
auto sumPercDiffs = 0.;
|
|
|
|
auto maxPercDiff = 0.;
|
|
|
|
auto minPercDiff = 0.;
|
|
|
|
auto maxPercDiffIndex = int(-1);
|
|
|
|
auto minPercDiffIndex = int(-1);
|
|
|
|
auto roundedPercentagesSum = 0.;
|
|
|
|
|
2023-09-26 01:11:18 +00:00
|
|
|
_pieHasSinglePart = false;
|
2023-09-25 12:20:43 +00:00
|
|
|
for (auto k = 0; k < sums.size(); k++) {
|
2023-09-25 13:20:28 +00:00
|
|
|
const auto rawPercentage = sums[k] / totalSum;
|
|
|
|
const auto rounded = 0.01 * std::round(rawPercentage * 100.);
|
|
|
|
roundedPercentagesSum += rounded;
|
|
|
|
const auto diff = rawPercentage - rounded;
|
|
|
|
sumPercDiffs += diff;
|
|
|
|
const auto diffAbs = std::abs(diff);
|
|
|
|
if (maxPercDiff < diffAbs) {
|
|
|
|
maxPercDiff = diffAbs;
|
|
|
|
maxPercDiffIndex = k;
|
|
|
|
}
|
|
|
|
if (minPercDiff < diffAbs) {
|
|
|
|
minPercDiff = diffAbs;
|
|
|
|
minPercDiffIndex = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
stackedPercentage += rounded;
|
|
|
|
result.push_back({ rounded, stackedPercentage * 360. - 180. });
|
2023-09-26 01:11:18 +00:00
|
|
|
_pieHasSinglePart |= (rounded == 1.);
|
2023-09-25 13:20:28 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto index = (roundedPercentagesSum > 1.)
|
|
|
|
? maxPercDiffIndex
|
|
|
|
: minPercDiffIndex;
|
|
|
|
if (index >= 0) {
|
|
|
|
result[index].roundedPercentage += sumPercDiffs;
|
|
|
|
const auto angleShrink = (sumPercDiffs) * 360.;
|
|
|
|
for (auto i = index; i < result.size(); i++) {
|
|
|
|
result[i].stackedAngle += angleShrink;
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
|
|
|
return result;
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
void StackLinearChartView::paintChartOrZoomAnimation(
|
2023-09-12 08:35:09 +00:00
|
|
|
QPainter &p,
|
2023-09-19 10:02:50 +00:00
|
|
|
const PaintContext &c) {
|
2023-09-25 15:14:28 +00:00
|
|
|
if (_transition.progress == 1.) {
|
2023-09-19 10:02:50 +00:00
|
|
|
if (c.footer) {
|
2023-09-19 14:35:39 +00:00
|
|
|
paintZoomedFooter(p, c);
|
2023-09-18 18:07:02 +00:00
|
|
|
} else {
|
2023-09-19 14:35:39 +00:00
|
|
|
paintZoomed(p, c);
|
2023-09-18 18:07:02 +00:00
|
|
|
}
|
2023-09-19 14:35:39 +00:00
|
|
|
return p.setOpacity(0.);
|
2023-09-14 13:16:51 +00:00
|
|
|
}
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto hasTransitionAnimation = _transition.progress && !c.footer;
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto &[localStart, localEnd] = c.footer
|
|
|
|
? Limits{ 0., float64(c.chartData.xPercentage.size() - 1) }
|
2023-09-25 15:14:28 +00:00
|
|
|
: _transition.zoomedOutXIndices;
|
2023-09-19 10:02:50 +00:00
|
|
|
_skipPoints = std::vector<bool>(c.chartData.lines.size(), false);
|
2023-09-12 14:24:46 +00:00
|
|
|
auto paths = std::vector<QPainterPath>(
|
2023-09-19 10:02:50 +00:00
|
|
|
c.chartData.lines.size(),
|
2023-09-12 14:24:46 +00:00
|
|
|
QPainterPath());
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto center = QPointF(c.rect.center());
|
2023-09-12 14:24:46 +00:00
|
|
|
|
|
|
|
const auto rotate = [&](float64 ang, const QPointF &p) {
|
|
|
|
return QTransform()
|
|
|
|
.translate(center.x(), center.y())
|
|
|
|
.rotate(ang)
|
|
|
|
.translate(-center.x(), -center.y())
|
|
|
|
.map(p);
|
|
|
|
};
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto xPercentageLimits = Limits{
|
|
|
|
c.chartData.xPercentage[localStart],
|
|
|
|
c.chartData.xPercentage[localEnd],
|
|
|
|
};
|
2023-09-12 14:24:46 +00:00
|
|
|
|
|
|
|
auto straightLineProgress = 0.;
|
|
|
|
auto hasEmptyPoint = false;
|
|
|
|
|
|
|
|
auto ovalPath = QPainterPath();
|
|
|
|
if (hasTransitionAnimation) {
|
|
|
|
constexpr auto kStraightLinePart = 0.6;
|
|
|
|
straightLineProgress = std::clamp(
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.progress / kStraightLinePart,
|
2023-09-12 14:24:46 +00:00
|
|
|
0.,
|
|
|
|
1.);
|
|
|
|
auto rectPath = QPainterPath();
|
2023-09-19 10:02:50 +00:00
|
|
|
rectPath.addRect(c.rect);
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto r = anim::interpolateF(
|
|
|
|
1.,
|
|
|
|
kCircleSizeRatio,
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.progress);
|
|
|
|
const auto per = anim::interpolateF(0., 100., _transition.progress);
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto side = (c.rect.width() / 2.) * r;
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto rectF = QRectF(
|
|
|
|
center - QPointF(side, side),
|
|
|
|
center + QPointF(side, side));
|
|
|
|
ovalPath.addRoundedRect(rectF, per, per, Qt::RelativeSize);
|
|
|
|
ovalPath = ovalPath.intersected(rectPath);
|
|
|
|
}
|
2023-09-12 08:35:09 +00:00
|
|
|
|
|
|
|
for (auto i = localStart; i <= localEnd; i++) {
|
|
|
|
auto stackOffset = 0.;
|
|
|
|
auto sum = 0.;
|
|
|
|
auto lastEnabled = int(0);
|
|
|
|
|
|
|
|
auto drawingLinesCount = int(0);
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
for (auto k = 0; k < c.chartData.lines.size(); k++) {
|
|
|
|
const auto &line = c.chartData.lines[k];
|
2023-09-12 08:35:09 +00:00
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.y[i] > 0) {
|
|
|
|
sum += line.y[i] * alpha(line.id);
|
|
|
|
drawingLinesCount++;
|
|
|
|
}
|
|
|
|
lastEnabled = k;
|
|
|
|
}
|
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
for (auto k = 0; k < c.chartData.lines.size(); k++) {
|
|
|
|
const auto &line = c.chartData.lines[k];
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto isLastLine = (k == lastEnabled);
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto &transitionLine = _transition.lines[k];
|
2023-09-12 08:35:09 +00:00
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto &y = line.y;
|
|
|
|
const auto lineAlpha = alpha(line.id);
|
|
|
|
|
|
|
|
auto &chartPath = paths[k];
|
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto yPercentage = (drawingLinesCount == 1)
|
|
|
|
? float64(y[i] ? lineAlpha : 0.)
|
|
|
|
: float64(sum ? (y[i] * lineAlpha / sum) : 0.);
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto xPoint = c.rect.width()
|
2023-09-25 12:20:43 +00:00
|
|
|
* ((c.chartData.xPercentage[i] - xPercentageLimits.min)
|
|
|
|
/ (xPercentageLimits.max - xPercentageLimits.min));
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
if (!yPercentage && isLastLine) {
|
|
|
|
hasEmptyPoint = true;
|
|
|
|
}
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto height = yPercentage * c.rect.height();
|
|
|
|
const auto yPoint = rect::bottom(c.rect) - height - stackOffset;
|
2023-09-12 14:24:46 +00:00
|
|
|
// startFromY[k] = yPoint;
|
|
|
|
|
|
|
|
auto angle = 0.;
|
|
|
|
auto resultPoint = QPointF(xPoint, yPoint);
|
2023-09-19 10:02:50 +00:00
|
|
|
auto pointZero = QPointF(xPoint, c.rect.y() + c.rect.height());
|
2023-09-12 14:24:46 +00:00
|
|
|
// if (i == localEnd) {
|
|
|
|
// endXPoint = xPoint;
|
|
|
|
// } else if (i == localStart) {
|
|
|
|
// startXPoint = xPoint;
|
|
|
|
// }
|
|
|
|
if (hasTransitionAnimation && !isLastLine) {
|
|
|
|
const auto point1 = (resultPoint.x() < center.x())
|
|
|
|
? transitionLine.start
|
|
|
|
: transitionLine.end;
|
|
|
|
|
|
|
|
const auto diff = center - point1;
|
|
|
|
const auto yTo = point1.y()
|
|
|
|
+ diff.y() * (resultPoint.x() - point1.x()) / diff.x();
|
|
|
|
const auto yToResult = yTo * straightLineProgress;
|
|
|
|
const auto revProgress = (1. - straightLineProgress);
|
|
|
|
|
|
|
|
resultPoint.setY(resultPoint.y() * revProgress + yToResult);
|
|
|
|
pointZero.setY(pointZero.y() * revProgress + yToResult);
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto angleK = diff.y() / float64(diff.x());
|
|
|
|
angle = (angleK > 0)
|
|
|
|
? (-std::atan(angleK)) * (180. / M_PI)
|
|
|
|
: (std::atan(std::abs(angleK))) * (180. / M_PI);
|
|
|
|
angle -= 90;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resultPoint.x() >= center.x()) {
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto resultAngle = _transition.progress * angle;
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto rotated = rotate(resultAngle, resultPoint);
|
|
|
|
resultPoint = QPointF(
|
|
|
|
std::max(rotated.x(), center.x()),
|
|
|
|
rotated.y());
|
2023-09-12 08:35:09 +00:00
|
|
|
|
2023-09-12 14:24:46 +00:00
|
|
|
pointZero = QPointF(
|
|
|
|
std::max(pointZero.x(), center.x()),
|
|
|
|
rotate(resultAngle, pointZero).y());
|
|
|
|
} else {
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto &xLimits = xPercentageLimits;
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto isNextXPointAfterCenter = false
|
2023-09-19 10:02:50 +00:00
|
|
|
|| center.x() < (c.rect.width() * ((i == localEnd)
|
2023-09-12 14:24:46 +00:00
|
|
|
? 1.
|
2023-09-19 10:02:50 +00:00
|
|
|
: ((c.chartData.xPercentage[i + 1] - xLimits.min)
|
2023-09-12 14:24:46 +00:00
|
|
|
/ (xLimits.max - xLimits.min))));
|
|
|
|
if (isNextXPointAfterCenter) {
|
|
|
|
pointZero = resultPoint = QPointF()
|
|
|
|
+ center * straightLineProgress
|
|
|
|
+ resultPoint * revProgress;
|
|
|
|
} else {
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto resultAngle = _transition.progress * angle
|
|
|
|
+ _transition.progress * transitionLine.angle;
|
2023-09-12 14:24:46 +00:00
|
|
|
resultPoint = rotate(resultAngle, resultPoint);
|
|
|
|
pointZero = rotate(resultAngle, pointZero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 08:35:09 +00:00
|
|
|
|
|
|
|
if (i == localStart) {
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto bottomLeft = QPointF(c.rect.x(), rect::bottom(c.rect));
|
2023-09-12 14:24:46 +00:00
|
|
|
const auto local = (hasTransitionAnimation && !isLastLine)
|
|
|
|
? rotate(
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.progress * angle
|
|
|
|
+ _transition.progress * transitionLine.angle,
|
2023-09-12 14:24:46 +00:00
|
|
|
bottomLeft - QPointF(center.x(), 0))
|
|
|
|
: bottomLeft;
|
|
|
|
chartPath.setFillRule(Qt::WindingFill);
|
|
|
|
chartPath.moveTo(local);
|
|
|
|
_skipPoints[k] = false;
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto yRatio = 1. - (isLastLine ? _transition.progress : 0.);
|
2023-09-12 14:24:46 +00:00
|
|
|
if ((!yPercentage)
|
2023-09-12 08:35:09 +00:00
|
|
|
&& (i > 0 && (y[i - 1] == 0))
|
2023-09-12 14:24:46 +00:00
|
|
|
&& (i < localEnd && (y[i + 1] == 0))
|
|
|
|
&& (!hasTransitionAnimation)) {
|
|
|
|
if (!_skipPoints[k]) {
|
|
|
|
chartPath.lineTo(pointZero.x(), pointZero.y() * yRatio);
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
2023-09-12 14:24:46 +00:00
|
|
|
_skipPoints[k] = true;
|
2023-09-12 08:35:09 +00:00
|
|
|
} else {
|
2023-09-12 14:24:46 +00:00
|
|
|
if (_skipPoints[k]) {
|
|
|
|
chartPath.lineTo(pointZero.x(), pointZero.y() * yRatio);
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
2023-09-12 14:24:46 +00:00
|
|
|
chartPath.lineTo(resultPoint.x(), resultPoint.y() * yRatio);
|
|
|
|
_skipPoints[k] = false;
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == localEnd) {
|
2023-09-12 14:24:46 +00:00
|
|
|
if (hasTransitionAnimation && !isLastLine) {
|
|
|
|
{
|
|
|
|
const auto diff = center - transitionLine.start;
|
|
|
|
const auto angleK = diff.y() / diff.x();
|
|
|
|
angle = (angleK > 0)
|
|
|
|
? ((-std::atan(angleK)) * (180. / M_PI))
|
|
|
|
: ((std::atan(std::abs(angleK))) * (180. / M_PI));
|
|
|
|
angle -= 90;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto local = rotate(
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.progress * angle
|
|
|
|
+ _transition.progress * transitionLine.angle,
|
2023-09-12 14:24:46 +00:00
|
|
|
transitionLine.start);
|
|
|
|
|
|
|
|
const auto ending = true
|
|
|
|
&& (std::abs(resultPoint.x() - local.x()) < 0.001)
|
|
|
|
&& ((local.y() < center.y()
|
|
|
|
&& resultPoint.y() < center.y())
|
|
|
|
|| (local.y() > center.y()
|
|
|
|
&& resultPoint.y() > center.y()));
|
|
|
|
const auto endQuarter = (!ending)
|
2023-09-19 10:02:50 +00:00
|
|
|
? QuarterForPoint(c.rect, resultPoint)
|
2023-09-12 14:24:46 +00:00
|
|
|
: kRightTop;
|
|
|
|
const auto startQuarter = (!ending)
|
2023-09-19 10:02:50 +00:00
|
|
|
? QuarterForPoint(c.rect, local)
|
2023-09-12 14:24:46 +00:00
|
|
|
: (transitionLine.angle == -180.)
|
|
|
|
? kRightTop
|
|
|
|
: kLeftTop;
|
|
|
|
|
|
|
|
for (auto q = endQuarter; q <= startQuarter; q++) {
|
|
|
|
chartPath.lineTo(
|
|
|
|
(q == kLeftTop || q == kLeftBottom)
|
2023-09-19 10:02:50 +00:00
|
|
|
? c.rect.x()
|
|
|
|
: rect::right(c.rect),
|
2023-09-12 14:24:46 +00:00
|
|
|
(q == kLeftTop || q == kRightTop)
|
2023-09-19 10:02:50 +00:00
|
|
|
? c.rect.y()
|
|
|
|
: rect::right(c.rect));
|
2023-09-12 14:24:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-19 10:02:50 +00:00
|
|
|
chartPath.lineTo(rect::right(c.rect), rect::bottom(c.rect));
|
2023-09-12 14:24:46 +00:00
|
|
|
}
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stackOffset += height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hq = PainterHighQualityEnabler(p);
|
|
|
|
|
2023-09-19 14:35:39 +00:00
|
|
|
p.fillRect(c.rect + QMargins(0, 0, 0, st::lineWidth), st::boxBg);
|
2023-09-12 14:24:46 +00:00
|
|
|
if (!ovalPath.isEmpty()) {
|
|
|
|
p.setClipPath(ovalPath);
|
|
|
|
}
|
|
|
|
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto opacity = c.footer ? (1. - _transition.progress) : 1.;
|
2023-09-19 10:02:50 +00:00
|
|
|
for (auto k = int(c.chartData.lines.size() - 1); k >= 0; k--) {
|
2023-09-12 08:35:09 +00:00
|
|
|
if (paths[k].isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto &line = c.chartData.lines[k];
|
2023-09-18 18:07:02 +00:00
|
|
|
p.setOpacity(alpha(line.id) * opacity);
|
2023-09-12 08:35:09 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.fillPath(paths[k], line.color);
|
|
|
|
}
|
2023-09-18 18:07:02 +00:00
|
|
|
p.setOpacity(opacity);
|
2023-09-19 10:02:50 +00:00
|
|
|
if (!c.footer) {
|
2023-09-14 14:14:15 +00:00
|
|
|
constexpr auto kAlphaTextPart = 0.6;
|
|
|
|
const auto progress = std::clamp(
|
2023-09-25 15:14:28 +00:00
|
|
|
(_transition.progress - kAlphaTextPart) / (1. - kAlphaTextPart),
|
2023-09-14 14:14:15 +00:00
|
|
|
0.,
|
|
|
|
1.);
|
|
|
|
if (progress > 0) {
|
|
|
|
auto o = ScopedPainterOpacity(p, progress);
|
2023-09-19 10:02:50 +00:00
|
|
|
paintPieText(p, c);
|
2023-09-14 14:14:15 +00:00
|
|
|
}
|
2023-09-25 23:42:40 +00:00
|
|
|
} else if (_transition.progress) {
|
2023-09-19 10:02:50 +00:00
|
|
|
paintZoomedFooter(p, c);
|
2023-09-14 14:14:15 +00:00
|
|
|
}
|
2023-09-12 14:24:46 +00:00
|
|
|
|
|
|
|
// Fix ugly outline.
|
2023-09-25 15:14:28 +00:00
|
|
|
if (!c.footer || !_transition.progress) {
|
2023-09-12 14:24:46 +00:00
|
|
|
p.setBrush(Qt::transparent);
|
|
|
|
p.setPen(st::boxBg);
|
|
|
|
p.drawPath(ovalPath);
|
|
|
|
}
|
2023-09-19 14:35:39 +00:00
|
|
|
|
|
|
|
if (!ovalPath.isEmpty()) {
|
|
|
|
p.setClipRect(c.rect, Qt::NoClip);
|
|
|
|
}
|
2023-09-25 15:14:28 +00:00
|
|
|
p.setOpacity(1. - _transition.progress);
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 13:16:51 +00:00
|
|
|
void StackLinearChartView::paintZoomed(QPainter &p, const PaintContext &c) {
|
|
|
|
if (c.footer) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-25 12:20:43 +00:00
|
|
|
|
|
|
|
saveZoomRange(c);
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto parts = partsPercentage(
|
|
|
|
c.chartData,
|
|
|
|
_transition.zoomedInRangeXIndices);
|
2023-09-25 12:20:43 +00:00
|
|
|
applyParts(parts);
|
|
|
|
|
2023-09-19 14:35:39 +00:00
|
|
|
p.fillRect(c.rect + QMargins(0, 0, 0, st::lineWidth), st::boxBg);
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto center = QPointF(c.rect.center());
|
|
|
|
const auto side = (c.rect.width() / 2.) * kCircleSizeRatio;
|
|
|
|
const auto rectF = QRectF(
|
|
|
|
center - QPointF(side, side),
|
|
|
|
center + QPointF(side, side));
|
|
|
|
|
|
|
|
auto hq = PainterHighQualityEnabler(p);
|
2023-09-15 09:41:14 +00:00
|
|
|
auto selectedLineIndex = -1;
|
2023-09-15 14:05:45 +00:00
|
|
|
const auto skipTranslation = skipSelectedTranslation();
|
2023-09-14 14:14:15 +00:00
|
|
|
for (auto k = 0; k < c.chartData.lines.size(); k++) {
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto previous = k
|
2023-09-25 12:20:43 +00:00
|
|
|
? parts[k - 1].stackedAngle
|
2023-09-14 13:16:51 +00:00
|
|
|
: -180;
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto now = parts[k].stackedAngle;
|
2023-09-14 13:16:51 +00:00
|
|
|
|
2023-09-15 09:41:14 +00:00
|
|
|
const auto &line = c.chartData.lines[k];
|
|
|
|
p.setBrush(line.color);
|
2023-09-14 13:16:51 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
2023-09-15 01:46:28 +00:00
|
|
|
const auto textAngle = (previous + kPieAngleOffset)
|
|
|
|
+ (now - previous) / 2.;
|
2023-09-15 14:05:45 +00:00
|
|
|
const auto partOffset = skipTranslation
|
|
|
|
? QPointF()
|
|
|
|
: _piePartController.offset(line.id, textAngle);
|
2023-09-15 01:46:28 +00:00
|
|
|
p.translate(partOffset);
|
2023-09-14 14:14:15 +00:00
|
|
|
p.drawPie(
|
|
|
|
rectF,
|
|
|
|
-(previous + kPieAngleOffset) * 16,
|
|
|
|
-(now - previous) * 16);
|
2023-09-15 01:46:28 +00:00
|
|
|
p.translate(-partOffset);
|
2023-09-15 09:41:14 +00:00
|
|
|
if (_piePartController.selected() == line.id) {
|
|
|
|
selectedLineIndex = k;
|
|
|
|
}
|
2023-09-14 13:16:51 +00:00
|
|
|
}
|
2023-09-19 14:37:20 +00:00
|
|
|
if (_piePartController.isFinished()) {
|
|
|
|
_piePartAnimation.stop();
|
|
|
|
}
|
2023-09-14 14:14:15 +00:00
|
|
|
paintPieText(p, c);
|
2023-09-15 09:41:14 +00:00
|
|
|
|
|
|
|
if (selectedLineIndex >= 0) {
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto &[zoomedStart, zoomedEnd] =
|
|
|
|
_transition.zoomedInRangeXIndices;
|
2023-09-15 09:41:14 +00:00
|
|
|
const auto &line = c.chartData.lines[selectedLineIndex];
|
|
|
|
auto sum = 0;
|
2023-09-25 12:20:43 +00:00
|
|
|
for (auto i = zoomedStart; i <= zoomedEnd; i++) {
|
2023-09-15 09:41:14 +00:00
|
|
|
sum += line.y[i];
|
|
|
|
}
|
|
|
|
sum *= alpha(line.id);
|
|
|
|
if (sum > 0) {
|
|
|
|
PaintDetails(p, line, sum, c.rect);
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 14:14:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 18:07:02 +00:00
|
|
|
void StackLinearChartView::paintZoomedFooter(
|
|
|
|
QPainter &p,
|
|
|
|
const PaintContext &c) {
|
|
|
|
if (!c.footer) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-25 15:14:28 +00:00
|
|
|
auto o = ScopedPainterOpacity(p, _transition.progress);
|
2023-09-18 18:07:02 +00:00
|
|
|
auto hq = PainterHighQualityEnabler(p);
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto &[zoomedStart, zoomedEnd] = _transition.zoomedInLimitXIndices;
|
2023-09-25 23:42:40 +00:00
|
|
|
const auto sideW = st::statisticsChartFooterSideWidth;
|
|
|
|
const auto width = c.rect.width() - sideW * 2.;
|
|
|
|
const auto leftStart = c.rect.x() + sideW;
|
|
|
|
const auto &xPercentage = c.chartData.xPercentage;
|
|
|
|
auto previousX = leftStart;
|
2023-09-26 13:43:35 +00:00
|
|
|
// Read FindStackXIndicesFromRawXPercentages.
|
2023-09-25 23:42:40 +00:00
|
|
|
const auto offset = (xPercentage[zoomedEnd] == 1.) ? 0 : 1;
|
2023-09-25 12:20:43 +00:00
|
|
|
for (auto i = zoomedStart; i <= zoomedEnd; i++) {
|
2023-09-18 18:07:02 +00:00
|
|
|
auto sum = 0.;
|
|
|
|
auto lastEnabledId = int(0);
|
|
|
|
for (const auto &line : c.chartData.lines) {
|
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sum += line.y[i] * alpha(line.id);
|
|
|
|
lastEnabledId = line.id;
|
|
|
|
}
|
|
|
|
|
2023-09-25 23:42:40 +00:00
|
|
|
const auto columnMargins = QMarginsF(
|
|
|
|
(i == zoomedStart) ? sideW : 0,
|
|
|
|
0,
|
|
|
|
(i == zoomedEnd - offset) ? sideW : 0,
|
|
|
|
0);
|
|
|
|
|
|
|
|
const auto next = std::clamp(i + offset, zoomedStart, zoomedEnd);
|
|
|
|
const auto xPointPercentage =
|
|
|
|
(xPercentage[next] - xPercentage[zoomedStart])
|
|
|
|
/ (xPercentage[zoomedEnd] - xPercentage[zoomedStart]);
|
|
|
|
const auto xPoint = leftStart + width * xPointPercentage;
|
|
|
|
|
2023-09-18 18:07:02 +00:00
|
|
|
auto stack = 0.;
|
|
|
|
for (auto k = int(c.chartData.lines.size() - 1); k >= 0; k--) {
|
|
|
|
const auto &line = c.chartData.lines[k];
|
|
|
|
if (!isEnabled(line.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto visibleHeight = c.rect.height() * (line.y[i] / sum);
|
|
|
|
const auto height = (line.id == lastEnabledId)
|
|
|
|
? c.rect.height()
|
|
|
|
: visibleHeight;
|
|
|
|
|
2023-09-25 23:42:40 +00:00
|
|
|
const auto column = columnMargins + QRectF(
|
|
|
|
previousX,
|
2023-09-18 18:07:02 +00:00
|
|
|
stack,
|
2023-09-25 23:42:40 +00:00
|
|
|
xPoint - previousX,
|
2023-09-18 18:07:02 +00:00
|
|
|
height);
|
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.fillRect(column, line.color);
|
|
|
|
stack += visibleHeight;
|
|
|
|
}
|
2023-09-25 23:42:40 +00:00
|
|
|
previousX = xPoint;
|
2023-09-18 18:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:14:15 +00:00
|
|
|
void StackLinearChartView::paintPieText(QPainter &p, const PaintContext &c) {
|
2023-09-25 15:31:48 +00:00
|
|
|
constexpr auto kMinPercentage = 0.03;
|
2023-09-25 15:14:28 +00:00
|
|
|
if (_transition.progress == 1.) {
|
2023-09-25 12:20:43 +00:00
|
|
|
savePieTextParts(c);
|
|
|
|
}
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto &parts = _transition.textParts;
|
2023-09-25 12:20:43 +00:00
|
|
|
|
2023-09-14 14:14:15 +00:00
|
|
|
const auto center = QPointF(c.rect.center());
|
|
|
|
const auto side = (c.rect.width() / 2.) * kCircleSizeRatio;
|
|
|
|
const auto rectF = QRectF(
|
|
|
|
center - QPointF(side, side),
|
|
|
|
center + QPointF(side, side));
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto &font = st::statisticsPieChartFont;
|
|
|
|
const auto maxScale = side / (font->height * 2);
|
|
|
|
const auto minScale = maxScale * kMinTextScaleRatio;
|
|
|
|
p.setBrush(Qt::NoBrush);
|
|
|
|
p.setPen(st::premiumButtonFg);
|
|
|
|
p.setFont(font);
|
2023-09-14 14:14:15 +00:00
|
|
|
const auto opacity = p.opacity();
|
2023-09-15 14:05:45 +00:00
|
|
|
const auto skipTranslation = skipSelectedTranslation();
|
2023-09-14 14:14:15 +00:00
|
|
|
for (auto k = 0; k < c.chartData.lines.size(); k++) {
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto previous = k
|
2023-09-25 12:20:43 +00:00
|
|
|
? parts[k - 1].stackedAngle
|
2023-09-14 13:16:51 +00:00
|
|
|
: -180;
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto now = parts[k].stackedAngle;
|
|
|
|
const auto percentage = parts[k].roundedPercentage;
|
2023-09-25 15:31:48 +00:00
|
|
|
if (percentage <= kMinPercentage) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-14 13:16:51 +00:00
|
|
|
|
|
|
|
const auto rText = side * std::sqrt(1. - percentage);
|
2023-09-14 14:14:15 +00:00
|
|
|
const auto textAngle = (previous + kPieAngleOffset)
|
|
|
|
+ (now - previous) / 2.;
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto textRadians = textAngle * M_PI / 180.;
|
|
|
|
const auto scale = (minScale) + percentage * (maxScale - minScale);
|
2023-09-25 13:20:28 +00:00
|
|
|
const auto text = QString::number(int(percentage * 100)) + u"%"_q;
|
2023-09-14 13:16:51 +00:00
|
|
|
const auto textW = font->width(text);
|
|
|
|
const auto textH = font->height;
|
|
|
|
const auto textXShift = textW / 2.;
|
|
|
|
const auto textYShift = textW / 2.;
|
|
|
|
const auto textRectCenter = rectF.center() + QPointF(
|
|
|
|
(rText - textXShift * (1. - scale)) * std::cos(textRadians),
|
|
|
|
(rText - textYShift * (1. - scale)) * std::sin(textRadians));
|
|
|
|
const auto textRect = QRectF(
|
|
|
|
textRectCenter - QPointF(textXShift, textYShift),
|
|
|
|
textRectCenter + QPointF(textXShift, textYShift));
|
2023-09-15 14:05:45 +00:00
|
|
|
const auto partOffset = skipTranslation
|
|
|
|
? QPointF()
|
|
|
|
: _piePartController.offset(c.chartData.lines[k].id, textAngle);
|
2023-09-14 13:16:51 +00:00
|
|
|
p.setTransform(
|
|
|
|
QTransform()
|
2023-09-15 01:46:28 +00:00
|
|
|
.translate(
|
|
|
|
textRectCenter.x() + partOffset.x(),
|
|
|
|
textRectCenter.y() + partOffset.y())
|
2023-09-14 13:16:51 +00:00
|
|
|
.scale(scale, scale)
|
|
|
|
.translate(-textRectCenter.x(), -textRectCenter.y()));
|
2023-09-14 14:14:15 +00:00
|
|
|
p.setOpacity(opacity * alpha(c.chartData.lines[k].id));
|
2023-09-14 13:16:51 +00:00
|
|
|
p.drawText(textRect, text, style::al_center);
|
|
|
|
}
|
|
|
|
p.resetTransform();
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:46:28 +00:00
|
|
|
void StackLinearChartView::setUpdateCallback(Fn<void()> callback) {
|
|
|
|
if (callback) {
|
|
|
|
_piePartAnimation.init([=] { callback(); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackLinearChartView::PiePartController::set(int id) {
|
|
|
|
if (_selected != id) {
|
|
|
|
update(_selected);
|
|
|
|
_selected = id;
|
|
|
|
update(_selected);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::PiePartController::update(int id) {
|
|
|
|
if (id >= 0) {
|
|
|
|
const auto was = _startedAt[id];
|
|
|
|
const auto p = (crl::now() - was) / st::slideWrapDuration;
|
|
|
|
const auto progress = ((p > 0) && (p < 1)) ? (1. - p) : 0.;
|
|
|
|
_startedAt[id] = crl::now() - (st::slideWrapDuration * progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:37:20 +00:00
|
|
|
float64 StackLinearChartView::PiePartController::progress(int id) const {
|
2023-09-15 01:46:28 +00:00
|
|
|
const auto it = _startedAt.find(id);
|
|
|
|
if (it == end(_startedAt)) {
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
const auto at = it->second;
|
|
|
|
const auto show = (_selected == id);
|
|
|
|
const auto progress = std::clamp(
|
|
|
|
(crl::now() - at) / float64(st::slideWrapDuration),
|
|
|
|
0.,
|
|
|
|
1.);
|
|
|
|
return std::clamp(show ? progress : (1. - progress), 0., 1.);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF StackLinearChartView::PiePartController::offset(
|
|
|
|
LineId id,
|
2023-09-19 14:37:20 +00:00
|
|
|
float64 angle) const {
|
2023-09-15 01:46:28 +00:00
|
|
|
const auto offset = st::statisticsPieChartPartOffset * progress(id);
|
|
|
|
const auto radians = angle * M_PI / 180.;
|
|
|
|
return { std::cos(radians) * offset, std::sin(radians) * offset };
|
|
|
|
}
|
|
|
|
|
2023-09-15 09:41:14 +00:00
|
|
|
auto StackLinearChartView::PiePartController::selected() const -> LineId {
|
|
|
|
return _selected;
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:37:20 +00:00
|
|
|
bool StackLinearChartView::PiePartController::isFinished() const {
|
|
|
|
for (const auto &[id, _] : _startedAt) {
|
|
|
|
const auto p = progress(id);
|
|
|
|
if (p > 0 && p < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:46:28 +00:00
|
|
|
void StackLinearChartView::handleMouseMove(
|
|
|
|
const Data::StatisticalChart &chartData,
|
|
|
|
const QPoint ¢er,
|
|
|
|
const QPoint &p) {
|
2023-09-25 15:14:28 +00:00
|
|
|
if (_transition.progress < 1) {
|
2023-09-15 01:46:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto theta = std::atan2(center.y() - p.y(), (center.x() - p.x()));
|
|
|
|
const auto angle = [&] {
|
|
|
|
const auto a = theta * (180. / M_PI) + 90.;
|
|
|
|
return (a > 180.) ? (a - 360.) : a;
|
|
|
|
}();
|
|
|
|
for (auto k = 0; k < chartData.lines.size(); k++) {
|
|
|
|
const auto previous = k
|
2023-09-25 15:14:28 +00:00
|
|
|
? _transition.lines[k - 1].angle
|
2023-09-15 01:46:28 +00:00
|
|
|
: -180;
|
2023-09-25 15:14:28 +00:00
|
|
|
const auto now = _transition.lines[k].angle;
|
2023-09-15 01:46:28 +00:00
|
|
|
if (angle > previous && angle <= now) {
|
2023-09-15 09:41:14 +00:00
|
|
|
const auto id = p.isNull()
|
|
|
|
? -1
|
|
|
|
: chartData.lines[k].id;
|
|
|
|
if (_piePartController.set(id)) {
|
2023-09-15 01:46:28 +00:00
|
|
|
if (!_piePartAnimation.animating()) {
|
|
|
|
_piePartAnimation.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:05:45 +00:00
|
|
|
bool StackLinearChartView::skipSelectedTranslation() const {
|
2023-09-26 01:11:18 +00:00
|
|
|
return _pieHasSinglePart
|
|
|
|
|| (_entries.size() == (_transition.lines.size() - 1));
|
2023-09-15 14:05:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 08:35:09 +00:00
|
|
|
void StackLinearChartView::paintSelectedXIndex(
|
|
|
|
QPainter &p,
|
2023-09-19 10:02:50 +00:00
|
|
|
const PaintContext &c,
|
2023-09-12 08:35:09 +00:00
|
|
|
int selectedXIndex,
|
|
|
|
float64 progress) {
|
2023-09-26 14:16:37 +00:00
|
|
|
if ((selectedXIndex < 0) || c.footer) {
|
2023-09-12 08:35:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-26 14:16:37 +00:00
|
|
|
const auto &[localStart, localEnd] = _transition.zoomedOutXIndices;
|
|
|
|
const auto xPercentageLimits = Limits{
|
|
|
|
c.chartData.xPercentage[localStart],
|
|
|
|
c.chartData.xPercentage[localEnd],
|
|
|
|
};
|
2023-09-12 08:35:09 +00:00
|
|
|
p.setBrush(st::boxBg);
|
|
|
|
const auto r = st::statisticsDetailsDotRadius;
|
|
|
|
const auto i = selectedXIndex;
|
|
|
|
const auto isSameToken = (_selectedPoints.lastXIndex == selectedXIndex)
|
2023-09-19 10:02:50 +00:00
|
|
|
&& (_selectedPoints.lastHeightLimits.min == c.heightLimits.min)
|
|
|
|
&& (_selectedPoints.lastHeightLimits.max == c.heightLimits.max)
|
2023-09-26 14:16:37 +00:00
|
|
|
&& (_selectedPoints.lastXLimits.min == xPercentageLimits.min)
|
|
|
|
&& (_selectedPoints.lastXLimits.max == xPercentageLimits.max);
|
|
|
|
{
|
|
|
|
const auto useCache = isSameToken;
|
2023-09-12 08:35:09 +00:00
|
|
|
if (!useCache) {
|
|
|
|
// Calculate.
|
2023-09-19 10:02:50 +00:00
|
|
|
const auto xPoint = c.rect.width()
|
2023-09-26 14:16:37 +00:00
|
|
|
* ((c.chartData.xPercentage[i] - xPercentageLimits.min)
|
|
|
|
/ (xPercentageLimits.max - xPercentageLimits.min));
|
|
|
|
_selectedPoints.xPoint = xPoint;
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-09-26 14:16:37 +00:00
|
|
|
[[maybe_unused]] const auto o = ScopedPainterOpacity(
|
|
|
|
p,
|
|
|
|
p.opacity() * progress);
|
2023-09-12 08:35:09 +00:00
|
|
|
const auto lineRect = QRectF(
|
2023-09-26 14:16:37 +00:00
|
|
|
_selectedPoints.xPoint - (st::lineWidth / 2.),
|
2023-09-19 10:02:50 +00:00
|
|
|
c.rect.y(),
|
2023-09-12 08:35:09 +00:00
|
|
|
st::lineWidth,
|
2023-09-19 10:02:50 +00:00
|
|
|
c.rect.height());
|
2023-09-12 08:35:09 +00:00
|
|
|
p.fillRect(lineRect, st::windowSubTextFg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_selectedPoints.lastXIndex = selectedXIndex;
|
2023-09-19 10:02:50 +00:00
|
|
|
_selectedPoints.lastHeightLimits = c.heightLimits;
|
2023-09-26 14:16:37 +00:00
|
|
|
_selectedPoints.lastXLimits = xPercentageLimits;
|
2023-09-12 08:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int StackLinearChartView::findXIndexByPosition(
|
|
|
|
const Data::StatisticalChart &chartData,
|
|
|
|
const Limits &xPercentageLimits,
|
|
|
|
const QRect &rect,
|
|
|
|
float64 x) {
|
2023-09-25 15:14:28 +00:00
|
|
|
if (_transition.progress == 1.) {
|
2023-09-15 01:46:28 +00:00
|
|
|
return -1;
|
|
|
|
} else if (x < rect.x()) {
|
2023-09-12 08:35:09 +00:00
|
|
|
return 0;
|
|
|
|
} else if (x > (rect.x() + rect.width())) {
|
|
|
|
return chartData.xPercentage.size() - 1;
|
|
|
|
}
|
|
|
|
const auto pointerRatio = std::clamp(
|
|
|
|
(x - rect.x()) / rect.width(),
|
|
|
|
0.,
|
|
|
|
1.);
|
|
|
|
const auto rawXPercentage = anim::interpolateF(
|
|
|
|
xPercentageLimits.min,
|
|
|
|
xPercentageLimits.max,
|
|
|
|
pointerRatio);
|
|
|
|
const auto it = ranges::lower_bound(
|
|
|
|
chartData.xPercentage,
|
|
|
|
rawXPercentage);
|
|
|
|
const auto left = rawXPercentage - (*(it - 1));
|
|
|
|
const auto right = (*it) - rawXPercentage;
|
|
|
|
const auto nearestXPercentageIt = ((right) > (left)) ? (it - 1) : it;
|
|
|
|
return std::distance(
|
|
|
|
begin(chartData.xPercentage),
|
|
|
|
nearestXPercentageIt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::setEnabled(int id, bool enabled, crl::time now) {
|
|
|
|
const auto it = _entries.find(id);
|
|
|
|
if (it == end(_entries)) {
|
|
|
|
_entries[id] = Entry{
|
|
|
|
.enabled = enabled,
|
|
|
|
.startedAt = now,
|
|
|
|
.anim = anim::value(enabled ? 0. : 1., enabled ? 1. : 0.),
|
|
|
|
};
|
|
|
|
} else if (it->second.enabled != enabled) {
|
|
|
|
auto &entry = it->second;
|
|
|
|
entry.enabled = enabled;
|
|
|
|
entry.startedAt = now;
|
|
|
|
entry.anim.start(enabled ? 1. : 0.);
|
|
|
|
}
|
|
|
|
_isFinished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackLinearChartView::isFinished() const {
|
|
|
|
return _isFinished;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackLinearChartView::isEnabled(int id) const {
|
|
|
|
const auto it = _entries.find(id);
|
|
|
|
return (it == end(_entries)) ? true : it->second.enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 StackLinearChartView::alpha(int id) const {
|
|
|
|
const auto it = _entries.find(id);
|
|
|
|
return (it == end(_entries)) ? 1. : it->second.alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractChartView::HeightLimits StackLinearChartView::heightLimits(
|
|
|
|
Data::StatisticalChart &chartData,
|
|
|
|
Limits xIndices) {
|
|
|
|
constexpr auto kMaxStackLinear = 100.;
|
|
|
|
return {
|
|
|
|
.full = { 0, kMaxStackLinear },
|
|
|
|
.ranged = { 0., kMaxStackLinear },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-25 12:20:43 +00:00
|
|
|
auto StackLinearChartView::maybeLocalZoom(
|
|
|
|
const LocalZoomArgs &args) -> LocalZoomResult {
|
2023-09-25 23:43:08 +00:00
|
|
|
// 8 days.
|
|
|
|
constexpr auto kLimitLength = int(8);
|
2023-09-25 12:20:43 +00:00
|
|
|
// 1 day in middle of limits.
|
2023-09-25 23:43:08 +00:00
|
|
|
constexpr auto kRangeLength = int(0);
|
|
|
|
constexpr auto kLeftSide = int(kLimitLength / 2 + kRangeLength);
|
|
|
|
constexpr auto kRightSide = int(kLimitLength / 2);
|
2023-09-25 12:20:43 +00:00
|
|
|
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.progress = args.progress;
|
2023-09-25 12:20:43 +00:00
|
|
|
if (args.type == LocalZoomArgs::Type::SkipCalculation) {
|
2023-09-25 15:14:28 +00:00
|
|
|
return { true, _transition.zoomedInLimit, _transition.zoomedInRange };
|
2023-09-25 12:20:43 +00:00
|
|
|
} else if (args.type == LocalZoomArgs::Type::CheckAvailability) {
|
|
|
|
return { .hasZoom = true };
|
|
|
|
} else if (args.type == LocalZoomArgs::Type::Prepare) {
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.pendingPrepareToZoomIn = true;
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
|
|
|
const auto xIndex = args.xIndex;
|
|
|
|
const auto &xPercentage = args.chartData.xPercentage;
|
|
|
|
const auto backIndex = (xPercentage.size() - 1);
|
|
|
|
const auto localRangeIndex = (xIndex == backIndex)
|
|
|
|
? (backIndex - kRangeLength)
|
|
|
|
: xIndex;
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInRange = {
|
2023-09-25 12:20:43 +00:00
|
|
|
xPercentage[localRangeIndex],
|
|
|
|
xPercentage[localRangeIndex + kRangeLength],
|
|
|
|
};
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInRangeXIndices = {
|
2023-09-25 12:20:43 +00:00
|
|
|
float64(localRangeIndex),
|
|
|
|
float64(localRangeIndex + kRangeLength),
|
|
|
|
};
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInLimitXIndices = (xIndex < kLeftSide)
|
2023-09-25 23:43:08 +00:00
|
|
|
? Limits{ 0, kLimitLength + kRangeLength }
|
|
|
|
: (xIndex > (backIndex - kRightSide - kRangeLength))
|
2023-09-25 15:14:28 +00:00
|
|
|
? Limits{ float64(backIndex - kLimitLength), float64(backIndex) }
|
|
|
|
: Limits{ float64(xIndex - kLeftSide), float64(xIndex + kRightSide) };
|
|
|
|
_transition.zoomedInLimit = {
|
2023-09-25 12:20:43 +00:00
|
|
|
anim::interpolateF(
|
|
|
|
0.,
|
2023-09-25 15:14:28 +00:00
|
|
|
xPercentage[_transition.zoomedInLimitXIndices.min],
|
2023-09-25 12:20:43 +00:00
|
|
|
args.progress),
|
|
|
|
anim::interpolateF(
|
|
|
|
1.,
|
2023-09-25 15:14:28 +00:00
|
|
|
xPercentage[_transition.zoomedInLimitXIndices.max],
|
2023-09-25 12:20:43 +00:00
|
|
|
args.progress),
|
|
|
|
};
|
2023-09-26 13:43:35 +00:00
|
|
|
const auto oneDay = std::abs(xPercentage[localRangeIndex]
|
|
|
|
- xPercentage[localRangeIndex + ((xIndex == backIndex) ? -1 : 1)]);
|
|
|
|
// Read FindStackXIndicesFromRawXPercentages.
|
|
|
|
const auto offset = (_transition.zoomedInLimitXIndices.max == backIndex)
|
|
|
|
? -oneDay
|
|
|
|
: 0.;
|
2023-09-25 12:20:43 +00:00
|
|
|
const auto resultRange = Limits{
|
|
|
|
InterpolationRatio(
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInLimit.min,
|
|
|
|
_transition.zoomedInLimit.max,
|
2023-09-26 13:43:35 +00:00
|
|
|
_transition.zoomedInRange.min + oneDay * 0.25 + offset),
|
2023-09-25 12:20:43 +00:00
|
|
|
InterpolationRatio(
|
2023-09-25 15:14:28 +00:00
|
|
|
_transition.zoomedInLimit.min,
|
|
|
|
_transition.zoomedInLimit.max,
|
2023-09-26 13:43:35 +00:00
|
|
|
_transition.zoomedInRange.max + oneDay * 0.75 + offset),
|
2023-09-25 12:20:43 +00:00
|
|
|
};
|
2023-09-25 23:44:05 +00:00
|
|
|
return { true, _transition.zoomedInLimitXIndices, resultRange };
|
2023-09-25 12:20:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 08:35:09 +00:00
|
|
|
void StackLinearChartView::tick(crl::time now) {
|
|
|
|
for (auto &[id, entry] : _entries) {
|
|
|
|
const auto dt = std::min(
|
|
|
|
(now - entry.startedAt) / kAlphaDuration,
|
|
|
|
1.);
|
|
|
|
if (dt > 1.) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return update(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLinearChartView::update(float64 dt) {
|
|
|
|
auto finishedCount = 0;
|
|
|
|
auto idsToRemove = std::vector<int>();
|
|
|
|
for (auto &[id, entry] : _entries) {
|
|
|
|
if (!entry.startedAt) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
entry.anim.update(dt, anim::linear);
|
|
|
|
const auto progress = entry.anim.current();
|
|
|
|
entry.alpha = std::clamp(
|
|
|
|
progress,
|
|
|
|
0.,
|
|
|
|
1.);
|
|
|
|
if (entry.alpha == 1.) {
|
|
|
|
idsToRemove.push_back(id);
|
|
|
|
}
|
|
|
|
if (entry.anim.current() == entry.anim.to()) {
|
|
|
|
finishedCount++;
|
|
|
|
entry.anim.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_isFinished = (finishedCount == _entries.size());
|
|
|
|
for (const auto &id : idsToRemove) {
|
|
|
|
_entries.remove(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Statistic
|