Skip to content

[Feature Request]] Render multiline text for labels #113

Open
@vthriller

Description

@vthriller

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"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions