Support selecting colors by name.
Some plots really want the colors to be in a specific order. Provide
the ability to select a color by name.
Change-Id: Ia3c0bc40ee373e9cff51e4a3b9ec3a732431210a
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/analysis/in_process_plotter.cc b/frc971/analysis/in_process_plotter.cc
index 585c933..5f08e5d 100644
--- a/frc971/analysis/in_process_plotter.cc
+++ b/frc971/analysis/in_process_plotter.cc
@@ -19,15 +19,26 @@
builder_(plot_sender_.MakeBuilder()) {
web_proxy_.SetDataPath(kDataPath);
event_loop_->SkipTimingReport();
- color_wheel_.push_back(Color(1, 0, 0));
- color_wheel_.push_back(Color(0, 1, 0));
- color_wheel_.push_back(Color(0, 0, 1));
- color_wheel_.push_back(Color(1, 1, 0));
- color_wheel_.push_back(Color(0, 1, 1));
- color_wheel_.push_back(Color(1, 0, 1));
- color_wheel_.push_back(Color(1, 0.6, 0));
- color_wheel_.push_back(Color(0.6, 0.3, 0));
- color_wheel_.push_back(Color(1, 1, 1));
+
+ color_wheel_.emplace_back(ColorWheelColor{.name = "red", .color = {1, 0, 0}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "green", .color = {0, 1, 0}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "purple", .color = {0.54, 0.3, 0.75}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "blue", .color = {0, 0, 1}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "yellow", .color = {1, 1, 0}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "teal", .color = {0, 1, 1}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "pink", .color = {1, 0, 1}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "orange", .color = {1, 0.6, 0}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "brown", .color = {0.6, 0.3, 0}});
+ color_wheel_.emplace_back(
+ ColorWheelColor{.name = "white", .color = {1, 1, 1}});
}
void Plotter::Spin() { event_loop_factory_.Run(); }
@@ -63,15 +74,14 @@
}
void Plotter::AddLine(const std::vector<double> &x,
- const std::vector<double> &y, std::string_view label,
- std::string_view line_style) {
+ const std::vector<double> &y, LineOptions options) {
CHECK_EQ(x.size(), y.size());
CHECK(!position_.IsNull())
<< "You must call AddFigure() before calling AddLine().";
flatbuffers::Offset<flatbuffers::String> label_offset;
- if (!label.empty()) {
- label_offset = builder_.fbb()->CreateString(label);
+ if (!options.label.empty()) {
+ label_offset = builder_.fbb()->CreateString(options.label);
}
std::vector<Point> points;
@@ -81,16 +91,28 @@
const flatbuffers::Offset<flatbuffers::Vector<const Point *>> points_offset =
builder_.fbb()->CreateVectorOfStructs(points);
- const Color *color = &color_wheel_.at(color_wheel_position_);
- color_wheel_position_ = (color_wheel_position_ + 1) % color_wheel_.size();
+ const Color *color;
+ if (options.color.empty()) {
+ color = &color_wheel_.at(color_wheel_position_).color;
+ color_wheel_position_ = (color_wheel_position_ + 1) % color_wheel_.size();
+ } else {
+ auto it = std::find_if(
+ color_wheel_.begin(), color_wheel_.end(),
+ [options_color = options.color](const ColorWheelColor &color) {
+ return color.name == options_color;
+ });
+ CHECK(it != color_wheel_.end()) << ": Failed to find " << options.color;
+ color = &(it->color);
+ }
LineStyle::Builder style_builder = builder_.MakeBuilder<LineStyle>();
- if (line_style.find('*') != line_style.npos) {
+ if (options.line_style.find('*') != options.line_style.npos) {
style_builder.add_point_size(3.0);
} else {
style_builder.add_point_size(0.0);
}
- style_builder.add_draw_line(line_style.find('-') != line_style.npos);
+ style_builder.add_draw_line(options.line_style.find('-') !=
+ options.line_style.npos);
const flatbuffers::Offset<LineStyle> style_offset = style_builder.Finish();
auto line_builder = builder_.MakeBuilder<Line>();
@@ -132,8 +154,7 @@
plot_builder.add_title(title_);
plot_builder.add_figures(figures_offset);
- CHECK_EQ(builder_.Send(plot_builder.Finish()),
- aos::RawSender::Error::kOk);
+ CHECK_EQ(builder_.Send(plot_builder.Finish()), aos::RawSender::Error::kOk);
builder_ = plot_sender_.MakeBuilder();
diff --git a/frc971/analysis/in_process_plotter.h b/frc971/analysis/in_process_plotter.h
index 8d4c84c..b78a8fb 100644
--- a/frc971/analysis/in_process_plotter.h
+++ b/frc971/analysis/in_process_plotter.h
@@ -42,14 +42,30 @@
void Title(std::string_view title);
void AddFigure(std::string_view title = "", double width = 0,
double height = 0);
+ struct LineOptions {
+ std::string_view label = "";
+ std::string_view line_style = "*-";
+ std::string_view color = "";
+ };
+
void AddLine(const std::vector<double> &x, const std::vector<double> &y,
- std::string_view label = "", std::string_view line_style = "*-");
+ std::string_view label) {
+ AddLine(x, y, LineOptions{.label = label});
+ }
+ void AddLine(const std::vector<double> &x, const std::vector<double> &y,
+ std::string_view label, std::string_view line_style) {
+ AddLine(x, y, LineOptions{.label = label, .line_style = line_style});
+ }
+ void AddLine(const std::vector<double> &x, const std::vector<double> &y,
+ LineOptions options);
+
void ShareXAxis(bool share) { share_x_axis_ = share; }
void XLabel(std::string_view label);
void YLabel(std::string_view label);
void Publish();
void Spin();
+
private:
void MaybeFinishFigure();
@@ -70,8 +86,13 @@
std::vector<flatbuffers::Offset<Figure>> figures_;
std::vector<flatbuffers::Offset<Line>> lines_;
+ struct ColorWheelColor {
+ std::string name;
+ Color color;
+ };
+
size_t color_wheel_position_ = 0;
- std::vector<Color> color_wheel_;
+ std::vector<ColorWheelColor> color_wheel_;
};
} // namespace analysis