abstract_chart_view.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "statistics/statistics_common.h"
  8. #include "statistics/view/abstract_chart_view.h"
  9. #include "data/data_statistics_chart.h"
  10. #include "statistics/chart_lines_filter_controller.h"
  11. #include "statistics/statistics_types.h"
  12. namespace Statistic {
  13. bool CachedSelectedPoints::isSame(int x, const PaintContext &c) const {
  14. return (lastXIndex == x)
  15. && (lastHeightLimits.min == c.heightLimits.min)
  16. && (lastHeightLimits.max == c.heightLimits.max)
  17. && (lastXLimits.min == c.xPercentageLimits.min)
  18. && (lastXLimits.max == c.xPercentageLimits.max);
  19. }
  20. DoubleLineRatios::DoubleLineRatios(bool isDouble) {
  21. first = second = (isDouble ? 0 : 1);
  22. }
  23. void DoubleLineRatios::init(const Data::StatisticalChart &chartData) {
  24. if (chartData.lines.size() != 2) {
  25. first = 1.;
  26. second = 1.;
  27. } else {
  28. const auto firstMax = chartData.lines.front().maxValue;
  29. const auto secondMax = chartData.lines.back().maxValue;
  30. if (firstMax > secondMax) {
  31. first = 1.;
  32. second = firstMax / float64(secondMax);
  33. } else {
  34. first = secondMax / float64(firstMax);
  35. second = 1.;
  36. }
  37. }
  38. }
  39. float64 DoubleLineRatios::ratio(int lineId) const {
  40. return (lineId == 1) ? first : second;
  41. }
  42. void AbstractChartView::setUpdateCallback(Fn<void()> callback) {
  43. _updateCallback = std::move(callback);
  44. }
  45. void AbstractChartView::update() {
  46. if (_updateCallback) {
  47. _updateCallback();
  48. }
  49. }
  50. void AbstractChartView::setLinesFilterController(
  51. std::shared_ptr<LinesFilterController> c) {
  52. _linesFilterController = std::move(c);
  53. }
  54. auto AbstractChartView::linesFilterController() const
  55. -> std::shared_ptr<LinesFilterController> {
  56. return _linesFilterController;
  57. }
  58. AbstractChartView::HeightLimits DefaultHeightLimits(
  59. const DoubleLineRatios &ratios,
  60. const std::shared_ptr<LinesFilterController> &linesFilter,
  61. Data::StatisticalChart &chartData,
  62. Limits xIndices) {
  63. auto minValue = std::numeric_limits<ChartValue>::max();
  64. auto maxValue = ChartValue(0);
  65. auto minValueFull = std::numeric_limits<ChartValue>::max();
  66. auto maxValueFull = ChartValue(0);
  67. for (auto &l : chartData.lines) {
  68. if (!linesFilter->isEnabled(l.id)) {
  69. continue;
  70. }
  71. const auto r = ratios.ratio(l.id);
  72. const auto lineMax = l.segmentTree.rMaxQ(xIndices.min, xIndices.max);
  73. const auto lineMin = l.segmentTree.rMinQ(xIndices.min, xIndices.max);
  74. maxValue = std::max(ChartValue(lineMax * r), maxValue);
  75. minValue = std::min(ChartValue(lineMin * r), minValue);
  76. maxValueFull = std::max(ChartValue(l.maxValue * r), maxValueFull);
  77. minValueFull = std::min(ChartValue(l.minValue * r), minValueFull);
  78. }
  79. if (maxValue == minValue) {
  80. maxValue = chartData.maxValue;
  81. minValue = chartData.minValue;
  82. }
  83. return {
  84. .full = Limits{ float64(minValueFull), float64(maxValueFull) },
  85. .ranged = Limits{ float64(minValue), float64(maxValue) },
  86. };
  87. }
  88. } // namespace Statistic