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 | |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 21 | enum PlotStyle { LINE_GRAPH, LINE_DOT_GRAPH, BAR_GRAPH }; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 22 | |
| 23 | struct TimeSeriesPoint { |
| 24 | TimeSeriesPoint(float x, float y) : x(x), y(y) {} |
| 25 | float x; |
| 26 | float y; |
| 27 | }; |
| 28 | |
| 29 | struct TimeSeries { |
| 30 | TimeSeries() = default; |
| 31 | TimeSeries(TimeSeries&& other) |
| 32 | : label(std::move(other.label)), |
| 33 | style(other.style), |
| 34 | points(std::move(other.points)) {} |
| 35 | TimeSeries& operator=(TimeSeries&& other) { |
| 36 | label = std::move(other.label); |
| 37 | style = other.style; |
| 38 | points = std::move(other.points); |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | std::string label; |
| 43 | PlotStyle style; |
| 44 | std::vector<TimeSeriesPoint> points; |
| 45 | }; |
| 46 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 47 | // A container that represents a general graph, with axes, title and one or |
| 48 | // more data series. A subclass should define the output format by overriding |
| 49 | // the Draw() method. |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 50 | class Plot { |
| 51 | public: |
| 52 | virtual ~Plot() {} |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 53 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 54 | // Overloaded to draw the plot. |
| 55 | virtual void Draw() = 0; |
| 56 | |
| 57 | // Sets the lower x-axis limit to min_value (if left_margin == 0). |
| 58 | // Sets the upper x-axis limit to max_value (if right_margin == 0). |
| 59 | // The margins are measured as fractions of the interval |
| 60 | // (max_value - min_value) and are added to either side of the plot. |
| 61 | void SetXAxis(float min_value, |
| 62 | float max_value, |
| 63 | std::string label, |
| 64 | float left_margin = 0, |
| 65 | float right_margin = 0); |
| 66 | |
| 67 | // Sets the lower and upper x-axis limits based on min_value and max_value, |
| 68 | // but modified such that all points in the data series can be represented |
| 69 | // on the x-axis. The margins are measured as fractions of the range of |
| 70 | // x-values and are added to either side of the plot. |
| 71 | void SetSuggestedXAxis(float min_value, |
| 72 | float max_value, |
| 73 | std::string label, |
| 74 | float left_margin = 0, |
| 75 | float right_margin = 0); |
| 76 | |
| 77 | // Sets the lower y-axis limit to min_value (if bottom_margin == 0). |
| 78 | // Sets the upper y-axis limit to max_value (if top_margin == 0). |
| 79 | // The margins are measured as fractions of the interval |
| 80 | // (max_value - min_value) and are added to either side of the plot. |
| 81 | void SetYAxis(float min_value, |
| 82 | float max_value, |
| 83 | std::string label, |
| 84 | float bottom_margin = 0, |
| 85 | float top_margin = 0); |
| 86 | |
| 87 | // Sets the lower and upper y-axis limits based on min_value and max_value, |
| 88 | // but modified such that all points in the data series can be represented |
| 89 | // on the y-axis. The margins are measured as fractions of the range of |
| 90 | // y-values and are added to either side of the plot. |
| 91 | void SetSuggestedYAxis(float min_value, |
| 92 | float max_value, |
| 93 | std::string label, |
| 94 | float bottom_margin = 0, |
| 95 | float top_margin = 0); |
| 96 | |
| 97 | // Sets the title of the plot. |
| 98 | void SetTitle(std::string title); |
| 99 | |
| 100 | std::vector<TimeSeries> series_list_; |
| 101 | |
| 102 | protected: |
| 103 | float xaxis_min_; |
| 104 | float xaxis_max_; |
| 105 | std::string xaxis_label_; |
| 106 | float yaxis_min_; |
| 107 | float yaxis_max_; |
| 108 | std::string yaxis_label_; |
| 109 | std::string title_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class PlotCollection { |
| 113 | public: |
| 114 | virtual ~PlotCollection() {} |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 115 | virtual void Draw() = 0; |
| 116 | virtual Plot* AppendNewPlot() = 0; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 117 | |
| 118 | protected: |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 119 | std::vector<std::unique_ptr<Plot> > plots_; |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace plotting |
| 123 | } // namespace webrtc |
| 124 | |
| 125 | #endif // WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_ |