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 | */ |
| 10 | #ifndef WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |
| 11 | #define WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |
| 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 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 56 | // A container that represents a general graph, with axes, title and one or |
| 57 | // more data series. A subclass should define the output format by overriding |
| 58 | // the Draw() method. |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 59 | class Plot { |
| 60 | public: |
| 61 | virtual ~Plot() {} |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 62 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 63 | // Overloaded to draw the plot. |
| 64 | virtual void Draw() = 0; |
| 65 | |
| 66 | // Sets the lower x-axis limit to min_value (if left_margin == 0). |
| 67 | // Sets the upper x-axis limit to max_value (if right_margin == 0). |
| 68 | // The margins are measured as fractions of the interval |
| 69 | // (max_value - min_value) and are added to either side of the plot. |
| 70 | void SetXAxis(float min_value, |
| 71 | float max_value, |
| 72 | std::string label, |
| 73 | float left_margin = 0, |
| 74 | float right_margin = 0); |
| 75 | |
| 76 | // Sets the lower and upper x-axis limits based on min_value and max_value, |
| 77 | // but modified such that all points in the data series can be represented |
| 78 | // on the x-axis. The margins are measured as fractions of the range of |
| 79 | // x-values and are added to either side of the plot. |
| 80 | void SetSuggestedXAxis(float min_value, |
| 81 | float max_value, |
| 82 | std::string label, |
| 83 | float left_margin = 0, |
| 84 | float right_margin = 0); |
| 85 | |
| 86 | // Sets the lower y-axis limit to min_value (if bottom_margin == 0). |
| 87 | // Sets the upper y-axis limit to max_value (if top_margin == 0). |
| 88 | // The margins are measured as fractions of the interval |
| 89 | // (max_value - min_value) and are added to either side of the plot. |
| 90 | void SetYAxis(float min_value, |
| 91 | float max_value, |
| 92 | std::string label, |
| 93 | float bottom_margin = 0, |
| 94 | float top_margin = 0); |
| 95 | |
| 96 | // Sets the lower and upper y-axis limits based on min_value and max_value, |
| 97 | // but modified such that all points in the data series can be represented |
| 98 | // on the y-axis. The margins are measured as fractions of the range of |
| 99 | // y-values and are added to either side of the plot. |
| 100 | void SetSuggestedYAxis(float min_value, |
| 101 | float max_value, |
| 102 | std::string label, |
| 103 | float bottom_margin = 0, |
| 104 | float top_margin = 0); |
| 105 | |
| 106 | // Sets the title of the plot. |
| 107 | void SetTitle(std::string title); |
| 108 | |
terelius | 23c595a | 2017-03-15 01:59:12 -0700 | [diff] [blame] | 109 | // Add a new TimeSeries to the plot. |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame^] | 110 | void AppendTimeSeries(TimeSeries&& time_series); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 111 | |
| 112 | protected: |
| 113 | float xaxis_min_; |
| 114 | float xaxis_max_; |
| 115 | std::string xaxis_label_; |
| 116 | float yaxis_min_; |
| 117 | float yaxis_max_; |
| 118 | std::string yaxis_label_; |
| 119 | std::string title_; |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame^] | 120 | std::vector<TimeSeries> series_list_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | class PlotCollection { |
| 124 | public: |
| 125 | virtual ~PlotCollection() {} |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 126 | virtual void Draw() = 0; |
| 127 | virtual Plot* AppendNewPlot() = 0; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 128 | |
| 129 | protected: |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 130 | std::vector<std::unique_ptr<Plot> > plots_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace plotting |
| 134 | } // namespace webrtc |
| 135 | |
| 136 | #endif // WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |