Fixed width of stats PointDetailsWidget for charts with currency.

This commit is contained in:
23rd 2024-09-01 23:29:21 +03:00
parent 8cd5e51982
commit 8aa7499e63
3 changed files with 47 additions and 12 deletions

View File

@ -1156,20 +1156,12 @@ void ChartWidget::setupDetails() {
_chartArea->update(); _chartArea->update();
return; return;
} }
const auto maxAbsoluteValue = [&] {
auto maxValue = ChartValue(0);
for (const auto &l : _chartData.lines) {
maxValue = std::max(l.maxValue, maxValue);
}
return maxValue;
}();
if (hasLocalZoom()) { if (hasLocalZoom()) {
_zoomEnabled = true; _zoomEnabled = true;
} }
_details.widget = base::make_unique_q<PointDetailsWidget>( _details.widget = base::make_unique_q<PointDetailsWidget>(
this, this,
_chartData, _chartData,
maxAbsoluteValue,
_zoomEnabled); _zoomEnabled);
_details.widget->setClickedCallback([=] { _details.widget->setClickedCallback([=] {
const auto index = _details.widget->xIndex(); const auto index = _details.widget->xIndex();

View File

@ -130,7 +130,6 @@ void PaintDetails(
PointDetailsWidget::PointDetailsWidget( PointDetailsWidget::PointDetailsWidget(
not_null<Ui::RpWidget*> parent, not_null<Ui::RpWidget*> parent,
const Data::StatisticalChart &chartData, const Data::StatisticalChart &chartData,
float64 maxAbsoluteValue,
bool zoomEnabled) bool zoomEnabled)
: Ui::AbstractButton(parent) : Ui::AbstractButton(parent)
, _zoomEnabled(zoomEnabled) , _zoomEnabled(zoomEnabled)
@ -173,12 +172,44 @@ PointDetailsWidget::PointDetailsWidget(
return 0; return 0;
}(); }();
const auto calculatedWidth = [&]{ const auto hasUsdLine = (_chartData.currencyRate != 0)
&& (_chartData.currency != Data::StatisticalCurrency::None)
&& (_chartData.lines.size() == 1);
const auto maxValueTextWidth = [&] {
if (hasUsdLine) {
auto maxValueWidth = 0;
const auto multiplier = float64(Data::kEarnMultiplier);
for (const auto &value : _chartData.lines.front().y) {
const auto valueText = Ui::Text::String(
_textStyle,
QString::number(value / multiplier));
const auto usdText = Ui::Text::String(
_textStyle,
Info::ChannelEarn::ToUsd(value, _chartData.currencyRate));
const auto width = std::max(
usdText.maxWidth(),
valueText.maxWidth());
if (width > maxValueWidth) {
maxValueWidth = width;
}
}
return maxValueWidth;
}
const auto maxAbsoluteValue = [&] {
auto maxValue = ChartValue(0);
for (const auto &l : _chartData.lines) {
maxValue = std::max(l.maxValue, maxValue);
}
return maxValue;
}();
const auto maxValueText = Ui::Text::String( const auto maxValueText = Ui::Text::String(
_textStyle, _textStyle,
Lang::FormatCountDecimal(maxAbsoluteValue)); Lang::FormatCountDecimal(maxAbsoluteValue));
const auto maxValueTextWidth = maxValueText.maxWidth(); return maxValueText.maxWidth();
}();
const auto calculatedWidth = [&]{
auto maxNameTextWidth = 0; auto maxNameTextWidth = 0;
for (const auto &dataLine : _chartData.lines) { for (const auto &dataLine : _chartData.lines) {
const auto maxNameText = Ui::Text::String( const auto maxNameText = Ui::Text::String(
@ -187,6 +218,19 @@ PointDetailsWidget::PointDetailsWidget(
maxNameTextWidth = std::max( maxNameTextWidth = std::max(
maxNameText.maxWidth(), maxNameText.maxWidth(),
maxNameTextWidth); maxNameTextWidth);
if (hasUsdLine) {
const auto currency = Ui::Text::String(
_textStyle,
tr::lng_channel_earn_chart_overriden_detail_currency(
tr::now));
const auto usd = Ui::Text::String(
_textStyle,
tr::lng_channel_earn_chart_overriden_detail_usd(
tr::now));
maxNameTextWidth = std::max(
std::max(currency.maxWidth(), usd.maxWidth()),
maxNameTextWidth);
}
} }
{ {
const auto maxHeaderText = Ui::Text::String( const auto maxHeaderText = Ui::Text::String(

View File

@ -27,7 +27,6 @@ public:
PointDetailsWidget( PointDetailsWidget(
not_null<Ui::RpWidget*> parent, not_null<Ui::RpWidget*> parent,
const Data::StatisticalChart &chartData, const Data::StatisticalChart &chartData,
float64 maxAbsoluteValue,
bool zoomEnabled); bool zoomEnabled);
[[nodiscard]] int xIndex() const; [[nodiscard]] int xIndex() const;