Skip to content

Commit 06c8a25

Browse files
committed
Add ability to style histograms
1 parent d903f00 commit 06c8a25

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

examples/histogram_svg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
extern crate plotlib;
22

3+
use plotlib::style::Bar;
4+
35
fn main() {
46
let data = [0.3, 0.5, 6.4, 5.3, 3.6, 3.6, 3.5, 7.5, 4.0];
5-
let h = plotlib::histogram::Histogram::from_vec(&data, 10);
7+
let h = plotlib::histogram::Histogram::from_vec(&data, 10)
8+
.style(plotlib::histogram::Style::new().fill("olive"));
69
let v = plotlib::view::View::new().add(&h);
710
plotlib::page::Page::single(&v).save("histogram.svg");
811
}

src/histogram.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,39 @@ use utils::PairWise;
2727
use svg_render;
2828
use text_render;
2929
use representation::Representation;
30+
use style;
31+
32+
#[derive(Debug, Default)]
33+
pub struct Style {
34+
fill: Option<String>,
35+
}
36+
37+
impl Style {
38+
pub fn new() -> Self {
39+
Style { fill: None }
40+
}
41+
42+
pub fn overlay(&mut self, other: &Self) {
43+
match other.fill {
44+
Some(ref v) => self.fill = Some(v.clone()),
45+
None => {}
46+
}
47+
}
48+
}
49+
50+
impl style::Bar for Style {
51+
fn fill<T>(&mut self, value: T) -> &mut Self
52+
where
53+
T: Into<String>,
54+
{
55+
self.fill = Some(value.into());
56+
self
57+
}
58+
59+
fn get_fill(&self) -> &Option<String> {
60+
&self.fill
61+
}
62+
}
3063

3164
/**
3265
A one-dimensional histogram with equal binning.
@@ -36,6 +69,7 @@ pub struct Histogram {
3669
pub bin_bounds: Vec<f64>, // will have N_bins + 1 entries
3770
pub bin_counts: Vec<u32>, // will have N_bins entries
3871
pub bin_densities: Vec<f64>, // will have N_bins entries
72+
style: Style,
3973
}
4074

4175
impl Histogram {
@@ -81,6 +115,7 @@ impl Histogram {
81115
bin_bounds: bounds,
82116
bin_counts: bins,
83117
bin_densities: density_per_bin,
118+
style: Style::new(),
84119
}
85120
}
86121

@@ -99,6 +134,15 @@ impl Histogram {
99134
let max = *self.bin_counts.iter().max().unwrap();
100135
(0., max as f64)
101136
}
137+
138+
pub fn style(mut self, style: &Style) -> Self {
139+
self.style.overlay(&style);
140+
self
141+
}
142+
143+
pub fn get_style(&self) -> &Style {
144+
&self.style
145+
}
102146
}
103147

104148
impl Representation for Histogram {
@@ -117,7 +161,7 @@ impl Representation for Histogram {
117161
face_width: f64,
118162
face_height: f64,
119163
) -> svg::node::element::Group {
120-
svg_render::draw_face_bars(self, x_axis, y_axis, face_width, face_height)
164+
svg_render::draw_face_bars(self, x_axis, y_axis, face_width, face_height, &self.style)
121165
}
122166

123167
fn to_text(

src/style.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ pub trait Point {
4747

4848
fn get_size(&self) -> &Option<f32>;
4949
}
50+
51+
pub trait Bar {
52+
fn fill<T>(&mut self, value: T) -> &mut Self
53+
where
54+
T: Into<String>;
55+
56+
fn get_fill(&self) -> &Option<String>;
57+
}

src/svg_render.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,17 @@ where
168168
group
169169
}
170170

171-
pub fn draw_face_bars(
171+
pub fn draw_face_bars<S>(
172172
h: &histogram::Histogram,
173173
x_axis: &axis::Axis,
174174
y_axis: &axis::Axis,
175175
face_width: f64,
176176
face_height: f64,
177-
) -> node::element::Group {
177+
style: &S,
178+
) -> node::element::Group
179+
where
180+
S: style::Bar,
181+
{
178182
let mut group = node::element::Group::new();
179183

180184
for ((&l, &u), &count) in h.bin_bounds.pairwise().zip(h.bin_counts.iter()) {
@@ -187,7 +191,10 @@ pub fn draw_face_bars(
187191
.set("y", -count_scaled)
188192
.set("width", width)
189193
.set("height", count_scaled)
190-
.set("fill", "burlywood")
194+
.set(
195+
"fill",
196+
style.get_fill().clone().unwrap_or("burlywood".into()),
197+
)
191198
.set("stroke", "black");
192199
group.append(rect);
193200
}

0 commit comments

Comments
 (0)