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());
}
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<QString> &&title) {
void ChartWidget::setZoomedChartData(
Data::StatisticalChart chartData,
float64 x) {
float64 x,
ChartViewType type) {
_zoomedChartWidget = base::make_unique_q<ChartWidget>(
dynamic_cast<Ui::RpWidget*>(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());

View File

@ -25,9 +25,12 @@ class ChartWidget : public Ui::RpWidget {
public:
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 setZoomedChartData(Data::StatisticalChart chartData, float64 x);
void setZoomedChartData(
Data::StatisticalChart chartData,
float64 x,
ChartViewType type);
void addHorizontalLine(Limits newHeight, bool animated);
[[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 "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<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto processZoom = [=](
not_null<Statistic::ChartWidget*> 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<Ui::GenericBox*> box, not_null<PeerData*> 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<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto processChart = [=](
not_null<Statistic::ChartWidget*> widget,
const Data::StatisticalGraph &graphData,
rpl::producer<QString> &&title) {
rpl::producer<QString> &&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<Ui::GenericBox*> box, not_null<PeerData*> 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<Ui::GenericBox*> box, not_null<PeerData*> 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());
}