mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-15 21:57:10 +02:00
Moved class of line ratio for double linear chart from private space.
This commit is contained in:
parent
bc6556ebc4
commit
5ea066e6a7
4 changed files with 88 additions and 30 deletions
68
Telegram/SourceFiles/statistics/view/abstract_chart_view.cpp
Normal file
68
Telegram/SourceFiles/statistics/view/abstract_chart_view.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
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/statistics_common.h"
|
||||
#include "statistics/view/abstract_chart_view.h"
|
||||
|
||||
#include "data/data_statistics_chart.h"
|
||||
|
||||
namespace Statistic {
|
||||
|
||||
bool CachedSelectedPoints::isSame(int x, const PaintContext &c) const {
|
||||
return (lastXIndex == x)
|
||||
&& (lastHeightLimits.min == c.heightLimits.min)
|
||||
&& (lastHeightLimits.max == c.heightLimits.max)
|
||||
&& (lastXLimits.min == c.xPercentageLimits.min)
|
||||
&& (lastXLimits.max == c.xPercentageLimits.max);
|
||||
}
|
||||
|
||||
DoubleLineRatios::DoubleLineRatios(bool isDouble) {
|
||||
first = second = (isDouble ? 0 : 1);
|
||||
}
|
||||
|
||||
void DoubleLineRatios::init(const Data::StatisticalChart &chartData) {
|
||||
if (chartData.lines.size() != 2) {
|
||||
first = 1.;
|
||||
second = 1.;
|
||||
} else {
|
||||
const auto firstMax = chartData.lines.front().maxValue;
|
||||
const auto secondMax = chartData.lines.back().maxValue;
|
||||
if (firstMax > secondMax) {
|
||||
first = 1.;
|
||||
second = firstMax / float64(secondMax);
|
||||
} else {
|
||||
first = secondMax / float64(firstMax);
|
||||
second = 1.;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float64 DoubleLineRatios::ratio(int lineId) const {
|
||||
return (lineId == 1) ? first : second;
|
||||
}
|
||||
|
||||
void AbstractChartView::setUpdateCallback(Fn<void()> callback) {
|
||||
_updateCallback = std::move(callback);
|
||||
}
|
||||
|
||||
void AbstractChartView::update() {
|
||||
if (_updateCallback) {
|
||||
_updateCallback();
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractChartView::setLinesFilterController(
|
||||
std::shared_ptr<LinesFilterController> c) {
|
||||
_linesFilterController = std::move(c);
|
||||
}
|
||||
|
||||
auto AbstractChartView::linesFilterController() const
|
||||
-> std::shared_ptr<LinesFilterController> {
|
||||
return _linesFilterController;
|
||||
}
|
||||
|
||||
} // namespace Statistic
|
|
@ -34,6 +34,18 @@ struct CachedSelectedPoints final {
|
|||
base::flat_map<int, QPointF> points;
|
||||
};
|
||||
|
||||
class DoubleLineRatios final : std::pair<float64, float64> {
|
||||
public:
|
||||
DoubleLineRatios(bool isDouble);
|
||||
|
||||
operator bool() const {
|
||||
return first > 0;
|
||||
}
|
||||
|
||||
void init(const Data::StatisticalChart &chartData);
|
||||
[[nodiscard]] float64 ratio(int lineId) const;
|
||||
};
|
||||
|
||||
class AbstractChartView {
|
||||
public:
|
||||
virtual ~AbstractChartView() = default;
|
||||
|
|
|
@ -18,17 +18,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
namespace Statistic {
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] float64 Ratio(
|
||||
const LinearChartView::CachedLineRatios &ratios,
|
||||
int id) {
|
||||
return (id == 1) ? ratios.first : ratios.second;
|
||||
}
|
||||
|
||||
void PaintChartLine(
|
||||
QPainter &p,
|
||||
int lineIndex,
|
||||
const PaintContext &c,
|
||||
const LinearChartView::CachedLineRatios &ratios) {
|
||||
const DoubleLineRatios &ratios) {
|
||||
const auto &line = c.chartData.lines[lineIndex];
|
||||
|
||||
auto chartPoints = QPolygonF();
|
||||
|
@ -39,7 +33,7 @@ void PaintChartLine(
|
|||
float64(c.chartData.xPercentage.size() - 1),
|
||||
c.xIndices.max + kOffset));
|
||||
|
||||
const auto ratio = Ratio(ratios, line.id);
|
||||
const auto ratio = ratios.ratio(line.id);
|
||||
|
||||
for (auto i = localStart; i <= localEnd; i++) {
|
||||
if (line.y[i] < 0) {
|
||||
|
@ -63,7 +57,7 @@ void PaintChartLine(
|
|||
} // namespace
|
||||
|
||||
LinearChartView::LinearChartView(bool isDouble)
|
||||
: _cachedLineRatios(CachedLineRatios{ isDouble ? 0 : 1, isDouble ? 0 : 1 }) {
|
||||
: _cachedLineRatios(isDouble) {
|
||||
}
|
||||
|
||||
LinearChartView::~LinearChartView() = default;
|
||||
|
@ -148,7 +142,7 @@ void LinearChartView::paintSelectedXIndex(
|
|||
|| (lineAlpha < 1. && !linesFilter->isEnabled(line.id));
|
||||
if (!useCache) {
|
||||
// Calculate.
|
||||
const auto r = Ratio(_cachedLineRatios, line.id);
|
||||
const auto r = _cachedLineRatios.ratio(line.id);
|
||||
const auto xPoint = c.rect.width()
|
||||
* ((c.chartData.xPercentage[i] - c.xPercentageLimits.min)
|
||||
/ (c.xPercentageLimits.max - c.xPercentageLimits.min));
|
||||
|
@ -216,22 +210,8 @@ int LinearChartView::findXIndexByPosition(
|
|||
AbstractChartView::HeightLimits LinearChartView::heightLimits(
|
||||
Data::StatisticalChart &chartData,
|
||||
Limits xIndices) {
|
||||
if (!_cachedLineRatios.first) {
|
||||
// Double Linear calculation.
|
||||
if (chartData.lines.size() != 2) {
|
||||
_cachedLineRatios.first = 1.;
|
||||
_cachedLineRatios.second = 1.;
|
||||
} else {
|
||||
const auto firstMax = chartData.lines.front().maxValue;
|
||||
const auto secondMax = chartData.lines.back().maxValue;
|
||||
if (firstMax > secondMax) {
|
||||
_cachedLineRatios.first = 1.;
|
||||
_cachedLineRatios.second = firstMax / float64(secondMax);
|
||||
} else {
|
||||
_cachedLineRatios.first = secondMax / float64(firstMax);
|
||||
_cachedLineRatios.second = 1.;
|
||||
}
|
||||
}
|
||||
if (!_cachedLineRatios) {
|
||||
_cachedLineRatios.init(chartData);
|
||||
}
|
||||
|
||||
auto minValue = std::numeric_limits<int>::max();
|
||||
|
@ -243,7 +223,7 @@ AbstractChartView::HeightLimits LinearChartView::heightLimits(
|
|||
if (!linesFilterController()->isEnabled(l.id)) {
|
||||
continue;
|
||||
}
|
||||
const auto r = Ratio(_cachedLineRatios, l.id);
|
||||
const auto r = _cachedLineRatios.ratio(l.id);
|
||||
const auto lineMax = l.segmentTree.rMaxQ(xIndices.min, xIndices.max);
|
||||
const auto lineMin = l.segmentTree.rMinQ(xIndices.min, xIndices.max);
|
||||
maxValue = std::max(int(lineMax * r), maxValue);
|
||||
|
|
|
@ -20,8 +20,6 @@ struct Limits;
|
|||
|
||||
class LinearChartView final : public AbstractChartView {
|
||||
public:
|
||||
using CachedLineRatios = std::pair<float64, float64>;
|
||||
|
||||
LinearChartView(bool isDouble);
|
||||
~LinearChartView() override final;
|
||||
|
||||
|
@ -44,7 +42,7 @@ public:
|
|||
Limits xIndices) override;
|
||||
|
||||
private:
|
||||
CachedLineRatios _cachedLineRatios;
|
||||
DoubleLineRatios _cachedLineRatios;
|
||||
|
||||
[[nodiscard]] float64 lineRatio() const;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue