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