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.89k stars 281 forks source link

[BUG] Drawing off by a few px #165

Open jangernert opened 4 years ago

jangernert commented 4 years ago

Describe the bug At least with the CanvasBackend the drawing of the axis seems to be off a little. Some lines of the axis don't quite connect. Some overlap which looks bad with the default opacity of the line.

It seems like the thickness of the line is not taken into consideration when drawing.

canvas

Overlap:

Screenshot from 2020-07-19 15-28-39

Overlap and end of X-Axis a little too short:

Screenshot from 2020-07-19 15-29-05

Axis not quite connecting right at 0:

Screenshot from 2020-07-19 15-28-05

Top of Y-Axis not going all the way to the top

Screenshot from 2020-07-19 15-28-23

To Reproduce

let backend = CanvasBackend::new("canvas").unwrap();
let root = backend.into_drawing_area();

root.fill(&WHITE).unwrap();

let mut chart = ChartBuilder::on(&root)
    .set_left_and_bottom_label_area_size(30)
    .margin(10)
    .build_ranged(0.0..2.0, 0.0..3.0)
    .unwrap();

chart
    .configure_mesh()
    .x_labels(3)
    .y_labels(2)
    .draw()
    .unwrap();

let data = (0..=20)
    .map(|x| x as f64 / 10.0)
    .map(|x| (x, x.powf(2.0) + 1.0));

chart.draw_series(
    AreaSeries::new(
        data.clone(),
        0.0,
        &RED.mix(0.2),
    )
    .border_style(&RED),
).unwrap();

chart.draw_series(
    data
        .map(|(x, y)| Circle::new((x, y), 3, BLUE.filled())),
).unwrap();

root.present().unwrap();

Version Information

plotters = "0.2"
Pascal-So commented 2 years ago

It seems like this is related to #128, specifically the fact that plotters tries to draw the lines at integer coordinates but when the backend supports float coordinates then we have to add 0.5 to the coordinates, otherwise we're drawing "between" pixels. See my PR in plotters-canvas for an attempt at fixing this.