From a0226f97893bad701361c45513e25d9c8420db88 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 8 Sep 2023 16:27:29 +0300 Subject: [PATCH] Added support of different chart types in chart widget. --- .../SourceFiles/statistics/chart_widget.cpp | 12 ++-- .../SourceFiles/statistics/chart_widget.h | 7 +- .../SourceFiles/statistics/statistics_box.cpp | 67 +++++++++++-------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/Telegram/SourceFiles/statistics/chart_widget.cpp b/Telegram/SourceFiles/statistics/chart_widget.cpp index 0dad2008b9..86f94a9d50 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.cpp +++ b/Telegram/SourceFiles/statistics/chart_widget.cpp @@ -1274,11 +1274,12 @@ void ChartWidget::setupFilterButtons() { }, _filterButtons->lifetime()); } -void ChartWidget::setChartData(Data::StatisticalChart chartData) { +void ChartWidget::setChartData( + Data::StatisticalChart chartData, + ChartViewType type) { _chartData = std::move(chartData); - // _chartView = CreateChartView(ChartViewType::Linear); - _chartView = CreateChartView(ChartViewType::Stack); + _chartView = CreateChartView(type); setupDetails(); setupFilterButtons(); @@ -1311,10 +1312,11 @@ void ChartWidget::setTitle(rpl::producer &&title) { void ChartWidget::setZoomedChartData( Data::StatisticalChart chartData, - float64 x) { + float64 x, + ChartViewType type) { _zoomedChartWidget = base::make_unique_q( dynamic_cast(parentWidget())); - _zoomedChartWidget->setChartData(std::move(chartData)); + _zoomedChartWidget->setChartData(std::move(chartData), type); geometryValue( ) | rpl::start_with_next([=](const QRect &geometry) { _zoomedChartWidget->moveToLeft(geometry.x(), geometry.y()); diff --git a/Telegram/SourceFiles/statistics/chart_widget.h b/Telegram/SourceFiles/statistics/chart_widget.h index 323f67a4fa..56d858b4f2 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.h +++ b/Telegram/SourceFiles/statistics/chart_widget.h @@ -25,9 +25,12 @@ class ChartWidget : public Ui::RpWidget { public: ChartWidget(not_null parent); - void setChartData(Data::StatisticalChart chartData); + void setChartData(Data::StatisticalChart chartData, ChartViewType type); void setTitle(rpl::producer &&title); - void setZoomedChartData(Data::StatisticalChart chartData, float64 x); + void setZoomedChartData( + Data::StatisticalChart chartData, + float64 x, + ChartViewType type); void addHorizontalLine(Limits newHeight, bool animated); [[nodiscard]] rpl::producer zoomRequests(); diff --git a/Telegram/SourceFiles/statistics/statistics_box.cpp b/Telegram/SourceFiles/statistics/statistics_box.cpp index c93961d156..144ffb9f44 100644 --- a/Telegram/SourceFiles/statistics/statistics_box.cpp +++ b/Telegram/SourceFiles/statistics/statistics_box.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "main/main_session.h" #include "statistics/chart_widget.h" +#include "statistics/statistics_common.h" #include "ui/toast/toast.h" #include "ui/layers/generic_box.h" @@ -34,7 +35,8 @@ void StatisticsBox(not_null box, not_null peer) { const auto processZoom = [=]( not_null widget, - const QString &zoomToken) { + const QString &zoomToken, + Statistic::ChartViewType type) { if (!zoomToken.isEmpty()) { widget->zoomRequests( ) | rpl::start_with_next([=](float64 x) { @@ -45,7 +47,7 @@ void StatisticsBox(not_null box, not_null peer) { ) | rpl::start_with_next_error_done([=]( const Data::StatisticalGraph &graph) { if (graph.chart) { - widget->setZoomedChartData(graph.chart, x); + widget->setZoomedChartData(graph.chart, x, type); } else if (!graph.error.isEmpty()) { Ui::Toast::Show( box->uiShow()->toastParent(), @@ -61,10 +63,11 @@ void StatisticsBox(not_null box, not_null peer) { const auto processChart = [=]( not_null widget, const Data::StatisticalGraph &graphData, - rpl::producer &&title) { + rpl::producer &&title, + Statistic::ChartViewType type) { if (graphData.chart) { - widget->setChartData(graphData.chart); - processZoom(widget, graphData.zoomToken); + widget->setChartData(graphData.chart, type); + processZoom(widget, graphData.zoomToken, type); widget->setTitle(std::move(title)); } else if (!graphData.zoomToken.isEmpty()) { api->requestZoom( @@ -74,8 +77,8 @@ void StatisticsBox(not_null box, not_null peer) { ) | rpl::start_with_next_error_done([=]( const Data::StatisticalGraph &graph) { if (graph.chart) { - widget->setChartData(graph.chart); - processZoom(widget, graph.zoomToken); + widget->setChartData(graph.chart, type); + processZoom(widget, graph.zoomToken, type); widget->setTitle(rpl::duplicate(title)); } else if (!graph.error.isEmpty()) { Ui::Toast::Show( @@ -91,28 +94,36 @@ void StatisticsBox(not_null box, not_null peer) { api->request( peer ) | rpl::start_with_done([=] { - if (const auto stats = api->channelStats()) { - processChart( - chartWidget, - stats.memberCountGraph, - tr::lng_chart_title_member_count()); - processChart( - chartWidget2, - stats.joinGraph, - tr::lng_chart_title_join()); - processChart( - chartWidget3, - stats.muteGraph, - tr::lng_chart_title_mute()); - processChart( - chartWidget4, - stats.viewCountBySourceGraph, - tr::lng_chart_title_view_count_by_source()); - processChart( - chartWidget5, - stats.joinBySourceGraph, - tr::lng_chart_title_join_by_source()); + const auto stats = api->channelStats(); + if (!stats) { + return; } + using Type = Statistic::ChartViewType; + processChart( + chartWidget, + stats.memberCountGraph, + tr::lng_chart_title_member_count(), + Type::Linear); + processChart( + chartWidget2, + stats.joinGraph, + tr::lng_chart_title_join(), + Type::Linear); + processChart( + chartWidget3, + stats.muteGraph, + tr::lng_chart_title_mute(), + Type::Linear); + processChart( + chartWidget4, + stats.viewCountBySourceGraph, + tr::lng_chart_title_view_count_by_source(), + Type::Stack); + processChart( + chartWidget5, + stats.joinBySourceGraph, + tr::lng_chart_title_join_by_source(), + Type::Stack); }, chartWidget->lifetime()); box->setTitle(tr::lng_stats_title()); }