Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for bar graphs. Helps with issue #98 #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

matiu2
Copy link

@matiu2 matiu2 commented Feb 12, 2020

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)),
        )?;

@codecov
Copy link

codecov bot commented Feb 12, 2020

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%) ⬇️
src/coord/logarithmic.rs 0.00% <0.00%> (-60.00%) ⬇️
src/style/font/ttf.rs 72.52% <0.00%> (-7.91%) ⬇️
src/series/line_series.rs 71.87% <0.00%> (-4.60%) ⬇️
src/drawing/rasterizer/line.rs 67.34% <0.00%> (-3.63%) ⬇️
src/element/boxplot.rs 77.77% <0.00%> (-2.51%) ⬇️
src/drawing/backend.rs 58.82% <0.00%> (-2.36%) ⬇️
src/style/font/font_desc.rs 47.67% <0.00%> (-2.33%) ⬇️
src/drawing/backend_impl/bitmap.rs 83.21% <0.00%> (-2.32%) ⬇️
src/drawing/backend_impl/svg.rs 73.47% <0.00%> (-2.07%) ⬇️
... 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.

@matiu2 matiu2 force-pushed the master branch 2 times, most recently from 8d20955 to a06f868 Compare February 12, 2020 13:54
@38
Copy link
Member

38 commented Feb 12, 2020

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!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants