plotters icon indicating copy to clipboard operation
plotters copied to clipboard

[Feature Request]] Render multiline text for labels

Open vthriller opened this issue 5 years ago • 4 comments

Describe the bug

subj:

test

To Reproduce

I basically picked examples/stock.rs, switched it to DateTime and added x_label_formatter:

use chrono::offset::{Local, TimeZone};
use chrono::{DateTime, Duration};
use plotters::prelude::*;
fn parse_time(t: &str) -> DateTime<Local> {
    Local
        .datetime_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
        .unwrap()
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let data = get_data();
    let root = BitMapBackend::new("test.png", (800, 480)).into_drawing_area();
    root.fill(&WHITE)?;

    let (to_date, from_date) = (
        parse_time(&data[0].0) + Duration::days(1),
        parse_time(&data[5].0) - Duration::days(1),
    );

    let mut chart = ChartBuilder::on(&root)
        .x_label_area_size(40)
        .y_label_area_size(40)
        .build_ranged(from_date..to_date, 110f32..135f32)?;

    chart.configure_mesh()
        .x_label_formatter(&|x: &DateTime<Local>| x.format("%Y-%m-%d\n%H:%M").to_string())
        .line_style_2(&WHITE).draw()?;

    chart.draw_series(
        data.iter()
            .map(|x| CandleStick::new(parse_time(x.0), x.1, x.2, x.3, x.4, &GREEN, &RED, 15)),
    )?;

    Ok(())
}

fn get_data() -> Vec<(&'static str, f32, f32, f32, f32)> {
    return vec![
        ("2019-04-25", 130.0600, 131.3700, 128.8300, 129.1500),
        ("2019-04-24", 125.7900, 125.8500, 124.5200, 125.0100),

        ("2019-04-08", 119.8100, 120.0200, 118.6400, 119.9300),
        ("2019-04-05", 119.3900, 120.2300, 119.3700, 119.8900),

        ("2019-03-15", 115.3400, 117.2500, 114.5900, 115.9100),
        ("2019-03-14", 114.5400, 115.2000, 114.3300, 114.5900),
    ];
}

Version Information

$ grep plotters Cargo.toml 
plotters = "0.2"
$ grep -A1 plotters Cargo.*
Cargo.lock:name = "plotters"
Cargo.lock-version = "0.2.12"

vthriller avatar Mar 07 '20 03:03 vthriller

(I'm going to change formatting string anyway, so feel free to take your time or dismiss this bug altogether.)

vthriller avatar Mar 07 '20 03:03 vthriller

Hi @vthriller ,

Sorry for the delay, haven't a chance to look at the issue until now. But currently this is expected, due to the label is rendered as single line text, which means even '\n' is treated as an normal char.

But it seems reasonable for multi-line label text.

Thanks for the issue, Cheers, Hao

38 avatar Apr 01 '20 18:04 38

Hi -- just curious is this is something that might be in the queue for development or if there's a reasonable workaround? Lack of multiline labels makes working with dates pretty hard unless you dial down the granularity of the labels a lot. :(

sjml avatar Jan 07 '23 19:01 sjml

@sjml plotters is effectively in maintenance mode currently. There are some PRs that get merged here and there but I struggle to find time to contribute (at least I manage to review PRs) and the other maintainers are even less active. I hope this will change soon and new people will step up to take care of moving the project forward.

AaronErhardt avatar Jan 09 '23 16:01 AaronErhardt