plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.85k stars 278 forks source link

[BUG] Stretched Area in Histogram #489

Open seanaye opened 1 year ago

seanaye commented 1 year ago

Describe the bug I have a simple histogram which plots some data in the wasm backend

fn draw(root_area: DrawingArea<CanvasBackend, Shift>, users: Vec<User>) -> Result<()> {
    let max_subs = users
        .iter()
        .map(|u| u.quantity)
        .fold(std::i64::MIN, |a, b| a.max(b));
    let emails = users.iter().map(|x| x.email.to_owned()).collect::<Vec<_>>();
    let mut ctx = ChartBuilder::on(&root_area)
        // .set_label_area_size(LabelAreaPosition::Left, 40)
        // .set_label_area_size(LabelAreaPosition::Bottom, 40)
        .build_cartesian_2d(emails.into_segmented(), 0..max_subs)
        .unwrap();

    ctx.configure_mesh().draw().unwrap();

    ctx.draw_series(
        Histogram::vertical(&ctx)
            .margin(100)
            .data(users.iter().map(|x| (&x.email, x.quantity))),
    )
    .unwrap();
    Ok(())
}

When this renders there is an area in the middle of the canvas that is stretched and warped. This appears to be a rendering bug

Screen Shot 2023-07-20 at 4 00 19 PM

To Reproduce See code above

Version Information plotters = "0.3.5" plotters-canvas = "0.3.0"

seanaye commented 1 year ago

I have found this only occurs when there are >20 bars to plot. It also occurs in the opposite way in Histogram::vertical

AliMMehr commented 11 months ago

Could it be because you are using a very high value for margin (.margin(100))? 100 pixels seems too large to me. Maybe there should be some check for when the total margin in each direction is bigger than the rectangle size in that direction.