blob: 318d425d41c2432d531a3f53228a39e47f5aae07 [file] [log] [blame]
terelius54ce6802016-07-13 06:44:41 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_
11#define RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_
terelius54ce6802016-07-13 06:44:41 -070012
13#include <memory>
14#include <string>
15#include <utility>
16#include <vector>
17
18namespace webrtc {
19namespace plotting {
20
philipele127e7a2017-03-29 16:28:53 +020021enum PlotStyle {
22 LINE_GRAPH,
23 LINE_DOT_GRAPH,
24 BAR_GRAPH,
25 LINE_STEP_GRAPH,
26 DOT_GRAPH
27};
terelius54ce6802016-07-13 06:44:41 -070028
29struct TimeSeriesPoint {
30 TimeSeriesPoint(float x, float y) : x(x), y(y) {}
31 float x;
32 float y;
33};
34
35struct TimeSeries {
36 TimeSeries() = default;
philipel35ba9bd2017-04-19 05:58:51 -070037 TimeSeries(const char* label, PlotStyle style) : label(label), style(style) {}
terelius23c595a2017-03-15 01:59:12 -070038 TimeSeries(const std::string& label, PlotStyle style)
philipel35ba9bd2017-04-19 05:58:51 -070039 : label(label), style(style) {}
terelius54ce6802016-07-13 06:44:41 -070040 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
philipel23c7f252017-07-14 06:30:03 -070056struct Interval {
57 Interval() = default;
58 Interval(double begin, double end) : begin(begin), end(end) {}
59
60 double begin;
61 double end;
62};
63
64struct 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
tereliusdc35dcd2016-08-01 12:03:27 -070079// 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.
terelius54ce6802016-07-13 06:44:41 -070082class Plot {
83 public:
84 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070085
tereliusdc35dcd2016-08-01 12:03:27 -070086 // 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
terelius23c595a2017-03-15 01:59:12 -0700132 // Add a new TimeSeries to the plot.
philipel35ba9bd2017-04-19 05:58:51 -0700133 void AppendTimeSeries(TimeSeries&& time_series);
tereliusdc35dcd2016-08-01 12:03:27 -0700134
philipel23c7f252017-07-14 06:30:03 -0700135 // Add a new IntervalSeries to the plot.
136 void AppendIntervalSeries(IntervalSeries&& interval_series);
137
terelius2c8e8a32017-06-02 01:29:48 -0700138 // 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
tereliusdc35dcd2016-08-01 12:03:27 -0700142 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_;
philipel35ba9bd2017-04-19 05:58:51 -0700150 std::vector<TimeSeries> series_list_;
philipel23c7f252017-07-14 06:30:03 -0700151 std::vector<IntervalSeries> interval_list_;
terelius54ce6802016-07-13 06:44:41 -0700152};
153
154class PlotCollection {
155 public:
156 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700157 virtual void Draw() = 0;
158 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700159
160 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700161 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700162};
163
164} // namespace plotting
165} // namespace webrtc
166
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200167#endif // RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_