I can't get plotters to render the x_labels for large timespans, no matter how I tweak the parameters. For short timespans it seems fine:
fn part_graph_view(part: &Part) -> Result<Node<Msg>, Box<dyn std::error::Error>> {
use plotters::prelude::*;
let mut buffer = Vec::new();
{
let svg = SVGBackend::with_buffer(&mut buffer, (480, 320));
let root = svg.into_drawing_area();
root.fill(&WHITE).unwrap();
let beginning = Utc.from_utc_date(&part.defined);
let today = Utc::today();
let (_qty, max_qty, quantities, counts) = part.entries.iter().fold(
(0_u64, 0_u64, Vec::new(), Vec::new()),
|(qty, max_qty, mut quantities, mut counts), entry| {
let qty = match &entry.directive {
Directive::Add(d) => {
let qty = qty.saturating_add(d.amount);
quantities.push((Utc.from_utc_date(&entry.date), qty));
qty
}
Directive::Take(d) => {
let qty = qty.saturating_sub(d.amount);
quantities.push((Utc.from_utc_date(&entry.date), qty));
qty
}
Directive::Count(d) => {
counts.push((Utc.from_utc_date(&entry.date), d.amount));
qty
}
_ => qty,
};
(qty, max_qty.max(qty), quantities, counts)
},
);
let mut chart = ChartBuilder::on(&root)
.caption(part.part_number, ("Arial", 20).into_font())
.margin(30)
.x_label_area_size(20)
.y_label_area_size(20)
.build_ranged(beginning..today, 0..max_qty)?;
// TODO: report errors
chart
.configure_mesh()
.label_style(("Arial", 5).into_font())
.x_label_formatter(&|date| date.format("--%m-%d").to_string())
.draw()?;
chart.draw_series(LineSeries::new(quantities, &RED))?;
chart.draw_series(PointSeries::of_element(
counts,
1,
ShapeStyle::from(&GREEN).filled(),
&|coord, size, style| {
EmptyElement::at(coord)
+ Circle::new((0, 0), size, style)
+ Text::new(
format!("{}, {}", coord.0.naive_utc(), coord.1),
(0, -5),
("Arial", 5).into_font(),
)
},
))?;
}
let svg_text = std::str::from_utf8(&buffer)?;
Ok(div![El::from_html(svg_text)])
}
This code sample isn't really a minimum code sample reproducing the error. I plan on making one soon.