Crate flot [] [src]

Flot is a JavaScript library for generating attractive data plots. Although usually used to enhance interactive websites, flot-rs is a nice way for command-line programs to create standalone HTML documents with plots. By default these refer to online sources, so they can be handed over to anybody else for display.

extern crate flot;

fn main() {
    let line_data = vec![(0.0,1.0),(1.0,4.5)];
    let points_data = vec![(0.5,1.2),(0.8,4.0)];

    let page = flot::Page::new("");

    let p = page.plot("Lines and Points");
    p.lines("lines",line_data).fill(0.3).line_width(0);
    p.points("points",points_data).symbol("circle");

    page.render("simple.html").expect("i/o error");
}

A Page may contain multiple plots; plots may contain multiple series with chart types (lines,points,bars).

The result of running this program is to create 'simple.html', which can be opened in your browser.

Page can be given a title, which if non-empty will both set the title of the document and create a H1 heading. Likewise, the plot method is given a title which if non-empty will provide a centered H2 heading for the plot.

Ways of specifying Data

By default, the series constructors take anything that converts to an iterator of (f64,f64) x-y pairs. Note that the vectors line_data and points_data are consumed by these calls.

If you have a source of tuples that isn't (f64,f64), then flot::to_f64 will convert that into a form that flot-rs accepts, provided that those types convert cleanly into f64.

Alternatively, you can map a iterator of references with a function - flot::mapr produces the desired points iterator, which here we collect into a vector.

extern crate flot;

fn make_gaussian(xvalues: &[f64], m: f64, s: f64) -> Vec<(f64,f64)> {
    use std::f64::consts::PI;
    let s2 = 2.0*s*s;
    let norm = 1.0/(s2*PI).sqrt();
    flot::mapr (
        xvalues,
        move |x| norm*(-(x-m).powi(2)/s2).exp()
    ).collect()
}

fn main() {
    let page = flot::Page::new("");

    let p = page.plot("Normal distribution").size(500,300);
    let xvalues: Vec<_> = flot::range(0.0,10.0,0.1).collect();
    p.lines("norm σ=1.0",make_gaussian(&xvalues,5.0,1.0));
    p.lines("norm σ=0.7",make_gaussian(&xvalues,6.0,0.5));

    page.render("normal.html").unwrap();
}

range is a little convenience iterator for making ranges of floating-point values (subsequently I've discovered that the itertools-num crate provides something similar - see linspace).

flot::mapv is similar, except it takes an iterator of values. Here are the squares of all integers from 0 to 9:

Be careful when using this code, it's not being tested!
page.plot().legend_pos(Corner::TopLeft)
        .bars("squares",mapv(0..10,|x| x*x))
        .width(0.75);

(The iterator given to mapr and mapv can provide any values which can be converted into a f64, so the integer range works.)

Finally, flot::zip can take two iterators of references, which are zipped together into point tuples. This is useful if you have separate x and y data as slices or vectors.

Structs

Axis

represents an axis

FRange

Iterator type for floating-point range iterator

Grid

represents the grid area of the plot

Legend

represents the legend of the plot

Markings

represents 'markings' or plot annotations.

Page

represents an HTML document containing plots

Plot

represents a particular plot

Series

describes a data series which can be plotted either as lines, points or bars

Enums

Corner

describes position of legend (None for no legend)

Side

describes sides of plot for axis position

Functions

mapr

map an iterator of references with a function producing point tuples. Like zip, the reference type can be anything that converts to f64

mapv

map an iterator of values with a function producing point tuples. The value type can be anything that converts to f64

range

generates an iterator between x1 and x2, step skip over floating point numbers. Similar to linspace in the itertools-num crate

to_f64

take an iterator of references to tuples of two types and produce point tuples. The types can be anything that converts to f64

valr

data from an iterator of references plotted against index. Like zip, the reference type can be anything that converts to f64

zip

join two iterators of references together to produce point tuples. The reference types can be anything that converts to f64