Skip to content

Support for downsampling #722

Open
Open

Description

@viktaur

Provide a ChartContext method to downsample the representation of the source data to a custom bucket size (not sure if something like this exists already, but could not find it).

Sometimes we are dealing with data sampled very frequently over long periods of time, which can make our chart look too cluttered.

Image

To fix this in one of my projects, I wrote the following function for DateTime data (x axis).

fn downsample(
    data: &[(DateTime<Utc>, f64)],
    bucket_size: Duration,
) -> Vec<(DateTime<Utc>, f64)> {
    if data.is_empty() {
        return vec![];
    }

    let mut buckets: Vec<(DateTime<Utc>, f64)> = Vec::new();
    let mut bucket_start = data[0].0;
    let mut sum = 0.0;
    let mut count = 0u32;

    for &(dt, val) in data {
        if dt >= bucket_start + bucket_size {
            if count > 0 {
                let mid = bucket_start + bucket_size / 2;
                buckets.push((mid, sum / count as f64));
            }
            bucket_start = dt;
            sum = 0.0;
            count = 0;
        }
        sum += val;
        count += 1;
    }

    // Last bucket
    if count > 0 {
        let mid = bucket_start + bucket_size / 2;
        buckets.push((mid, sum / count as f64));
    }

    buckets
}

Then you just pass the new bucket size, how often you want a sample to be displayed.

let data = downsample(&data, Duration::hours(12));

chart
    .draw_series(LineSeries::new(data, color.stroke_width(2)))?
    .label(position.to_string())
    .legend(move |(x, y)| {
        PathElement::new(vec![(x, y), (x + 20, y)], color.stroke_width(2))
    });

Final result:

Image

Activity

  1. nielsle commented on Feb 16, 2026

    @nielsle

    Hmm... the downsample function doesn´t require plotters, so alternatively it could just be published it as a separate library.

  2. viktaur commented on Feb 16, 2026

    @viktaur
    Author

    But it has a direct impact on how the chart displays the data.., and reducing the number of plotted samples seems like something people would need to do frequently

  3. el-hult commented on Mar 18, 2026

    @el-hult
    Contributor

    Downsampling is tricky business. In my experience (mostly python), plotting libraries don't go into the subsampling aspects because it can distort the data in unexpected ways. In the python ecosystem, I think only Datashaders do the subsampling for you. And you have to chose how. Read the example on https://datashader.org/user_guide/Timeseries.html#create-some-fake-timeseries-data for some context.

    IMHO, plotters should not do subsampling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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