1
/*!
2

3
Bar chart
4

5
# Examples
6

7
```
8
# use plotlib::repr::BarChart;
9
# use plotlib::view::CategoricalView;
10
let b1 = BarChart::new(5.2).label("b1");
11
let b2 = BarChart::new(1.6).label("b2");
12
let v = CategoricalView::new().add(b1).add(b2);
13
```
14
*/
15

16
use std::f64;
17

18
use svg;
19

20
use crate::axis;
21
use crate::repr::CategoricalRepresentation;
22
use crate::style::BoxStyle;
23
use crate::svg_render;
24

25
pub struct BarChart {
26
    value: f64,
27
    label: String,
28
    style: BoxStyle,
29
}
30

31
impl BarChart {
32 0
    pub fn new(v: f64) -> Self {
33
        BarChart {
34
            value: v,
35 0
            style: BoxStyle::new(),
36 0
            label: String::new(),
37
        }
38
    }
39

40 0
    pub fn style(mut self, style: &BoxStyle) -> Self {
41 0
        self.style.overlay(style);
42 0
        self
43
    }
44

45 0
    pub fn get_style(&self) -> &BoxStyle {
46 0
        &self.style
47
    }
48

49
    pub fn label<T>(mut self, label: T) -> Self
50
    where
51
        T: Into<String>,
52
    {
53 0
        self.label = label.into();
54 0
        self
55
    }
56

57 0
    pub fn get_label(&self) -> &String {
58 0
        &self.label
59
    }
60

61 0
    fn get_value(&self) -> f64 {
62 0
        self.value
63
    }
64
}
65

66
impl CategoricalRepresentation for BarChart {
67
    /// The maximum range. Used for auto-scaling axis
68 0
    fn range(&self) -> (f64, f64) {
69 0
        (0.0, self.value)
70
    }
71

72
    /// The ticks that this representation covers. Used to collect all ticks for display
73 0
    fn ticks(&self) -> Vec<String> {
74 0
        vec![self.label.clone()]
75
    }
76

77 0
    fn to_svg(
78
        &self,
79
        x_axis: &axis::CategoricalAxis,
80
        y_axis: &axis::ContinuousAxis,
81
        face_width: f64,
82
        face_height: f64,
83
    ) -> svg::node::element::Group {
84
        svg_render::draw_face_barchart(
85 0
            self.get_value(),
86 0
            &self.label,
87 0
            x_axis,
88 0
            y_axis,
89 0
            face_width,
90 0
            face_height,
91 0
            &self.style,
92
        )
93
    }
94

95 0
    fn to_text(
96
        &self,
97
        _x_axis: &axis::CategoricalAxis,
98
        _y_axis: &axis::ContinuousAxis,
99
        _face_width: u32,
100
        _face_height: u32,
101
    ) -> String {
102 0
        "".into()
103
    }
104
}

Read our documentation on viewing source code .

Loading