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