Crate eb_bars

Source
Expand description

EB - Bars: Plotting library for Rust providing a simple way to create barcharts in svg format.

§Quick Start

The simplest usecase (which you never really might want) is written like so.

use eb_bars::BarPlot;

// Start out with an empty plot.
let mut plot = BarPlot::new();

// Add a set of values.
plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);

// Render to svg format.
let svg: String = plot.to_svg(1600, 1000);

§But the above “Quick Start” looks bad and boring

As the above example stands, the largest number will have its bar take up the full height of the window. Also, the lowest value will be a bar of zero height e.g. no bar at all. The problem is that we have not set a specific scaling for the values. This means that the bars are all scaled based on the minimum and maximum value. Let’s improve it a bit in the next section by adding a scale.

use eb_bars::BarPlot;

let mut plot = BarPlot::new();

// Add same values as before.
plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);

// Here, we are setting a scale range for better visibility/scaling of bars.
plot.set_scale_range(0, 20, 2);
// The bars will look better, but the numbers on the scale won't be visible.
// We need to shrink the plot window relative to the full window.
// Keep in mind that all size and offset values are based of a percentage.
// Setting a width to 100 means it takes up the whole width.
// Same goes for the height.

// Let's shrink the plot size.
plot.set_plot_window_size(95.0, 85.0, 93.0, 50.0);
// We have now set the width at 95% and moved it 85% right from the left side.
// We also set the height at 93% and moved it 50% down from the top.
// First two parameters affect the width and the left/right offset respectively.
// The last two parameters affect the height and the top/bottom offset respectively.

// Let's render the svg.
let svg: String = plot.to_svg(1600, 1000);

It is still kinda boring, so please checkout all the tweaks available in BarPlot.

§Important note

If the method name is prefixed add_, then calling it multiple times will add stuff to the plot. This goes for adding multiple sets of values (categories) and adding colors to those values etc..

If the method name is prefixed set_, then calling it multiple times will override the previous one. You most certainly never want to call these more than once, unless there is a good reason to.

Check out BarPlot for all implementations.

§Panics and error handling.

This library has very limited error handling at the moment. Actually, it has none. There are some assertions here and there that will provoke a panic on invalid input. That way, you can try-and-re-try your code until it works.

Once everything works, it is very unlikely that it will panic on continous use in your application. However, if you pass values that are generated from a source that you do not have full control over, then the task of making sure the input is sanitized and double checked lies on your end and your code.

Structs§

BarPlot