blob: 72fa575ea690e466064658e70993ae8571b86f99 [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 {
terelius54ce6802016-07-13 06:44:41 -070019
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010020enum class LineStyle {
21 kNone, // No line connecting the points. Used to create scatter plots.
22 kLine, // Straight line between consecutive points.
23 kStep, // Horizontal line until the next value. Used for state changes.
24 kBar // Vertical bars from the x-axis to the point.
25};
26
27enum class PointStyle {
28 kNone, // Don't draw the points.
29 kHighlight // Draw circles or dots to highlight the points.
philipele127e7a2017-03-29 16:28:53 +020030};
terelius54ce6802016-07-13 06:44:41 -070031
32struct TimeSeriesPoint {
33 TimeSeriesPoint(float x, float y) : x(x), y(y) {}
34 float x;
35 float y;
36};
37
38struct TimeSeries {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010039 TimeSeries() = default; // TODO(terelius): Remove the default constructor.
40 TimeSeries(const char* label,
41 LineStyle line_style,
42 PointStyle point_style = PointStyle::kNone)
43 : label(label), line_style(line_style), point_style(point_style) {}
44 TimeSeries(const std::string& label,
45 LineStyle line_style,
46 PointStyle point_style = PointStyle::kNone)
47 : label(label), line_style(line_style), point_style(point_style) {}
terelius54ce6802016-07-13 06:44:41 -070048 TimeSeries(TimeSeries&& other)
49 : label(std::move(other.label)),
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010050 line_style(other.line_style),
51 point_style(other.point_style),
terelius54ce6802016-07-13 06:44:41 -070052 points(std::move(other.points)) {}
53 TimeSeries& operator=(TimeSeries&& other) {
54 label = std::move(other.label);
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010055 line_style = other.line_style;
56 point_style = other.point_style;
terelius54ce6802016-07-13 06:44:41 -070057 points = std::move(other.points);
58 return *this;
59 }
60
61 std::string label;
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010062 LineStyle line_style = LineStyle::kLine;
63 PointStyle point_style = PointStyle::kNone;
terelius54ce6802016-07-13 06:44:41 -070064 std::vector<TimeSeriesPoint> points;
65};
66
philipel23c7f252017-07-14 06:30:03 -070067struct Interval {
68 Interval() = default;
69 Interval(double begin, double end) : begin(begin), end(end) {}
70
71 double begin;
72 double end;
73};
74
75struct IntervalSeries {
76 enum Orientation { kHorizontal, kVertical };
77
78 IntervalSeries() = default;
79 IntervalSeries(const std::string& label,
80 const std::string& color,
81 IntervalSeries::Orientation orientation)
82 : label(label), color(color), orientation(orientation) {}
83
84 std::string label;
85 std::string color;
86 Orientation orientation;
87 std::vector<Interval> intervals;
88};
89
tereliusdc35dcd2016-08-01 12:03:27 -070090// A container that represents a general graph, with axes, title and one or
91// more data series. A subclass should define the output format by overriding
92// the Draw() method.
terelius54ce6802016-07-13 06:44:41 -070093class Plot {
94 public:
95 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070096
tereliusdc35dcd2016-08-01 12:03:27 -070097 // Overloaded to draw the plot.
98 virtual void Draw() = 0;
99
100 // Sets the lower x-axis limit to min_value (if left_margin == 0).
101 // Sets the upper x-axis limit to max_value (if right_margin == 0).
102 // The margins are measured as fractions of the interval
103 // (max_value - min_value) and are added to either side of the plot.
104 void SetXAxis(float min_value,
105 float max_value,
106 std::string label,
107 float left_margin = 0,
108 float right_margin = 0);
109
110 // Sets the lower and upper x-axis limits based on min_value and max_value,
111 // but modified such that all points in the data series can be represented
112 // on the x-axis. The margins are measured as fractions of the range of
113 // x-values and are added to either side of the plot.
114 void SetSuggestedXAxis(float min_value,
115 float max_value,
116 std::string label,
117 float left_margin = 0,
118 float right_margin = 0);
119
120 // Sets the lower y-axis limit to min_value (if bottom_margin == 0).
121 // Sets the upper y-axis limit to max_value (if top_margin == 0).
122 // The margins are measured as fractions of the interval
123 // (max_value - min_value) and are added to either side of the plot.
124 void SetYAxis(float min_value,
125 float max_value,
126 std::string label,
127 float bottom_margin = 0,
128 float top_margin = 0);
129
130 // Sets the lower and upper y-axis limits based on min_value and max_value,
131 // but modified such that all points in the data series can be represented
132 // on the y-axis. The margins are measured as fractions of the range of
133 // y-values and are added to either side of the plot.
134 void SetSuggestedYAxis(float min_value,
135 float max_value,
136 std::string label,
137 float bottom_margin = 0,
138 float top_margin = 0);
139
140 // Sets the title of the plot.
Bjorn Terelius1aa9ee92019-06-11 17:39:38 +0200141 void SetTitle(const std::string& title);
142
143 // Sets an unique ID for the plot. The ID is similar to the title except that
144 // the title might change in future releases whereas the ID should be stable
145 // over time.
146 void SetId(const std::string& id);
tereliusdc35dcd2016-08-01 12:03:27 -0700147
terelius23c595a2017-03-15 01:59:12 -0700148 // Add a new TimeSeries to the plot.
philipel35ba9bd2017-04-19 05:58:51 -0700149 void AppendTimeSeries(TimeSeries&& time_series);
tereliusdc35dcd2016-08-01 12:03:27 -0700150
philipel23c7f252017-07-14 06:30:03 -0700151 // Add a new IntervalSeries to the plot.
152 void AppendIntervalSeries(IntervalSeries&& interval_series);
153
terelius2c8e8a32017-06-02 01:29:48 -0700154 // Add a new TimeSeries to the plot if the series contains contains data.
155 // Otherwise, the call has no effect and the timeseries is destroyed.
156 void AppendTimeSeriesIfNotEmpty(TimeSeries&& time_series);
157
tereliusdc35dcd2016-08-01 12:03:27 -0700158 protected:
159 float xaxis_min_;
160 float xaxis_max_;
161 std::string xaxis_label_;
162 float yaxis_min_;
163 float yaxis_max_;
164 std::string yaxis_label_;
165 std::string title_;
Bjorn Terelius1aa9ee92019-06-11 17:39:38 +0200166 std::string id_;
philipel35ba9bd2017-04-19 05:58:51 -0700167 std::vector<TimeSeries> series_list_;
philipel23c7f252017-07-14 06:30:03 -0700168 std::vector<IntervalSeries> interval_list_;
terelius54ce6802016-07-13 06:44:41 -0700169};
170
171class PlotCollection {
172 public:
173 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700174 virtual void Draw() = 0;
175 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700176
177 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700178 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700179};
180
terelius54ce6802016-07-13 06:44:41 -0700181} // namespace webrtc
182
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200183#endif // RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_