tiby312 / poloto-project

MIT License
159 stars 10 forks source link

How to plot directly into a file #213

Closed Guelakais closed 7 months ago

Guelakais commented 7 months ago

Your tool is simply not self-explanatory. Is there a better way than the following to transfer a created plot directly into a file?

cargo run > plog.svg

What would I have to change in the following example?

use tagu::format_move;
use poloto::build;
fn main() {
    // hourly trend over one day.
    let trend = vec![
        0, 0, 0, 0, 0, 3, 5, 5, 10, 20, 50, 60, 70, 50, 40, 34, 34, 20, 10, 20, 10, 4, 2, 0,
    ];

    let plots = poloto::plots!(
        build::plot("").histogram((0..).zip(trend)),
        build::markers([24], [])
    );

    let data = poloto::frame_build().data(plots);

    let ticks =
        poloto::ticks::from_iter((0..).step_by(6)).with_tick_fmt(|&v| format_move!("{} hr", v));

    data.map_xticks(|_| ticks)
        .build_and_label(("title", "x", "y"))
        .append_to(poloto::header().light_theme())
        .render_stdout();
}
Guelakais commented 7 months ago

Got it right, the code now looks like this:

use tagu::format_move;
use poloto::build;
use std::fs::write;
fn main() {
    // hourly trend over one day.
    let trend = vec![
        2.83,
        37.8,
        0.25
    ];

    let plots = poloto::plots!(
        build::plot("").histogram((0..).zip(trend)),
        build::markers([6], [])
    );

    let data = poloto::frame_build().data(plots);
    write("plot.svg", data.map_xticks(|_| poloto::ticks::from_iter((0..).step_by(1)).with_tick_fmt(|&v| format_move!("{} hr", v)))
        .build_and_label(("Runtime Comparision", "Runtime in sec", "Programming Language"))
        .append_to(poloto::header().light_theme())
        .render_string().unwrap()).unwrap();
}