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.8k stars 276 forks source link

Y labels are not aligned #550

Open wangjiawen2013 opened 7 months ago

wangjiawen2013 commented 7 months ago

Hi, When plotting both the left and right y-axis, the labels on the left y-axis was not aligned:

evcxr_figure((640, 480), |root| {
    let x_axis = (-3.4f32..3.4).step(0.01);
    root.fill(&WHITE)?;
    let mut chart = ChartBuilder::on(&root).margin(1)
        .set_all_label_area_size(50)
        .build_cartesian_2d(-3.4f32..3.4, -1.2f32..1.2f32)?;

    chart.configure_mesh().x_labels(20).y_labels(10).disable_mesh()
        .x_desc("X")
        .y_desc("Y")
        .label_style(("sans-serif", 20, &BLACK))
        .x_label_formatter(&|v| format!("{:.1}", v))
        .y_label_formatter(&|v| format!("{:.1}", v))
        .draw()?;
    chart.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.sin())), &RED))?
        .label("sin")
        .legend(|(x, y)| PathElement::new(vec![(x,y), (x+20,y)], &RED)); 
    chart.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.cos())), &BLUE))?
        .label("cos")
        .legend(|(x, y)| PathElement::new(vec![(x,y), (x+20,y)], &BLUE));
    chart.configure_series_labels()
        .margin(10)
        .position(SeriesLabelPosition::UpperLeft)
        .border_style(&BLACK)
        .label_font(("Calibri", 20))
        .draw()?;
    Ok(())
}).style("width:60%")

here is the result, it seems that the alignment of right y-axis are affected by the negative sign: image

wangjiawen2013 commented 7 months ago

And here is another example: image