plotters icon indicating copy to clipboard operation
plotters copied to clipboard

How to create a bar chart with string/labels instead of numbers for indexes?

Open capveg-netdebug opened this issue 2 years ago • 1 comments

Apologies - I've read over a lot of the docs and examples and it seems like I should be able to do this, but I can't seem to figure it out. This example histogram seems the closest : https://plotters-rs.github.io/book/basic/basic_data_plotting.html#visualize-distribution - but I don't want a histogram, I want to plot the raw data. My data looks effectively like:

{ "apples" : 20, "pears": 15, "bananas": 5, }

FYI: I'd really prefer a stacked barchart but I see from the other issues that's an outstanding feature request.

If folks could provide a pointer, it would be much appreciated - thank you in advance!

capveg-netdebug avatar Jul 06 '23 14:07 capveg-netdebug

You can use chart.x_label_formatter(), like this:

let names = ["apples", "pears", "bananas"];
chart.x_label_formatter(&|x| match x {
    SegmentValue::CenterOf(v) => names[*v as usize].clone(),
    _ => "UNK".to_string(),
});

AliMMehr avatar Nov 06 '23 08:11 AliMMehr