Struct Chart

Source
pub struct Chart { /* private fields */ }
Expand description

The Chart struct contains the configuration for displaying some data.

By default, a chart has a radius of 9, an aspect ratio of 2 and doesn’t show its legend.

§Example usage:

use piechart::{Chart, Color, Data};

let data = vec![
    Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
    Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
    Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
];

Chart::new()
    .radius(9)
    .aspect_ratio(3)
    .legend(true)
    .draw(&data);

will result in

example image

Implementations§

Source§

impl Chart

Source

pub fn new() -> Self

Contructs a new chart initialized with its default values

Examples found in repository?
examples/basic.rs (line 12)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "BTC".into(), value: 5977.0, color: None, fill: '•' },
7        Data { label: "BCH".into(), value: 3045.0, color: None, fill: '◍' },
8        Data { label: "LTC".into(), value: 2030.0, color: None, fill: '.' },
9        Data { label: "ETH".into(), value: 2350.0, color: None, fill: '*' },
10    ];
11
12    Chart::new().draw(&data);
13}
More examples
Hide additional examples
examples/config.rs (line 11)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
7        Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
8        Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
9    ];
10
11    Chart::new()
12        .radius(9)
13        .aspect_ratio(3)
14        .legend(true)
15        .draw(&data);
16}
examples/comparison.rs (line 11)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "dd1".into(), value: 4.0, color: Some(Color::Red.into()), fill: '•' },
7        Data { label: "dd2".into(), value: 2.0, color: Some(Color::Green.into()), fill: '•' },
8        Data { label: "dd3".into(), value: 2.6, color: Some(Color::Blue.into()), fill: '•' },
9    ];
10
11    let mut chart = Chart::new();
12
13    for a in 0..=12 {
14        chart.radius(a);
15        chart.draw(&data);
16        println!();
17    }
18}
Source

pub fn radius(&mut self, radius: u16) -> &mut Self

Sets the radius of the pie chart. To choose which size fits the best, you can run a code snippet like this:

let mut chart = Chart::new();
for radius in 0..=12 {
    chart.radius(radius);
    chart.draw(&data);
}
Examples found in repository?
examples/config.rs (line 12)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
7        Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
8        Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
9    ];
10
11    Chart::new()
12        .radius(9)
13        .aspect_ratio(3)
14        .legend(true)
15        .draw(&data);
16}
More examples
Hide additional examples
examples/comparison.rs (line 14)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "dd1".into(), value: 4.0, color: Some(Color::Red.into()), fill: '•' },
7        Data { label: "dd2".into(), value: 2.0, color: Some(Color::Green.into()), fill: '•' },
8        Data { label: "dd3".into(), value: 2.6, color: Some(Color::Blue.into()), fill: '•' },
9    ];
10
11    let mut chart = Chart::new();
12
13    for a in 0..=12 {
14        chart.radius(a);
15        chart.draw(&data);
16        println!();
17    }
18}
Source

pub fn aspect_ratio(&mut self, aspect_ratio: u16) -> &mut Self

The aspect ratio controls how stretched or squished the circle is. Since terminal columns are more tall than wide a ration of 2 or 3 is the best in most cases.

Examples found in repository?
examples/config.rs (line 13)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
7        Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
8        Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
9    ];
10
11    Chart::new()
12        .radius(9)
13        .aspect_ratio(3)
14        .legend(true)
15        .draw(&data);
16}
Source

pub fn legend(&mut self, legend: bool) -> &mut Self

Specifies whether the chart should render a legend with the labels and their percentages.

Examples found in repository?
examples/config.rs (line 14)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
7        Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
8        Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
9    ];
10
11    Chart::new()
12        .radius(9)
13        .aspect_ratio(3)
14        .legend(true)
15        .draw(&data);
16}
Source§

impl Chart

Source

pub fn draw(&self, data: &[Data])

Renders the chart and outputs it onto stdout. The method panics in case of an error. If you want more fine-grained control about error recovery and how the buffer the chart is rendered into the buffer, use Chart::draw_into.

Examples found in repository?
examples/basic.rs (line 12)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "BTC".into(), value: 5977.0, color: None, fill: '•' },
7        Data { label: "BCH".into(), value: 3045.0, color: None, fill: '◍' },
8        Data { label: "LTC".into(), value: 2030.0, color: None, fill: '.' },
9        Data { label: "ETH".into(), value: 2350.0, color: None, fill: '*' },
10    ];
11
12    Chart::new().draw(&data);
13}
More examples
Hide additional examples
examples/config.rs (line 15)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "Chocolate".into(), value: 4.0, color: Some(Color::Blue.into()), fill: '•' },
7        Data { label: "Strawberry".into(), value: 2.0, color: Some(Color::Red.into()), fill: '▪' },
8        Data { label: "Vanilla".into(), value: 2.6, color: Some(Color::Yellow.into()), fill: '▴' },
9    ];
10
11    Chart::new()
12        .radius(9)
13        .aspect_ratio(3)
14        .legend(true)
15        .draw(&data);
16}
examples/comparison.rs (line 15)
3fn main() {
4    #[rustfmt::skip]
5    let data = vec![
6        Data { label: "dd1".into(), value: 4.0, color: Some(Color::Red.into()), fill: '•' },
7        Data { label: "dd2".into(), value: 2.0, color: Some(Color::Green.into()), fill: '•' },
8        Data { label: "dd3".into(), value: 2.6, color: Some(Color::Blue.into()), fill: '•' },
9    ];
10
11    let mut chart = Chart::new();
12
13    for a in 0..=12 {
14        chart.radius(a);
15        chart.draw(&data);
16        println!();
17    }
18}
Source

pub fn draw_into(&self, f: impl Write, data: &[Data]) -> Result<()>

Same as Chart::draw, but you can supply your own impl Write and you can handle errors gracefully.

Trait Implementations§

Source§

impl Debug for Chart

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chart

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Chart

§

impl RefUnwindSafe for Chart

§

impl Send for Chart

§

impl Sync for Chart

§

impl Unpin for Chart

§

impl UnwindSafe for Chart

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.