From 6109ec70b8b3ae94f01dc840d94210122bc0c806 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 3 Oct 2023 16:53:11 +0300 Subject: [PATCH] Slightly improved format of dates on charts. --- .../data/data_statistics_chart.cpp | 2 +- .../statistics/info_statistics_widget.cpp | 2 +- .../SourceFiles/statistics/chart_widget.cpp | 37 +++++++++++++------ .../statistics/point_details_widget.cpp | 16 +++++--- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/data/data_statistics_chart.cpp b/Telegram/SourceFiles/data/data_statistics_chart.cpp index d3e3a1026..9978e498f 100644 --- a/Telegram/SourceFiles/data/data_statistics_chart.cpp +++ b/Telegram/SourceFiles/data/data_statistics_chart.cpp @@ -43,7 +43,7 @@ void StatisticalChart::measure() { const auto dateCount = int((end - start) / timeStep) + 10; daysLookup.reserve(dateCount); constexpr auto kOneDay = 3600 * 24 * 1000; - const auto formatter = u"MMM d"_q; + const auto formatter = u"d MMM"_q; for (auto i = 0; i < dateCount; i++) { const auto r = (start + (i * timeStep)) / 1000; const auto dateTime = QDateTime::fromSecsSinceEpoch(r); diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp index 877e4e937..2898c63fb 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp @@ -268,7 +268,7 @@ void FillOverview( st::statisticsLayerMargins + st::statisticsChartHeaderPadding); header->resizeToWidth(header->width()); header->setTitle(tr::lng_stats_overview_title(tr::now)); - const auto formatter = u"MMM d"_q; + const auto formatter = u"d MMM yyyy"_q; const auto from = QDateTime::fromSecsSinceEpoch(startDate); const auto to = QDateTime::fromSecsSinceEpoch(endDate); header->setRightInfo(QLocale().toString(from.date(), formatter) diff --git a/Telegram/SourceFiles/statistics/chart_widget.cpp b/Telegram/SourceFiles/statistics/chart_widget.cpp index 6b79ef57e..7072e151a 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.cpp +++ b/Telegram/SourceFiles/statistics/chart_widget.cpp @@ -65,14 +65,28 @@ void FillLineColorsByKey(Data::StatisticalChart &chartData) { [[nodiscard]] QString HeaderRightInfo( const Data::StatisticalChart &chartData, - const Limits &limits) { - return (limits.min == limits.max) - ? chartData.getDayString(limits.min) - : chartData.getDayString(limits.min) + int xIndexMin, + int xIndexMax) { + constexpr auto kOneDay = 3600 * 24 * 1000; + const auto leftTimestamp = chartData.x[xIndexMin]; + if (leftTimestamp < kOneDay) { + return {}; + } + const auto formatter = u"d MMM yyyy"_q; + const auto leftDateTime = QDateTime::fromSecsSinceEpoch( + leftTimestamp / 1000); + const auto leftText = QLocale().toString(leftDateTime.date(), formatter); + if (xIndexMin == xIndexMax) { + return leftText; + } else { + const auto rightDateTime = QDateTime::fromSecsSinceEpoch( + chartData.x[xIndexMax] / 1000); + return leftText + ' ' + QChar(8212) + ' ' - + chartData.getDayString(limits.max); + + QLocale().toString(rightDateTime.date(), formatter); + } } void PaintBottomLine( @@ -1057,8 +1071,8 @@ void ChartWidget::updateHeader() { if (!_chartData) { return; } - const auto indices = _animationController.currentXIndices(); - _header->setRightInfo(HeaderRightInfo(_chartData, indices)); + const auto i = _animationController.currentXIndices(); + _header->setRightInfo(HeaderRightInfo(_chartData, i.min, i.max)); _header->update(); } @@ -1261,7 +1275,7 @@ void ChartWidget::processLocalZoom(int xIndex) { header->setGeometry(g); }, header->lifetime()); header->setTitle(_header->title()); - header->setRightInfo(_chartData.getDayString(xIndex)); + header->setRightInfo(HeaderRightInfo(_chartData, xIndex, xIndex)); const auto enableMouse = [=](bool value) { setAttribute(Qt::WA_TransparentForMouseEvents, !value); @@ -1332,11 +1346,11 @@ void ChartWidget::processLocalZoom(int xIndex) { createMouseTracking(); _footer->xPercentageLimitsChange( ) | rpl::start_with_next([=](const Limits &l) { - const auto result = FindStackXIndicesFromRawXPercentages( + const auto r = FindStackXIndicesFromRawXPercentages( _chartData, l, zoomLimitIndices); - header->setRightInfo(HeaderRightInfo(_chartData, result)); + header->setRightInfo(HeaderRightInfo(_chartData, r.min, r.max)); header->update(); }, header->lifetime()); }; @@ -1468,7 +1482,8 @@ void ChartWidget::setZoomedChartData( ranges::find(_chartData.x, x)); customHeader->setTitle(_header->title()); if ((xIndex >= 0) && (xIndex < _chartData.x.size())) { - customHeader->setRightInfo(_chartData.getDayString(xIndex)); + customHeader->setRightInfo( + HeaderRightInfo(_chartData, xIndex, xIndex)); } const auto &headerPadding = st::statisticsChartHeaderPadding; customHeader->moveToLeft(headerPadding.left(), headerPadding.top()); diff --git a/Telegram/SourceFiles/statistics/point_details_widget.cpp b/Telegram/SourceFiles/statistics/point_details_widget.cpp index 820da5b3e..bb9c9c162 100644 --- a/Telegram/SourceFiles/statistics/point_details_widget.cpp +++ b/Telegram/SourceFiles/statistics/point_details_widget.cpp @@ -127,8 +127,8 @@ PointDetailsWidget::PointDetailsWidget( , _chartData(chartData) , _textStyle(st::statisticsDetailsPopupStyle) , _headerStyle(st::statisticsDetailsPopupHeaderStyle) -, _longFormat(u"ddd, MMM d hh:mm"_q) -, _shortFormat(u"ddd, MMM d"_q) { +, _longFormat(u"ddd, d MMM hh:mm"_q) +, _shortFormat(u"ddd, d MMM yyyy"_q) { if (zoomEnabled) { rpl::single(rpl::empty_value()) | rpl::then( @@ -232,9 +232,15 @@ void PointDetailsWidget::setXIndex(int xIndex) { if (xIndex < 0) { return; } - _header.setText( - _headerStyle, - FormatTimestamp(_chartData.x[xIndex], _longFormat, _shortFormat)); + { + constexpr auto kOneDay = 3600 * 24 * 1000; + const auto timestamp = _chartData.x[xIndex]; + _header.setText( + _headerStyle, + (timestamp < kOneDay) + ? _chartData.getDayString(xIndex) + : FormatTimestamp(timestamp, _longFormat, _shortFormat)); + } _lines.clear(); _lines.reserve(_chartData.lines.size());