Skip to content

Commit

Permalink
Merge pull request #42 from blinkseb/lines-style
Browse files Browse the repository at this point in the history
Allow the user to set the line style per line
  • Loading branch information
OlivierBondu committed Jan 15, 2016
2 parents 1abc17e + d5d1aa3 commit c0fb9ed
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 31 deletions.
27 changes: 19 additions & 8 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <boost/filesystem.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>

#include <iostream>

#include <defines.h>
Expand Down Expand Up @@ -78,17 +80,25 @@ namespace plotIt {
Summary summary;
};

struct PlotStyle {
struct LineStyle {
float line_width = 1;
int16_t line_color = 1;
int16_t line_type = 1;

LineStyle() = default;
LineStyle(const YAML::Node& node);

void parse(const YAML::Node& node);
};

struct PlotStyle: public LineStyle {

// Style
float marker_size;
int16_t marker_color;
int16_t marker_type;
int16_t fill_color;
int16_t fill_type;
float line_width;
int16_t line_color;
int16_t line_type;
std::string drawing_options;

// Legend
Expand Down Expand Up @@ -210,6 +220,8 @@ namespace plotIt {
Point start;
Point end;

boost::optional<LineStyle> style;

bool operator==(const Line& other) {
return ((start == other.start) && (end == other.end));
}
Expand All @@ -224,6 +236,7 @@ namespace plotIt {
start = *c.begin();
end = *(c.begin() + 1);
}

Line(const YAML::Node& node, Orientation);
};

Expand Down Expand Up @@ -349,9 +362,7 @@ namespace plotIt {
int16_t ratio_fit_error_fill_color = 42;
int16_t ratio_fit_error_fill_style = 1001;

int16_t line_color = 0;
int16_t line_width = 1;
int16_t line_style = 1;
LineStyle line_style;

std::vector<Label> labels;

Expand Down
6 changes: 3 additions & 3 deletions src/TH1Plotter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ namespace plotIt {
std::shared_ptr<TLine> l(new TLine(line.start.x, line.start.y, line.end.x, line.end.y));
TemporaryPool::get().add(l);

l->SetLineColor(m_plotIt.getConfiguration().line_color);
l->SetLineWidth(m_plotIt.getConfiguration().line_width);
l->SetLineStyle(m_plotIt.getConfiguration().line_style);
l->SetLineColor(line.style->line_color);
l->SetLineWidth(line.style->line_width);
l->SetLineStyle(line.style->line_type);

l->Draw("same");
}
Expand Down
14 changes: 6 additions & 8 deletions src/plotIt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,7 @@ namespace plotIt {
if (node["blinded-range-fill-style"])
m_config.blinded_range_fill_style = node["blinded-range-fill-style"].as<uint16_t>();

if (node["line-color"])
m_config.line_color = loadColor(node["line-color"]);

if (node["line-width"])
m_config.line_width = node["line-style"].as<int16_t>();

if (node["line-style"])
m_config.line_style = node["line-style"].as<int16_t>();
m_config.line_style.parse(node);

if (node["labels"]) {
YAML::Node labels = node["labels"];
Expand Down Expand Up @@ -552,6 +545,11 @@ namespace plotIt {
plot.lines.push_back(Line(line, UNSPECIFIED));
}
}

for (auto& line: plot.lines) {
if (! line.style)
line.style = m_config.line_style;
}

// Handle log
std::vector<bool> logs_x;
Expand Down
36 changes: 25 additions & 11 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ namespace plotIt {
if (node["fill-type"])
fill_type = node["fill-type"].as<int16_t>();

if (node["line-color"])
line_color = loadColor(node["line-color"]);

if (node["line-type"])
line_type = node["line-type"].as<int16_t>();

if (node["line-width"])
line_width = node["line-width"].as<float>();
LineStyle::parse(node);

if (node["marker-color"])
marker_color = loadColor(node["marker-color"]);
Expand All @@ -82,18 +75,39 @@ namespace plotIt {
marker_size = node["marker-size"].as<float>();
}

LineStyle::LineStyle(const YAML::Node& node) {
parse(node);
}

void LineStyle::parse(const YAML::Node& node) {
if (node["line-color"])
line_color = loadColor(node["line-color"]);

if (node["line-type"])
line_type = node["line-type"].as<int16_t>();

if (node["line-width"])
line_width = node["line-width"].as<float>();
}

Line::Line(const YAML::Node& node, Orientation orientation) {
auto NaN = std::numeric_limits<float>::quiet_NaN();

YAML::Node configuration = node;
if (node.Type() == YAML::NodeType::Map) {
style = LineStyle(node);
configuration = node["value"];
}

if (orientation == UNSPECIFIED) {
auto points = node.as<std::vector<Point>>();
auto points = configuration.as<std::vector<Point>>();
if (points.size() != 2)
throw nullptr; // FIXME
throw YAML::ParserException(YAML::Mark::null_mark(), "A line is defined by exactly two points");

start = points[0];
end = points[1];
} else {
float value = node.as<float>();
float value = configuration.as<float>();
if (orientation == HORIZONTAL) {
start = {NaN, value};
end = {NaN, value};
Expand Down
Binary file not shown.
25 changes: 24 additions & 1 deletion test/unit_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_lines(self):

configuration['configuration']['line-color'] = "#1685f2"
configuration['configuration']['line-width'] = 3
configuration['configuration']['line-style'] = 4
configuration['configuration']['line-type'] = 4

configuration['plots']['histo1']['vertical-lines'] = [3, 7]
configuration['plots']['histo1']['horizontal-lines'] = [80]
Expand All @@ -248,3 +248,26 @@ def test_lines(self):
os.path.join(self.output_folder.name, 'histo1.pdf'),
get_golden_file('default_configuration_lines.pdf')
)

configuration = get_configuration()

configuration['configuration']['line-color'] = "#1685f2"
configuration['configuration']['line-width'] = 3
configuration['configuration']['line-type'] = 4

configuration['plots']['histo1']['vertical-lines'] = [{
'line-color': '#ae4fea',
'line-width': 10,
'value': 3}, 7]
configuration['plots']['histo1']['horizontal-lines'] = [80]
configuration['plots']['histo1']['lines'] = [
[[3, 20], [8, 90]],
[[5.5, float('nan')], [5.5, float('nan')]]
]

self.run_plotit(configuration)

self.compare_images(
os.path.join(self.output_folder.name, 'histo1.pdf'),
get_golden_file('default_configuration_lines_inline_style.pdf')
)

0 comments on commit c0fb9ed

Please sign in to comment.