plotters icon indicating copy to clipboard operation
plotters copied to clipboard

Support for bar graphs. Helps with issue #98

Open matiu2 opened this issue 5 years ago β€’ 2 comments

I'll admit it's not a very elegant pull request and won't be offended if you completely redo it.

It allows you to use Category axis in histograms by making them implement PartialEq, Eq, Hash, and DiscreteRanged.

When it implements DiscreteRanged, it just wraps around the ends of the category.

Rough usage example:

        let x_labels = vec!["< 1", "1-3", "3-6", "6-9", "> 9"];
        let mut data: Vec<usize> = Vec::with_capacity(x_labels.len());
        data.push(days.iter().take_while(|&&days| days < 1).count() as usize);
        data.push(days.iter().skip_while(|&&days| days < 1).take_while(|&&days| days < 3).count());
        data.push(days.iter().skip_while(|&&days| days < 3).take_while(|&&days| days < 6).count());
        data.push(days.iter().skip_while(|&&days| days < 6).take_while(|&&days| days < 9).count());
        let max_tickets: usize = *data.iter().max().unwrap();

        chart
            .configure_mesh()
            // Don't show a grid for the x axis
            .disable_x_mesh()
            // Make the y axis grid be light gray
            .line_style_1(&WHITE.mix(0.3))
            .x_desc("Biggest gap in days")
            .y_desc("Interaction Count")
            .axis_desc_style(("sans-serif", 15).into_font())
            .draw()?;

        chart.draw_series(
            Histogram::vertical(&chart)
                .style(RED.mix(0.5).filled())
                .data(x_labels.iter().flat_map(|x| category.get(x)).zip(data)),
        )?;

matiu2 avatar Feb 12 '20 12:02 matiu2

Codecov Report

Merging #111 (a06f868) into master (dee7727) will decrease coverage by 1.37%. The diff coverage is 13.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #111      +/-   ##
==========================================
- Coverage   67.48%   66.11%   -1.38%     
==========================================
  Files          53       53              
  Lines        5160     5152       -8     
==========================================
- Hits         3482     3406      -76     
- Misses       1678     1746      +68     
Impacted Files Coverage Ξ”
src/coord/category.rs 62.06% <13.63%> (-16.72%) :arrow_down:
src/coord/logarithmic.rs 0.00% <0.00%> (-60.00%) :arrow_down:
src/style/font/ttf.rs 72.52% <0.00%> (-7.91%) :arrow_down:
src/series/line_series.rs 71.87% <0.00%> (-4.60%) :arrow_down:
src/drawing/rasterizer/line.rs 67.34% <0.00%> (-3.63%) :arrow_down:
src/element/boxplot.rs 77.77% <0.00%> (-2.51%) :arrow_down:
src/drawing/backend.rs 58.82% <0.00%> (-2.36%) :arrow_down:
src/style/font/font_desc.rs 47.67% <0.00%> (-2.33%) :arrow_down:
src/drawing/backend_impl/bitmap.rs 83.21% <0.00%> (-2.32%) :arrow_down:
src/drawing/backend_impl/svg.rs 73.47% <0.00%> (-2.07%) :arrow_down:
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data Powered by Codecov. Last update dee7727...12f8430. Read the comment docs.

codecov[bot] avatar Feb 12 '20 13:02 codecov[bot]

Thanks for the PR.

I think the PR looks good to me. I am currently working on a new major release shipping with discrete coordinate trait refactor #89 and slice coordinate support #104 . With those changes, Category will be removed and slice will be able to use as coordinate spec directly. (Actually the discrete coordinate trait has been messed up for a long time)

One of the related example on this change would be https://github.com/38/plotters/blob/v0.3-pre/examples/nested_coord.rs Though it's not really the bar chart but roughly shows how things work.

And here's a quick example use v0.3:

use plotters_bitmap::BitMapBackend;
use plotters::prelude::*;
fn main() {
    let mut root = BitMapBackend::new("out.png", (800, 600)).into_drawing_area();

    root.fill(&WHITE);
    let data = ["a", "b", "c", "d", "e"];
    let mut chart = ChartBuilder::on(&root)
        .set_all_label_area_size(30)
        .build_ranged((&data[..]).into_centric(), 0..10)
        .unwrap();

    chart.configure_mesh().disable_mesh().draw();

    let hist = Histogram::vertical(&chart).data(vec![(&"a", 5), (&"b", 2), (&"c", 8), (&"d", 7), (&"e", 4)]).margin(15);

    chart.draw_series(hist);
}

image

So if my understand is right, it seems this PR will provide the exactly same feature.

Sorry for the confusion. Thanks again for the contribution!

Cheers!

38 avatar Feb 12 '20 18:02 38