blob: 0abd6befec867b18d71836310605949d7bd58830 [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 */
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
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
tereliusdc35dcd2016-08-01 12:03:27 -070056// 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.
terelius54ce6802016-07-13 06:44:41 -070059class Plot {
60 public:
61 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070062
tereliusdc35dcd2016-08-01 12:03:27 -070063 // 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
terelius23c595a2017-03-15 01:59:12 -0700109 // Add a new TimeSeries to the plot.
philipel35ba9bd2017-04-19 05:58:51 -0700110 void AppendTimeSeries(TimeSeries&& time_series);
tereliusdc35dcd2016-08-01 12:03:27 -0700111
terelius2c8e8a32017-06-02 01:29:48 -0700112 // Add a new TimeSeries to the plot if the series contains contains data.
113 // Otherwise, the call has no effect and the timeseries is destroyed.
114 void AppendTimeSeriesIfNotEmpty(TimeSeries&& time_series);
115
tereliusdc35dcd2016-08-01 12:03:27 -0700116 protected:
117 float xaxis_min_;
118 float xaxis_max_;
119 std::string xaxis_label_;
120 float yaxis_min_;
121 float yaxis_max_;
122 std::string yaxis_label_;
123 std::string title_;
philipel35ba9bd2017-04-19 05:58:51 -0700124 std::vector<TimeSeries> series_list_;
terelius54ce6802016-07-13 06:44:41 -0700125};
126
127class PlotCollection {
128 public:
129 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700130 virtual void Draw() = 0;
131 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700132
133 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700134 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700135};
136
137} // namespace plotting
138} // namespace webrtc
139
140#endif // WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_