Skip to content

Commit b763194

Browse files
committed
Break out line drawing style into a trait
1 parent d229844 commit b763194

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

examples/function_svg.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate plotlib;
22

3+
use plotlib::style::Line;
4+
35
fn main() {
46
let f1 = plotlib::function::Function::new(|x| x * 5., 0., 10.)
57
.style(plotlib::function::Style::new().colour("burlywood"));

src/function.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use svg;
2020
use axis;
2121
use representation::Representation;
2222
use svg_render;
23+
use style;
2324

2425
#[derive(Debug, Default)]
2526
pub struct Style {
@@ -31,22 +32,24 @@ impl Style {
3132
Style { colour: None }
3233
}
3334

34-
pub fn overlay(&mut self, other: Self) {
35+
pub fn overlay(&mut self, other: &Self) {
3536
match other.colour {
36-
Some(v) => self.colour = Some(v),
37+
Some(ref v) => self.colour = Some(v.clone()),
3738
None => {}
3839
}
3940
}
41+
}
4042

41-
pub fn colour<T>(mut self, value: T) -> Self
43+
impl style::Line for Style {
44+
fn colour<T>(&mut self, value: T) -> &mut Self
4245
where
4346
T: Into<String>,
4447
{
4548
self.colour = Some(value.into());
4649
self
4750
}
4851

49-
pub fn get_colour(&self) -> String {
52+
fn get_colour(&self) -> String {
5053
match self.colour.clone() {
5154
Some(v) => v,
5255
None => "".into(),
@@ -75,8 +78,8 @@ impl Function {
7578
}
7679
}
7780

78-
pub fn style(mut self, style: Style) -> Self {
79-
self.style.overlay(style);
81+
pub fn style(mut self, style: &Style) -> Self {
82+
self.style.overlay(&style);
8083
self
8184
}
8285

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub mod page;
7474
pub mod histogram;
7575
pub mod scatter;
7676
pub mod function;
77+
pub mod style;
7778
mod axis;
7879
mod utils;
7980
mod text_render;

src/style.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*!
2+
3+
Manage how elements should be drawn
4+
5+
*/
6+
7+
pub trait Line {
8+
fn colour<T>(&mut self, value: T) -> &mut Self
9+
where
10+
T: Into<String>;
11+
12+
fn get_colour(&self) -> String;
13+
}

src/svg_render.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use histogram;
55
use scatter;
66
use function;
77
use axis;
8+
use style;
89
use utils::PairWise;
910

1011
fn value_to_face_offset(value: f64, axis: &axis::Axis, face_size: f64) -> f64 {
@@ -191,14 +192,17 @@ pub fn draw_face_bars(
191192
group
192193
}
193194

194-
pub fn draw_face_line(
195+
pub fn draw_face_line<S>(
195196
s: &function::Function,
196197
x_axis: &axis::Axis,
197198
y_axis: &axis::Axis,
198199
face_width: f64,
199200
face_height: f64,
200-
style: &function::Style,
201-
) -> node::element::Group {
201+
style: &S,
202+
) -> node::element::Group
203+
where
204+
S: style::Line,
205+
{
202206
let mut group = node::element::Group::new();
203207

204208
let mut d: Vec<node::element::path::Command> = vec![];

0 commit comments

Comments
 (0)