blob: b2ab299a58ba73fa1f6181a4ff55efad4be82bc7 [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;
terelius23c595a2017-03-15 01:59:12 -070037 TimeSeries(const char* label, PlotStyle style)
38 : label(label), style(style), points() {}
39 TimeSeries(const std::string& label, PlotStyle style)
40 : label(label), style(style), points() {}
terelius54ce6802016-07-13 06:44:41 -070041 TimeSeries(TimeSeries&& other)
42 : label(std::move(other.label)),
43 style(other.style),
44 points(std::move(other.points)) {}
45 TimeSeries& operator=(TimeSeries&& other) {
46 label = std::move(other.label);
47 style = other.style;
48 points = std::move(other.points);
49 return *this;
50 }
51
52 std::string label;
53 PlotStyle style;
54 std::vector<TimeSeriesPoint> points;
55};
56
tereliusdc35dcd2016-08-01 12:03:27 -070057// A container that represents a general graph, with axes, title and one or
58// more data series. A subclass should define the output format by overriding
59// the Draw() method.
terelius54ce6802016-07-13 06:44:41 -070060class Plot {
61 public:
62 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070063
tereliusdc35dcd2016-08-01 12:03:27 -070064 // Overloaded to draw the plot.
65 virtual void Draw() = 0;
66
67 // Sets the lower x-axis limit to min_value (if left_margin == 0).
68 // Sets the upper x-axis limit to max_value (if right_margin == 0).
69 // The margins are measured as fractions of the interval
70 // (max_value - min_value) and are added to either side of the plot.
71 void SetXAxis(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 and upper x-axis limits based on min_value and max_value,
78 // but modified such that all points in the data series can be represented
79 // on the x-axis. The margins are measured as fractions of the range of
80 // x-values and are added to either side of the plot.
81 void SetSuggestedXAxis(float min_value,
82 float max_value,
83 std::string label,
84 float left_margin = 0,
85 float right_margin = 0);
86
87 // Sets the lower y-axis limit to min_value (if bottom_margin == 0).
88 // Sets the upper y-axis limit to max_value (if top_margin == 0).
89 // The margins are measured as fractions of the interval
90 // (max_value - min_value) and are added to either side of the plot.
91 void SetYAxis(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 lower and upper y-axis limits based on min_value and max_value,
98 // but modified such that all points in the data series can be represented
99 // on the y-axis. The margins are measured as fractions of the range of
100 // y-values and are added to either side of the plot.
101 void SetSuggestedYAxis(float min_value,
102 float max_value,
103 std::string label,
104 float bottom_margin = 0,
105 float top_margin = 0);
106
107 // Sets the title of the plot.
108 void SetTitle(std::string title);
109
terelius23c595a2017-03-15 01:59:12 -0700110 // Add a new TimeSeries to the plot.
111 TimeSeries* AddTimeSeries(const char* label, PlotStyle style);
112 TimeSeries* AddTimeSeries(const std::string& label, PlotStyle style);
113
tereliusdc35dcd2016-08-01 12:03:27 -0700114 std::vector<TimeSeries> series_list_;
115
116 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_;
terelius54ce6802016-07-13 06:44:41 -0700124};
125
126class PlotCollection {
127 public:
128 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700129 virtual void Draw() = 0;
130 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700131
132 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700133 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700134};
135
136} // namespace plotting
137} // namespace webrtc
138
139#endif // WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_