blob: 700ffbf02dd5eee108d76b64faa26ecde8e070f3 [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
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010021enum class LineStyle {
22 kNone, // No line connecting the points. Used to create scatter plots.
23 kLine, // Straight line between consecutive points.
24 kStep, // Horizontal line until the next value. Used for state changes.
25 kBar // Vertical bars from the x-axis to the point.
26};
27
28enum class PointStyle {
29 kNone, // Don't draw the points.
30 kHighlight // Draw circles or dots to highlight the points.
philipele127e7a2017-03-29 16:28:53 +020031};
terelius54ce6802016-07-13 06:44:41 -070032
33struct TimeSeriesPoint {
34 TimeSeriesPoint(float x, float y) : x(x), y(y) {}
35 float x;
36 float y;
37};
38
39struct TimeSeries {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010040 TimeSeries() = default; // TODO(terelius): Remove the default constructor.
41 TimeSeries(const char* label,
42 LineStyle line_style,
43 PointStyle point_style = PointStyle::kNone)
44 : label(label), line_style(line_style), point_style(point_style) {}
45 TimeSeries(const std::string& label,
46 LineStyle line_style,
47 PointStyle point_style = PointStyle::kNone)
48 : label(label), line_style(line_style), point_style(point_style) {}
terelius54ce6802016-07-13 06:44:41 -070049 TimeSeries(TimeSeries&& other)
50 : label(std::move(other.label)),
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010051 line_style(other.line_style),
52 point_style(other.point_style),
terelius54ce6802016-07-13 06:44:41 -070053 points(std::move(other.points)) {}
54 TimeSeries& operator=(TimeSeries&& other) {
55 label = std::move(other.label);
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010056 line_style = other.line_style;
57 point_style = other.point_style;
terelius54ce6802016-07-13 06:44:41 -070058 points = std::move(other.points);
59 return *this;
60 }
61
62 std::string label;
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +010063 LineStyle line_style = LineStyle::kLine;
64 PointStyle point_style = PointStyle::kNone;
terelius54ce6802016-07-13 06:44:41 -070065 std::vector<TimeSeriesPoint> points;
66};
67
philipel23c7f252017-07-14 06:30:03 -070068struct Interval {
69 Interval() = default;
70 Interval(double begin, double end) : begin(begin), end(end) {}
71
72 double begin;
73 double end;
74};
75
76struct IntervalSeries {
77 enum Orientation { kHorizontal, kVertical };
78
79 IntervalSeries() = default;
80 IntervalSeries(const std::string& label,
81 const std::string& color,
82 IntervalSeries::Orientation orientation)
83 : label(label), color(color), orientation(orientation) {}
84
85 std::string label;
86 std::string color;
87 Orientation orientation;
88 std::vector<Interval> intervals;
89};
90
tereliusdc35dcd2016-08-01 12:03:27 -070091// A container that represents a general graph, with axes, title and one or
92// more data series. A subclass should define the output format by overriding
93// the Draw() method.
terelius54ce6802016-07-13 06:44:41 -070094class Plot {
95 public:
96 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070097
tereliusdc35dcd2016-08-01 12:03:27 -070098 // Overloaded to draw the plot.
99 virtual void Draw() = 0;
100
101 // Sets the lower x-axis limit to min_value (if left_margin == 0).
102 // Sets the upper x-axis limit to max_value (if right_margin == 0).
103 // The margins are measured as fractions of the interval
104 // (max_value - min_value) and are added to either side of the plot.
105 void SetXAxis(float min_value,
106 float max_value,
107 std::string label,
108 float left_margin = 0,
109 float right_margin = 0);
110
111 // Sets the lower and upper x-axis limits based on min_value and max_value,
112 // but modified such that all points in the data series can be represented
113 // on the x-axis. The margins are measured as fractions of the range of
114 // x-values and are added to either side of the plot.
115 void SetSuggestedXAxis(float min_value,
116 float max_value,
117 std::string label,
118 float left_margin = 0,
119 float right_margin = 0);
120
121 // Sets the lower y-axis limit to min_value (if bottom_margin == 0).
122 // Sets the upper y-axis limit to max_value (if top_margin == 0).
123 // The margins are measured as fractions of the interval
124 // (max_value - min_value) and are added to either side of the plot.
125 void SetYAxis(float min_value,
126 float max_value,
127 std::string label,
128 float bottom_margin = 0,
129 float top_margin = 0);
130
131 // Sets the lower and upper y-axis limits based on min_value and max_value,
132 // but modified such that all points in the data series can be represented
133 // on the y-axis. The margins are measured as fractions of the range of
134 // y-values and are added to either side of the plot.
135 void SetSuggestedYAxis(float min_value,
136 float max_value,
137 std::string label,
138 float bottom_margin = 0,
139 float top_margin = 0);
140
141 // Sets the title of the plot.
142 void SetTitle(std::string title);
143
terelius23c595a2017-03-15 01:59:12 -0700144 // Add a new TimeSeries to the plot.
philipel35ba9bd2017-04-19 05:58:51 -0700145 void AppendTimeSeries(TimeSeries&& time_series);
tereliusdc35dcd2016-08-01 12:03:27 -0700146
philipel23c7f252017-07-14 06:30:03 -0700147 // Add a new IntervalSeries to the plot.
148 void AppendIntervalSeries(IntervalSeries&& interval_series);
149
terelius2c8e8a32017-06-02 01:29:48 -0700150 // Add a new TimeSeries to the plot if the series contains contains data.
151 // Otherwise, the call has no effect and the timeseries is destroyed.
152 void AppendTimeSeriesIfNotEmpty(TimeSeries&& time_series);
153
tereliusdc35dcd2016-08-01 12:03:27 -0700154 protected:
155 float xaxis_min_;
156 float xaxis_max_;
157 std::string xaxis_label_;
158 float yaxis_min_;
159 float yaxis_max_;
160 std::string yaxis_label_;
161 std::string title_;
philipel35ba9bd2017-04-19 05:58:51 -0700162 std::vector<TimeSeries> series_list_;
philipel23c7f252017-07-14 06:30:03 -0700163 std::vector<IntervalSeries> interval_list_;
terelius54ce6802016-07-13 06:44:41 -0700164};
165
166class PlotCollection {
167 public:
168 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700169 virtual void Draw() = 0;
170 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700171
172 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700173 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700174};
175
176} // namespace plotting
177} // namespace webrtc
178
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200179#endif // RTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_