Skip to content

Commit 40d2a0a

Browse files
committed
Add colour options to Function
1 parent 73278a4 commit 40d2a0a

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/function.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,43 @@ use representation::Representation;
77

88
use svg_render;
99

10+
#[derive(Debug)]
11+
pub struct Style {
12+
colour: Option<String>,
13+
}
14+
15+
impl Style {
16+
pub fn new() -> Self {
17+
Style {
18+
colour: None,
19+
}
20+
}
21+
22+
pub fn overlay(&mut self, other: Self) {
23+
match other.colour {
24+
Some(v) => self.colour = Some(v),
25+
None => {}
26+
}
27+
}
28+
29+
pub fn colour<T>(mut self, value: T) -> Self
30+
where T: Into<String>
31+
{
32+
self.colour = Some(value.into());
33+
self
34+
}
35+
36+
pub fn get_colour(&self) -> String {
37+
match self.colour.clone() {
38+
Some(v) => v,
39+
None => "".into(),
40+
}
41+
}
42+
}
43+
1044
pub struct Function {
1145
pub data: Vec<(f64, f64)>,
46+
style: Style,
1247
}
1348

1449
impl Function {
@@ -19,9 +54,19 @@ impl Function {
1954
let values = samples.map(|s| (s, f(s))).collect();
2055
Function {
2156
data: values,
57+
style: Style::new(),
2258
}
2359
}
2460

61+
pub fn style(mut self, style: Style) -> Self {
62+
self.style.overlay(style);
63+
self
64+
}
65+
66+
pub fn get_style(&self) -> &Style {
67+
&self.style
68+
}
69+
2570
fn x_range(&self) -> (f64, f64) {
2671
let mut min = f64::INFINITY;
2772
let mut max = f64::NEG_INFINITY;
@@ -58,7 +103,7 @@ impl Representation for Function {
58103
face_width: f64,
59104
face_height: f64)
60105
-> svg::node::element::Group {
61-
svg_render::draw_face_line(self, x_axis, y_axis, face_width, face_height)
106+
svg_render::draw_face_line(self, x_axis, y_axis, face_width, face_height, &self.style)
62107
}
63108

64109
fn to_text(&self,

src/plot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl<'a> Plot<'a> {
3535
*/
3636
pub fn to_svg(&self) -> svg::Document {
3737
let mut document = Document::new().set("viewBox", (0, 0, 600, 400));
38+
// TODO put multiple views in correct places
3839
for &view in &self.views {
3940
let view_group = view.to_svg(500., 340.)
4041
.set("transform", format!("translate({}, {})", 50, 360));

src/representation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use svg;
2-
32
use axis;
43

54
pub trait Representation {
5+
/// The maximum range in each dimension. Used for auto-scaling axes
66
fn range(&self, dim: u32) -> (f64, f64);
77

88
fn to_svg(&self,
@@ -11,6 +11,7 @@ pub trait Representation {
1111
face_width: f64,
1212
face_height: f64)
1313
-> svg::node::element::Group;
14+
1415
fn to_text(&self,
1516
x_axis: &axis::Axis,
1617
y_axis: &axis::Axis,

src/svg_render.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ pub fn draw_face_line(s: &function::Function,
185185
x_axis: &axis::Axis,
186186
y_axis: &axis::Axis,
187187
face_width: f64,
188-
face_height: f64)
188+
face_height: f64,
189+
style: &function::Style)
189190
-> node::element::Group {
190191
let mut group = node::element::Group::new();
191192

@@ -204,7 +205,7 @@ pub fn draw_face_line(s: &function::Function,
204205

205206
group.append(node::element::Path::new()
206207
.set("fill", "none")
207-
.set("stroke", "black")
208+
.set("stroke", style.get_colour())
208209
.set("stroke-width", 2)
209210
.set("d", path));
210211

0 commit comments

Comments
 (0)