Slightly improved format of dates on charts.

This commit is contained in:
23rd 2023-10-03 16:53:11 +03:00 committed by John Preston
parent c20bd17029
commit 6109ec70b8
4 changed files with 39 additions and 18 deletions

View file

@ -43,7 +43,7 @@ void StatisticalChart::measure() {
const auto dateCount = int((end - start) / timeStep) + 10; const auto dateCount = int((end - start) / timeStep) + 10;
daysLookup.reserve(dateCount); daysLookup.reserve(dateCount);
constexpr auto kOneDay = 3600 * 24 * 1000; 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++) { for (auto i = 0; i < dateCount; i++) {
const auto r = (start + (i * timeStep)) / 1000; const auto r = (start + (i * timeStep)) / 1000;
const auto dateTime = QDateTime::fromSecsSinceEpoch(r); const auto dateTime = QDateTime::fromSecsSinceEpoch(r);

View file

@ -268,7 +268,7 @@ void FillOverview(
st::statisticsLayerMargins + st::statisticsChartHeaderPadding); st::statisticsLayerMargins + st::statisticsChartHeaderPadding);
header->resizeToWidth(header->width()); header->resizeToWidth(header->width());
header->setTitle(tr::lng_stats_overview_title(tr::now)); 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 from = QDateTime::fromSecsSinceEpoch(startDate);
const auto to = QDateTime::fromSecsSinceEpoch(endDate); const auto to = QDateTime::fromSecsSinceEpoch(endDate);
header->setRightInfo(QLocale().toString(from.date(), formatter) header->setRightInfo(QLocale().toString(from.date(), formatter)

View file

@ -65,14 +65,28 @@ void FillLineColorsByKey(Data::StatisticalChart &chartData) {
[[nodiscard]] QString HeaderRightInfo( [[nodiscard]] QString HeaderRightInfo(
const Data::StatisticalChart &chartData, const Data::StatisticalChart &chartData,
const Limits &limits) { int xIndexMin,
return (limits.min == limits.max) int xIndexMax) {
? chartData.getDayString(limits.min) constexpr auto kOneDay = 3600 * 24 * 1000;
: chartData.getDayString(limits.min) 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) + QChar(8212)
+ ' ' + ' '
+ chartData.getDayString(limits.max); + QLocale().toString(rightDateTime.date(), formatter);
}
} }
void PaintBottomLine( void PaintBottomLine(
@ -1057,8 +1071,8 @@ void ChartWidget::updateHeader() {
if (!_chartData) { if (!_chartData) {
return; return;
} }
const auto indices = _animationController.currentXIndices(); const auto i = _animationController.currentXIndices();
_header->setRightInfo(HeaderRightInfo(_chartData, indices)); _header->setRightInfo(HeaderRightInfo(_chartData, i.min, i.max));
_header->update(); _header->update();
} }
@ -1261,7 +1275,7 @@ void ChartWidget::processLocalZoom(int xIndex) {
header->setGeometry(g); header->setGeometry(g);
}, header->lifetime()); }, header->lifetime());
header->setTitle(_header->title()); header->setTitle(_header->title());
header->setRightInfo(_chartData.getDayString(xIndex)); header->setRightInfo(HeaderRightInfo(_chartData, xIndex, xIndex));
const auto enableMouse = [=](bool value) { const auto enableMouse = [=](bool value) {
setAttribute(Qt::WA_TransparentForMouseEvents, !value); setAttribute(Qt::WA_TransparentForMouseEvents, !value);
@ -1332,11 +1346,11 @@ void ChartWidget::processLocalZoom(int xIndex) {
createMouseTracking(); createMouseTracking();
_footer->xPercentageLimitsChange( _footer->xPercentageLimitsChange(
) | rpl::start_with_next([=](const Limits &l) { ) | rpl::start_with_next([=](const Limits &l) {
const auto result = FindStackXIndicesFromRawXPercentages( const auto r = FindStackXIndicesFromRawXPercentages(
_chartData, _chartData,
l, l,
zoomLimitIndices); zoomLimitIndices);
header->setRightInfo(HeaderRightInfo(_chartData, result)); header->setRightInfo(HeaderRightInfo(_chartData, r.min, r.max));
header->update(); header->update();
}, header->lifetime()); }, header->lifetime());
}; };
@ -1468,7 +1482,8 @@ void ChartWidget::setZoomedChartData(
ranges::find(_chartData.x, x)); ranges::find(_chartData.x, x));
customHeader->setTitle(_header->title()); customHeader->setTitle(_header->title());
if ((xIndex >= 0) && (xIndex < _chartData.x.size())) { if ((xIndex >= 0) && (xIndex < _chartData.x.size())) {
customHeader->setRightInfo(_chartData.getDayString(xIndex)); customHeader->setRightInfo(
HeaderRightInfo(_chartData, xIndex, xIndex));
} }
const auto &headerPadding = st::statisticsChartHeaderPadding; const auto &headerPadding = st::statisticsChartHeaderPadding;
customHeader->moveToLeft(headerPadding.left(), headerPadding.top()); customHeader->moveToLeft(headerPadding.left(), headerPadding.top());

View file

@ -127,8 +127,8 @@ PointDetailsWidget::PointDetailsWidget(
, _chartData(chartData) , _chartData(chartData)
, _textStyle(st::statisticsDetailsPopupStyle) , _textStyle(st::statisticsDetailsPopupStyle)
, _headerStyle(st::statisticsDetailsPopupHeaderStyle) , _headerStyle(st::statisticsDetailsPopupHeaderStyle)
, _longFormat(u"ddd, MMM d hh:mm"_q) , _longFormat(u"ddd, d MMM hh:mm"_q)
, _shortFormat(u"ddd, MMM d"_q) { , _shortFormat(u"ddd, d MMM yyyy"_q) {
if (zoomEnabled) { if (zoomEnabled) {
rpl::single(rpl::empty_value()) | rpl::then( rpl::single(rpl::empty_value()) | rpl::then(
@ -232,9 +232,15 @@ void PointDetailsWidget::setXIndex(int xIndex) {
if (xIndex < 0) { if (xIndex < 0) {
return; return;
} }
_header.setText( {
_headerStyle, constexpr auto kOneDay = 3600 * 24 * 1000;
FormatTimestamp(_chartData.x[xIndex], _longFormat, _shortFormat)); const auto timestamp = _chartData.x[xIndex];
_header.setText(
_headerStyle,
(timestamp < kOneDay)
? _chartData.getDayString(xIndex)
: FormatTimestamp(timestamp, _longFormat, _shortFormat));
}
_lines.clear(); _lines.clear();
_lines.reserve(_chartData.lines.size()); _lines.reserve(_chartData.lines.size());