FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[BUG] Zero division panic with datetime axis and short span of data · Issue #709 · plotters-rs/plotters · GitHub

[BUG] Zero division panic with datetime axis and short span of data #709

Description

Describe the bug

I was trying to draw a graph of temperature changes via plotters-iced when it crashed with a similar stacktrace to the following:

» RUST_BACKTRACE=1 cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/repro`

thread 'main' (60058) panicked at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/coord/ranged1d/types/datetime.rs:253:24:
attempt to divide by zero
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:698:5
   1: core::panicking::panic_fmt
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:80:14
   2: core::panicking::panic_const::panic_const_div_by_zero
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:180:17
   3: <plotters::coord::ranged1d::types::datetime::RangedDate<D> as plotters::coord::ranged1d::Ranged>::key_points
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/coord/ranged1d/types/datetime.rs:253:24
   4: <plotters::coord::ranged1d::types::datetime::RangedDateTime<DT> as plotters::coord::ranged1d::Ranged>::key_points
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/coord/ranged1d/types/datetime.rs:701:14
   5: plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>::draw_mesh
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/coord/ranged2d/cartesian.rs:57:26
   6: plotters::drawing::area::DrawingArea<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>::draw_mesh::{{closure}}
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/drawing/area.rs:202:18
   7: plotters::drawing::area::DrawingArea<DB,CT>::backend_ops
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/drawing/area.rs:282:13
   8: plotters::drawing::area::DrawingArea<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>::draw_mesh
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/drawing/area.rs:200:14
   9: plotters::chart::context::cartesian2d::draw_impl::<impl plotters::chart::context::ChartContext<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>>::draw_mesh_lines
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/chart/context/cartesian2d/draw_impl.rs:36:27
  10: plotters::chart::context::cartesian2d::draw_impl::<impl plotters::chart::context::ChartContext<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>>::draw_mesh
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/chart/context/cartesian2d/draw_impl.rs:341:18
  11: plotters::chart::mesh::MeshStyle<X,Y,DB>::draw
             at /home/tux/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/chart/mesh.rs:467:16
  12: repro::main
             at ./src/main.rs:12:28
  13: core::ops::function::FnOnce::call_once
             at /home/tux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

https://github.com/plotters-rs/plotters/blob/master/plotters/src/coord/ranged1d/types/datetime.rs#L253
[plotters/src/coord/ranged1d/types/datetime.rs:251:9] &total_weeks = -1
[plotters/src/coord/ranged1d/types/datetime.rs:252:9] &max_points = 110
[plotters/src/coord/ranged1d/types/datetime.rs:255:9] &week_per_point = 0

this issue seems very related to #190.
This should be a valid use-case, right? At least it should not crash like this

To Reproduce

Repro without iced:

// cargo add chrono plotters

use chrono::{TimeZone, Utc};
use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let root = BitMapBackend::new("0.png", (640, 480)).into_drawing_area();

    let newest_time = Utc.with_ymd_and_hms(2025, 12, 29, 0, 0, 0).unwrap();
    let oldest_time = Utc.with_ymd_and_hms(2026, 1, 4, 23, 0, 0).unwrap();

    let mut chart = ChartBuilder::on(&root).build_cartesian_2d(oldest_time..newest_time, 0..10)?;

    chart.configure_mesh().draw()?;
    Ok(())
}

Version Information

chrono = "0.4.42"
plotters = "0.3.7"
rustc 1.92.0 (ded5c06cf 2025-12-08)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL