Added initial support of zooming single chart to chart widget.

This commit is contained in:
23rd 2023-07-27 07:08:05 +03:00 committed by John Preston
parent 7b921dea3b
commit c9a976bf87
3 changed files with 74 additions and 1 deletions

View File

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "statistics/chart_widget.h"
#include "ui/effects/show_animation.h"
#include "base/qt/qt_key_modifiers.h"
#include "statistics/chart_lines_filter_widget.h"
#include "statistics/linear_chart_view.h"
@ -18,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/round_rect.h"
#include "ui/effects/ripple_animation.h"
#include "ui/image/image_prepare.h"
#include "ui/widgets/buttons.h"
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
#include "styles/style_statistics.h"
@ -1131,7 +1133,12 @@ void ChartWidget::setupDetails() {
this,
_chartData,
maxAbsoluteValue,
false);
_zoomEnabled);
_details.widget->setClickedCallback([=] {
if (const auto index = _details.widget->xIndex(); index >= 0) {
_zoomRequests.fire_copy(_chartData.x[index]);
}
});
_details.widget->shownValue(
) | rpl::start_with_next([=](bool shown) {
@ -1233,6 +1240,36 @@ void ChartWidget::setChartData(Data::StatisticalChart chartData) {
_footer->update();
}
void ChartWidget::setZoomedChartData(Data::StatisticalChart chartData) {
_zoomedChartWidget = base::make_unique_q<ChartWidget>(
dynamic_cast<Ui::RpWidget*>(parentWidget()));
_zoomedChartWidget->setChartData(std::move(chartData));
geometryValue(
) | rpl::start_with_next([=](const QRect &geometry) {
_zoomedChartWidget->moveToLeft(geometry.x(), geometry.y());
}, _zoomedChartWidget->lifetime());
_zoomedChartWidget->show();
_zoomedChartWidget->resizeToWidth(width());
const auto zoomOutButton = Ui::CreateChild<Ui::RoundButton>(
_zoomedChartWidget.get(),
rpl::single(QString("Zoom Out")),
st::defaultActiveButton);
Ui::Animations::ShowWidgets({ _zoomedChartWidget.get(), zoomOutButton });
Ui::Animations::HideWidgets({ this });
zoomOutButton->moveToLeft(0, 0);
zoomOutButton->setClickedCallback([=] {
shownValue(
) | rpl::start_with_next([=](bool shown) {
if (shown) {
_zoomedChartWidget = nullptr;
}
}, _zoomedChartWidget->lifetime());
Ui::Animations::ShowWidgets({ this });
Ui::Animations::HideWidgets({ _zoomedChartWidget.get() });
});
}
void ChartWidget::addHorizontalLine(Limits newHeight, bool animated) {
const auto newLinesData = ChartHorizontalLinesData(
newHeight.max,
@ -1250,4 +1287,10 @@ void ChartWidget::addHorizontalLine(Limits newHeight, bool animated) {
}
}
rpl::producer<float64> ChartWidget::zoomRequests() {
_zoomEnabled = true;
setupDetails();
return _zoomRequests.events();
}
} // namespace Statistic

View File

@ -26,8 +26,11 @@ public:
ChartWidget(not_null<Ui::RpWidget*> parent);
void setChartData(Data::StatisticalChart chartData);
void setZoomedChartData(Data::StatisticalChart chartData);
void addHorizontalLine(Limits newHeight, bool animated);
[[nodiscard]] rpl::producer<float64> zoomRequests();
struct BottomCaptionLineData final {
int step = 0;
int stepMax = 0;
@ -133,6 +136,8 @@ private:
base::unique_qptr<ChartLinesFilterWidget> _filterButtons;
Data::StatisticalChart _chartData;
base::unique_qptr<ChartWidget> _zoomedChartWidget;
std::unique_ptr<LinearChartView> _linearChartView;
struct {
@ -155,6 +160,9 @@ private:
std::vector<ChartHorizontalLinesData> _horizontalLines;
bool _zoomEnabled = false;
rpl::event_stream<float64> _zoomRequests;
};
} // namespace Statistic

View File

@ -23,6 +23,28 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto api = chartWidget->lifetime().make_state<Api::Statistics>(
&peer->session().api());
const auto processZoom = [=](
not_null<Statistic::ChartWidget*> widget,
const QString &zoomToken) {
if (!zoomToken.isEmpty()) {
widget->zoomRequests(
) | rpl::start_with_next([=](float64 x) {
api->requestZoom(
peer,
zoomToken,
x
) | rpl::start_with_next_error_done([=](
const Data::StatisticalGraph &graph) {
if (graph.chart) {
widget->setZoomedChartData(graph.chart);
}
}, [=](const QString &error) {
}, [=] {
}, widget->lifetime());
}, widget->lifetime());
}
};
api->request(
peer
) | rpl::start_with_done([=] {