blob: 0c0ebc0b7c87593ee9e3559d13ff595122e6d145 [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
terelius77f05802017-02-01 06:34:53 -080021enum PlotStyle { LINE_GRAPH, LINE_DOT_GRAPH, BAR_GRAPH, LINE_STEP_GRAPH };
terelius54ce6802016-07-13 06:44:41 -070022
23struct TimeSeriesPoint {
24 TimeSeriesPoint(float x, float y) : x(x), y(y) {}
25 float x;
26 float y;
27};
28
29struct TimeSeries {
30 TimeSeries() = default;
terelius23c595a2017-03-15 01:59:12 -070031 TimeSeries(const char* label, PlotStyle style)
32 : label(label), style(style), points() {}
33 TimeSeries(const std::string& label, PlotStyle style)
34 : label(label), style(style), points() {}
terelius54ce6802016-07-13 06:44:41 -070035 TimeSeries(TimeSeries&& other)
36 : label(std::move(other.label)),
37 style(other.style),
38 points(std::move(other.points)) {}
39 TimeSeries& operator=(TimeSeries&& other) {
40 label = std::move(other.label);
41 style = other.style;
42 points = std::move(other.points);
43 return *this;
44 }
45
46 std::string label;
47 PlotStyle style;
48 std::vector<TimeSeriesPoint> points;
49};
50
tereliusdc35dcd2016-08-01 12:03:27 -070051// A container that represents a general graph, with axes, title and one or
52// more data series. A subclass should define the output format by overriding
53// the Draw() method.
terelius54ce6802016-07-13 06:44:41 -070054class Plot {
55 public:
56 virtual ~Plot() {}
terelius54ce6802016-07-13 06:44:41 -070057
tereliusdc35dcd2016-08-01 12:03:27 -070058 // Overloaded to draw the plot.
59 virtual void Draw() = 0;
60
61 // Sets the lower x-axis limit to min_value (if left_margin == 0).
62 // Sets the upper x-axis limit to max_value (if right_margin == 0).
63 // The margins are measured as fractions of the interval
64 // (max_value - min_value) and are added to either side of the plot.
65 void SetXAxis(float min_value,
66 float max_value,
67 std::string label,
68 float left_margin = 0,
69 float right_margin = 0);
70
71 // Sets the lower and upper x-axis limits based on min_value and max_value,
72 // but modified such that all points in the data series can be represented
73 // on the x-axis. The margins are measured as fractions of the range of
74 // x-values and are added to either side of the plot.
75 void SetSuggestedXAxis(float min_value,
76 float max_value,
77 std::string label,
78 float left_margin = 0,
79 float right_margin = 0);
80
81 // Sets the lower y-axis limit to min_value (if bottom_margin == 0).
82 // Sets the upper y-axis limit to max_value (if top_margin == 0).
83 // The margins are measured as fractions of the interval
84 // (max_value - min_value) and are added to either side of the plot.
85 void SetYAxis(float min_value,
86 float max_value,
87 std::string label,
88 float bottom_margin = 0,
89 float top_margin = 0);
90
91 // Sets the lower and upper y-axis limits based on min_value and max_value,
92 // but modified such that all points in the data series can be represented
93 // on the y-axis. The margins are measured as fractions of the range of
94 // y-values and are added to either side of the plot.
95 void SetSuggestedYAxis(float min_value,
96 float max_value,
97 std::string label,
98 float bottom_margin = 0,
99 float top_margin = 0);
100
101 // Sets the title of the plot.
102 void SetTitle(std::string title);
103
terelius23c595a2017-03-15 01:59:12 -0700104 // Add a new TimeSeries to the plot.
105 TimeSeries* AddTimeSeries(const char* label, PlotStyle style);
106 TimeSeries* AddTimeSeries(const std::string& label, PlotStyle style);
107
tereliusdc35dcd2016-08-01 12:03:27 -0700108 std::vector<TimeSeries> series_list_;
109
110 protected:
111 float xaxis_min_;
112 float xaxis_max_;
113 std::string xaxis_label_;
114 float yaxis_min_;
115 float yaxis_max_;
116 std::string yaxis_label_;
117 std::string title_;
terelius54ce6802016-07-13 06:44:41 -0700118};
119
120class PlotCollection {
121 public:
122 virtual ~PlotCollection() {}
tereliusdc35dcd2016-08-01 12:03:27 -0700123 virtual void Draw() = 0;
124 virtual Plot* AppendNewPlot() = 0;
terelius54ce6802016-07-13 06:44:41 -0700125
126 protected:
tereliusdc35dcd2016-08-01 12:03:27 -0700127 std::vector<std::unique_ptr<Plot> > plots_;
terelius54ce6802016-07-13 06:44:41 -0700128};
129
130} // namespace plotting
131} // namespace webrtc
132
133#endif // WEBRTC_TOOLS_EVENT_LOG_VISUALIZER_PLOT_BASE_H_