terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |
| 11 | #define RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace webrtc { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 19 | |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 20 | enum class LineStyle { |
| 21 | kNone, // No line connecting the points. Used to create scatter plots. |
| 22 | kLine, // Straight line between consecutive points. |
| 23 | kStep, // Horizontal line until the next value. Used for state changes. |
| 24 | kBar // Vertical bars from the x-axis to the point. |
| 25 | }; |
| 26 | |
| 27 | enum class PointStyle { |
| 28 | kNone, // Don't draw the points. |
| 29 | kHighlight // Draw circles or dots to highlight the points. |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 30 | }; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 31 | |
| 32 | struct TimeSeriesPoint { |
| 33 | TimeSeriesPoint(float x, float y) : x(x), y(y) {} |
| 34 | float x; |
| 35 | float y; |
| 36 | }; |
| 37 | |
| 38 | struct TimeSeries { |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 39 | TimeSeries() = default; // TODO(terelius): Remove the default constructor. |
| 40 | TimeSeries(const char* label, |
| 41 | LineStyle line_style, |
| 42 | PointStyle point_style = PointStyle::kNone) |
| 43 | : label(label), line_style(line_style), point_style(point_style) {} |
| 44 | TimeSeries(const std::string& label, |
| 45 | LineStyle line_style, |
| 46 | PointStyle point_style = PointStyle::kNone) |
| 47 | : label(label), line_style(line_style), point_style(point_style) {} |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 48 | TimeSeries(TimeSeries&& other) |
| 49 | : label(std::move(other.label)), |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 50 | line_style(other.line_style), |
| 51 | point_style(other.point_style), |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 52 | points(std::move(other.points)) {} |
| 53 | TimeSeries& operator=(TimeSeries&& other) { |
| 54 | label = std::move(other.label); |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 55 | line_style = other.line_style; |
| 56 | point_style = other.point_style; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 57 | points = std::move(other.points); |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | std::string label; |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 62 | LineStyle line_style = LineStyle::kLine; |
| 63 | PointStyle point_style = PointStyle::kNone; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 64 | std::vector<TimeSeriesPoint> points; |
| 65 | }; |
| 66 | |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 67 | struct Interval { |
| 68 | Interval() = default; |
| 69 | Interval(double begin, double end) : begin(begin), end(end) {} |
| 70 | |
| 71 | double begin; |
| 72 | double end; |
| 73 | }; |
| 74 | |
| 75 | struct IntervalSeries { |
| 76 | enum Orientation { kHorizontal, kVertical }; |
| 77 | |
| 78 | IntervalSeries() = default; |
| 79 | IntervalSeries(const std::string& label, |
| 80 | const std::string& color, |
| 81 | IntervalSeries::Orientation orientation) |
| 82 | : label(label), color(color), orientation(orientation) {} |
| 83 | |
| 84 | std::string label; |
| 85 | std::string color; |
| 86 | Orientation orientation; |
| 87 | std::vector<Interval> intervals; |
| 88 | }; |
| 89 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 90 | // A container that represents a general graph, with axes, title and one or |
| 91 | // more data series. A subclass should define the output format by overriding |
| 92 | // the Draw() method. |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 93 | class Plot { |
| 94 | public: |
| 95 | virtual ~Plot() {} |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 96 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 97 | // Overloaded to draw the plot. |
| 98 | virtual void Draw() = 0; |
| 99 | |
| 100 | // Sets the lower x-axis limit to min_value (if left_margin == 0). |
| 101 | // Sets the upper x-axis limit to max_value (if right_margin == 0). |
| 102 | // The margins are measured as fractions of the interval |
| 103 | // (max_value - min_value) and are added to either side of the plot. |
| 104 | void SetXAxis(float min_value, |
| 105 | float max_value, |
| 106 | std::string label, |
| 107 | float left_margin = 0, |
| 108 | float right_margin = 0); |
| 109 | |
| 110 | // Sets the lower and upper x-axis limits based on min_value and max_value, |
| 111 | // but modified such that all points in the data series can be represented |
| 112 | // on the x-axis. The margins are measured as fractions of the range of |
| 113 | // x-values and are added to either side of the plot. |
| 114 | void SetSuggestedXAxis(float min_value, |
| 115 | float max_value, |
| 116 | std::string label, |
| 117 | float left_margin = 0, |
| 118 | float right_margin = 0); |
| 119 | |
| 120 | // Sets the lower y-axis limit to min_value (if bottom_margin == 0). |
| 121 | // Sets the upper y-axis limit to max_value (if top_margin == 0). |
| 122 | // The margins are measured as fractions of the interval |
| 123 | // (max_value - min_value) and are added to either side of the plot. |
| 124 | void SetYAxis(float min_value, |
| 125 | float max_value, |
| 126 | std::string label, |
| 127 | float bottom_margin = 0, |
| 128 | float top_margin = 0); |
| 129 | |
| 130 | // Sets the lower and upper y-axis limits based on min_value and max_value, |
| 131 | // but modified such that all points in the data series can be represented |
| 132 | // on the y-axis. The margins are measured as fractions of the range of |
| 133 | // y-values and are added to either side of the plot. |
| 134 | void SetSuggestedYAxis(float min_value, |
| 135 | float max_value, |
| 136 | std::string label, |
| 137 | float bottom_margin = 0, |
| 138 | float top_margin = 0); |
| 139 | |
| 140 | // Sets the title of the plot. |
Bjorn Terelius | 1aa9ee9 | 2019-06-11 17:39:38 +0200 | [diff] [blame] | 141 | void SetTitle(const std::string& title); |
| 142 | |
| 143 | // Sets an unique ID for the plot. The ID is similar to the title except that |
| 144 | // the title might change in future releases whereas the ID should be stable |
| 145 | // over time. |
| 146 | void SetId(const std::string& id); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 147 | |
terelius | 23c595a | 2017-03-15 01:59:12 -0700 | [diff] [blame] | 148 | // Add a new TimeSeries to the plot. |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame] | 149 | void AppendTimeSeries(TimeSeries&& time_series); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 150 | |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 151 | // Add a new IntervalSeries to the plot. |
| 152 | void AppendIntervalSeries(IntervalSeries&& interval_series); |
| 153 | |
terelius | 2c8e8a3 | 2017-06-02 01:29:48 -0700 | [diff] [blame] | 154 | // Add a new TimeSeries to the plot if the series contains contains data. |
| 155 | // Otherwise, the call has no effect and the timeseries is destroyed. |
| 156 | void AppendTimeSeriesIfNotEmpty(TimeSeries&& time_series); |
| 157 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 158 | protected: |
| 159 | float xaxis_min_; |
| 160 | float xaxis_max_; |
| 161 | std::string xaxis_label_; |
| 162 | float yaxis_min_; |
| 163 | float yaxis_max_; |
| 164 | std::string yaxis_label_; |
| 165 | std::string title_; |
Bjorn Terelius | 1aa9ee9 | 2019-06-11 17:39:38 +0200 | [diff] [blame] | 166 | std::string id_; |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame] | 167 | std::vector<TimeSeries> series_list_; |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 168 | std::vector<IntervalSeries> interval_list_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class PlotCollection { |
| 172 | public: |
| 173 | virtual ~PlotCollection() {} |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 174 | virtual void Draw() = 0; |
| 175 | virtual Plot* AppendNewPlot() = 0; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 176 | |
| 177 | protected: |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 178 | std::vector<std::unique_ptr<Plot> > plots_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 181 | } // namespace webrtc |
| 182 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 183 | #endif // RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |