Added support of different chart types in chart widget.

This commit is contained in:
23rd 2023-09-08 16:27:29 +03:00 committed by John Preston
parent 54fecd497e
commit a0226f9789
3 changed files with 51 additions and 35 deletions

View file

@ -1274,11 +1274,12 @@ void ChartWidget::setupFilterButtons() {
}, _filterButtons->lifetime()); }, _filterButtons->lifetime());
} }
void ChartWidget::setChartData(Data::StatisticalChart chartData) { void ChartWidget::setChartData(
Data::StatisticalChart chartData,
ChartViewType type) {
_chartData = std::move(chartData); _chartData = std::move(chartData);
// _chartView = CreateChartView(ChartViewType::Linear); _chartView = CreateChartView(type);
_chartView = CreateChartView(ChartViewType::Stack);
setupDetails(); setupDetails();
setupFilterButtons(); setupFilterButtons();
@ -1311,10 +1312,11 @@ void ChartWidget::setTitle(rpl::producer<QString> &&title) {
void ChartWidget::setZoomedChartData( void ChartWidget::setZoomedChartData(
Data::StatisticalChart chartData, Data::StatisticalChart chartData,
float64 x) { float64 x,
ChartViewType type) {
_zoomedChartWidget = base::make_unique_q<ChartWidget>( _zoomedChartWidget = base::make_unique_q<ChartWidget>(
dynamic_cast<Ui::RpWidget*>(parentWidget())); dynamic_cast<Ui::RpWidget*>(parentWidget()));
_zoomedChartWidget->setChartData(std::move(chartData)); _zoomedChartWidget->setChartData(std::move(chartData), type);
geometryValue( geometryValue(
) | rpl::start_with_next([=](const QRect &geometry) { ) | rpl::start_with_next([=](const QRect &geometry) {
_zoomedChartWidget->moveToLeft(geometry.x(), geometry.y()); _zoomedChartWidget->moveToLeft(geometry.x(), geometry.y());

View file

@ -25,9 +25,12 @@ class ChartWidget : public Ui::RpWidget {
public: public:
ChartWidget(not_null<Ui::RpWidget*> parent); ChartWidget(not_null<Ui::RpWidget*> parent);
void setChartData(Data::StatisticalChart chartData); void setChartData(Data::StatisticalChart chartData, ChartViewType type);
void setTitle(rpl::producer<QString> &&title); void setTitle(rpl::producer<QString> &&title);
void setZoomedChartData(Data::StatisticalChart chartData, float64 x); void setZoomedChartData(
Data::StatisticalChart chartData,
float64 x,
ChartViewType type);
void addHorizontalLine(Limits newHeight, bool animated); void addHorizontalLine(Limits newHeight, bool animated);
[[nodiscard]] rpl::producer<float64> zoomRequests(); [[nodiscard]] rpl::producer<float64> zoomRequests();

View file

@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "statistics/chart_widget.h" #include "statistics/chart_widget.h"
#include "statistics/statistics_common.h"
#include "ui/toast/toast.h" #include "ui/toast/toast.h"
#include "ui/layers/generic_box.h" #include "ui/layers/generic_box.h"
@ -34,7 +35,8 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto processZoom = [=]( const auto processZoom = [=](
not_null<Statistic::ChartWidget*> widget, not_null<Statistic::ChartWidget*> widget,
const QString &zoomToken) { const QString &zoomToken,
Statistic::ChartViewType type) {
if (!zoomToken.isEmpty()) { if (!zoomToken.isEmpty()) {
widget->zoomRequests( widget->zoomRequests(
) | rpl::start_with_next([=](float64 x) { ) | rpl::start_with_next([=](float64 x) {
@ -45,7 +47,7 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
) | rpl::start_with_next_error_done([=]( ) | rpl::start_with_next_error_done([=](
const Data::StatisticalGraph &graph) { const Data::StatisticalGraph &graph) {
if (graph.chart) { if (graph.chart) {
widget->setZoomedChartData(graph.chart, x); widget->setZoomedChartData(graph.chart, x, type);
} else if (!graph.error.isEmpty()) { } else if (!graph.error.isEmpty()) {
Ui::Toast::Show( Ui::Toast::Show(
box->uiShow()->toastParent(), box->uiShow()->toastParent(),
@ -61,10 +63,11 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto processChart = [=]( const auto processChart = [=](
not_null<Statistic::ChartWidget*> widget, not_null<Statistic::ChartWidget*> widget,
const Data::StatisticalGraph &graphData, const Data::StatisticalGraph &graphData,
rpl::producer<QString> &&title) { rpl::producer<QString> &&title,
Statistic::ChartViewType type) {
if (graphData.chart) { if (graphData.chart) {
widget->setChartData(graphData.chart); widget->setChartData(graphData.chart, type);
processZoom(widget, graphData.zoomToken); processZoom(widget, graphData.zoomToken, type);
widget->setTitle(std::move(title)); widget->setTitle(std::move(title));
} else if (!graphData.zoomToken.isEmpty()) { } else if (!graphData.zoomToken.isEmpty()) {
api->requestZoom( api->requestZoom(
@ -74,8 +77,8 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
) | rpl::start_with_next_error_done([=]( ) | rpl::start_with_next_error_done([=](
const Data::StatisticalGraph &graph) { const Data::StatisticalGraph &graph) {
if (graph.chart) { if (graph.chart) {
widget->setChartData(graph.chart); widget->setChartData(graph.chart, type);
processZoom(widget, graph.zoomToken); processZoom(widget, graph.zoomToken, type);
widget->setTitle(rpl::duplicate(title)); widget->setTitle(rpl::duplicate(title));
} else if (!graph.error.isEmpty()) { } else if (!graph.error.isEmpty()) {
Ui::Toast::Show( Ui::Toast::Show(
@ -91,28 +94,36 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
api->request( api->request(
peer peer
) | rpl::start_with_done([=] { ) | rpl::start_with_done([=] {
if (const auto stats = api->channelStats()) { const auto stats = api->channelStats();
processChart( if (!stats) {
chartWidget, return;
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());
} }
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()); }, chartWidget->lifetime());
box->setTitle(tr::lng_stats_title()); box->setTitle(tr::lng_stats_title());
} }