Added Data class for horizontal lines on statistic charts.

This commit is contained in:
23rd 2023-05-24 15:38:22 +03:00 committed by John Preston
parent 029e0c9488
commit 06948ad15e
3 changed files with 135 additions and 0 deletions

View File

@ -1282,6 +1282,8 @@ PRIVATE
settings/settings_type.h
settings/settings_websites.cpp
settings/settings_websites.h
statistics/chart_horizontal_lines_data.cpp
statistics/chart_horizontal_lines_data.h
statistics/segment_tree.cpp
statistics/segment_tree.h
statistics/statistics_box.cpp

View File

@ -0,0 +1,100 @@
/*
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/chart_horizontal_lines_data.h"
#include "lang/lang_tag.h"
namespace Statistic {
namespace {
constexpr auto kMinLines = int(2);
constexpr auto kMaxLines = int(6);
constexpr auto kStep = 5.;
[[nodiscard]] int Round(int maxValue) {
const auto k = int(maxValue / kStep);
return (k % 10 == 0) ? maxValue : ((maxValue / 10 + 1) * 10);
}
} // namespace
ChartHorizontalLinesData::ChartHorizontalLinesData(
int newMaxHeight,
int newMinHeight,
bool useMinHeight) {
if (!useMinHeight) {
const auto v = (newMaxHeight > 100)
? Round(newMaxHeight)
: newMaxHeight;
const auto step = std::max(1, int(std::ceil(v / kStep)));
auto n = kMaxLines;
if (v < kMaxLines) {
n = std::max(2, v + 1);
} else if (v / 2 < kMaxLines) {
n = v / 2 + 1;
if (v % 2 != 0) {
n++;
}
}
lines.resize(n);
for (auto i = 1; i < n; i++) {
auto &line = lines[i];
line.absoluteValue = i * step;
line.caption = Lang::FormatCountToShort(
line.absoluteValue).string;
}
} else {
auto n = int(0);
const auto diff = newMaxHeight - newMinHeight;
auto step = 0.;
if (diff == 0) {
newMinHeight--;
n = kMaxLines / 2;
step = 1.;
} else if (diff < kMaxLines) {
n = std::max(kMinLines, diff + 1);
step = 1.;
} else if (diff / 2 < kMaxLines) {
n = diff / 2 + diff % 2 + 1;
step = 2.;
} else {
step = (newMaxHeight - newMinHeight) / kStep;
if (step <= 0) {
step = 1;
n = std::max(kMinLines, newMaxHeight - newMinHeight + 1);
} else {
n = 6;
}
}
lines.resize(n);
const auto diffAbsoluteValue = int((n - 1) * step);
for (auto i = 0; i < n; i++) {
auto &line = lines[i];
const auto value = int(i * step);
line.absoluteValue = newMinHeight + value;
line.relativeValue = value / float64(diffAbsoluteValue);
line.caption = (line.absoluteValue >= 10'000)
? Lang::FormatCountToShort(line.absoluteValue).string
: QString("%L1").arg(line.absoluteValue);
}
}
}
int ChartHorizontalLinesData::LookupHeight(int maxValue) {
const auto v = (maxValue > 100) ? Round(maxValue) : maxValue;
const auto step = int(std::ceil(v / kStep));
return step * kStep;
}
} // namespace Statistic

View File

@ -0,0 +1,33 @@
/*
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
*/
#pragma once
namespace Statistic {
struct ChartHorizontalLinesData final {
public:
ChartHorizontalLinesData(
int newMaxHeight,
int newMinHeight,
bool useMinHeight);
[[nodiscard]] static int LookupHeight(int maxValue);
struct Line final {
float64 absoluteValue = 0.;
float64 relativeValue = 0.;
QString caption;
};
std::vector<Line> lines;
float64 alpha = 0.;
float64 fixedAlpha = 1.;
};
} // namespace Statistic