mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-07 15:43:55 +02:00
Added ability to paint horizontal lines for double linear charts.
This commit is contained in:
parent
62b3b60c45
commit
d13fe39629
4 changed files with 71 additions and 16 deletions
|
@ -21,12 +21,19 @@ constexpr auto kStep = 5.;
|
||||||
return (k % 10 == 0) ? maxValue : ((maxValue / 10 + 1) * 10);
|
return (k % 10 == 0) ? maxValue : ((maxValue / 10 + 1) * 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] QString Format(int absoluteValue) {
|
||||||
|
return (absoluteValue >= 10'000)
|
||||||
|
? Lang::FormatCountToShort(absoluteValue).string
|
||||||
|
: QString("%L1").arg(absoluteValue);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
ChartHorizontalLinesData::ChartHorizontalLinesData(
|
ChartHorizontalLinesData::ChartHorizontalLinesData(
|
||||||
int newMaxHeight,
|
int newMaxHeight,
|
||||||
int newMinHeight,
|
int newMinHeight,
|
||||||
bool useMinHeight) {
|
bool useMinHeight,
|
||||||
|
float64 rightRatio) {
|
||||||
if (!useMinHeight) {
|
if (!useMinHeight) {
|
||||||
const auto v = (newMaxHeight > 100)
|
const auto v = (newMaxHeight > 100)
|
||||||
? Round(newMaxHeight)
|
? Round(newMaxHeight)
|
||||||
|
@ -78,14 +85,21 @@ ChartHorizontalLinesData::ChartHorizontalLinesData(
|
||||||
|
|
||||||
lines.resize(n);
|
lines.resize(n);
|
||||||
const auto diffAbsoluteValue = int((n - 1) * step);
|
const auto diffAbsoluteValue = int((n - 1) * step);
|
||||||
|
const auto skipFloatValues = (step / rightRatio) < 1;
|
||||||
for (auto i = 0; i < n; i++) {
|
for (auto i = 0; i < n; i++) {
|
||||||
auto &line = lines[i];
|
auto &line = lines[i];
|
||||||
const auto value = int(i * step);
|
const auto value = int(i * step);
|
||||||
line.absoluteValue = newMinHeight + value;
|
line.absoluteValue = newMinHeight + value;
|
||||||
line.relativeValue = 1. - value / float64(diffAbsoluteValue);
|
line.relativeValue = 1. - value / float64(diffAbsoluteValue);
|
||||||
line.caption = (line.absoluteValue >= 10'000)
|
line.caption = Format(line.absoluteValue);
|
||||||
? Lang::FormatCountToShort(line.absoluteValue).string
|
if (rightRatio > 0) {
|
||||||
: QString("%L1").arg(line.absoluteValue);
|
const auto v = (newMinHeight + i * step) / rightRatio;
|
||||||
|
line.scaledLineCaption = (!skipFloatValues)
|
||||||
|
? Format(v)
|
||||||
|
: ((v - int(v)) < 0.01)
|
||||||
|
? Format(v)
|
||||||
|
: QString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@ public:
|
||||||
ChartHorizontalLinesData(
|
ChartHorizontalLinesData(
|
||||||
int newMaxHeight,
|
int newMaxHeight,
|
||||||
int newMinHeight,
|
int newMinHeight,
|
||||||
bool useMinHeight);
|
bool useMinHeight,
|
||||||
|
float64 rightRatio);
|
||||||
|
|
||||||
void computeRelative(
|
void computeRelative(
|
||||||
int newMaxHeight,
|
int newMaxHeight,
|
||||||
|
@ -26,6 +27,8 @@ public:
|
||||||
float64 absoluteValue = 0.;
|
float64 absoluteValue = 0.;
|
||||||
float64 relativeValue = 0.;
|
float64 relativeValue = 0.;
|
||||||
QString caption;
|
QString caption;
|
||||||
|
QString scaledLineCaption;
|
||||||
|
float64 rightCaptionWidth = 0.;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Line> lines;
|
std::vector<Line> lines;
|
||||||
|
|
|
@ -18,10 +18,21 @@ ChartHorizontalLinesView::ChartHorizontalLinesView() = default;
|
||||||
void ChartHorizontalLinesView::setChartData(
|
void ChartHorizontalLinesView::setChartData(
|
||||||
const Data::StatisticalChart &chartData,
|
const Data::StatisticalChart &chartData,
|
||||||
ChartViewType type) {
|
ChartViewType type) {
|
||||||
|
_horizontalLines.clear();
|
||||||
_isDouble = (type == ChartViewType::DoubleLinear);
|
_isDouble = (type == ChartViewType::DoubleLinear);
|
||||||
if (_isDouble && (chartData.lines.size() == 2)) {
|
if (_isDouble && (chartData.lines.size() == 2)) {
|
||||||
_leftColor = chartData.lines.front().color;
|
_leftPen = QPen(chartData.lines.front().color);
|
||||||
_rightColor = chartData.lines.back().color;
|
_rightPen = QPen(chartData.lines.back().color);
|
||||||
|
|
||||||
|
const auto firstMax = chartData.lines.front().maxValue;
|
||||||
|
const auto secondMax = chartData.lines.back().maxValue;
|
||||||
|
if (firstMax > secondMax) {
|
||||||
|
_isLeftLineScaled = false;
|
||||||
|
_scaledLineRatio = firstMax / float64(secondMax);
|
||||||
|
} else {
|
||||||
|
_isLeftLineScaled = true;
|
||||||
|
_scaledLineRatio = secondMax / float64(firstMax);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,16 +57,31 @@ void ChartHorizontalLinesView::paintHorizontalLines(
|
||||||
void ChartHorizontalLinesView::paintCaptionsToHorizontalLines(
|
void ChartHorizontalLinesView::paintCaptionsToHorizontalLines(
|
||||||
QPainter &p,
|
QPainter &p,
|
||||||
const QRect &r) {
|
const QRect &r) {
|
||||||
|
const auto offset = r.y() - st::statisticsChartHorizontalLineCaptionSkip;
|
||||||
|
p.setFont(st::statisticsDetailsBottomCaptionStyle.font);
|
||||||
for (auto &horizontalLine : _horizontalLines) {
|
for (auto &horizontalLine : _horizontalLines) {
|
||||||
const auto alpha = p.opacity();
|
const auto alpha = p.opacity();
|
||||||
p.setOpacity(horizontalLine.alpha);
|
p.setOpacity(horizontalLine.alpha);
|
||||||
p.setFont(st::statisticsDetailsBottomCaptionStyle.font);
|
|
||||||
p.setPen(st::windowSubTextFg);
|
|
||||||
const auto offset = r.y()
|
|
||||||
- st::statisticsChartHorizontalLineCaptionSkip;
|
|
||||||
for (const auto &line : horizontalLine.lines) {
|
for (const auto &line : horizontalLine.lines) {
|
||||||
const auto y = offset + r.height() * line.relativeValue;
|
const auto y = offset + r.height() * line.relativeValue;
|
||||||
p.drawText(0, y, line.caption);
|
p.setPen(_isDouble ? _leftPen : st::windowSubTextFg);
|
||||||
|
p.drawText(
|
||||||
|
0,
|
||||||
|
y,
|
||||||
|
(!_isDouble)
|
||||||
|
? line.caption
|
||||||
|
: _isLeftLineScaled
|
||||||
|
? line.scaledLineCaption
|
||||||
|
: line.caption);
|
||||||
|
if (_isDouble) {
|
||||||
|
p.setPen(_rightPen);
|
||||||
|
p.drawText(
|
||||||
|
r.width() - line.rightCaptionWidth,
|
||||||
|
y,
|
||||||
|
_isLeftLineScaled
|
||||||
|
? line.caption
|
||||||
|
: line.scaledLineCaption);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p.setOpacity(alpha);
|
p.setOpacity(alpha);
|
||||||
}
|
}
|
||||||
|
@ -87,10 +113,19 @@ void ChartHorizontalLinesView::setAlpha(float64 value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChartHorizontalLinesView::add(Limits newHeight, bool animated) {
|
void ChartHorizontalLinesView::add(Limits newHeight, bool animated) {
|
||||||
const auto newLinesData = ChartHorizontalLinesData(
|
auto newLinesData = ChartHorizontalLinesData(
|
||||||
newHeight.max,
|
newHeight.max,
|
||||||
newHeight.min,
|
newHeight.min,
|
||||||
true);
|
true,
|
||||||
|
_isDouble ? _scaledLineRatio : 0.);
|
||||||
|
if (_isDouble) {
|
||||||
|
const auto &font = st::statisticsDetailsBottomCaptionStyle.font;
|
||||||
|
for (auto &line : newLinesData.lines) {
|
||||||
|
line.rightCaptionWidth = font->width(_isLeftLineScaled
|
||||||
|
? line.caption
|
||||||
|
: line.scaledLineCaption);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!animated) {
|
if (!animated) {
|
||||||
_horizontalLines.clear();
|
_horizontalLines.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,11 +36,14 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _isDouble = false;
|
bool _isDouble = false;
|
||||||
QColor _leftColor;
|
QPen _leftPen;
|
||||||
QColor _rightColor;
|
QPen _rightPen;
|
||||||
|
|
||||||
std::vector<ChartHorizontalLinesData> _horizontalLines;
|
std::vector<ChartHorizontalLinesData> _horizontalLines;
|
||||||
|
|
||||||
|
float64 _scaledLineRatio = 0.;
|
||||||
|
bool _isLeftLineScaled = false;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Statistic
|
} // namespace Statistic
|
||||||
|
|
Loading…
Add table
Reference in a new issue