Documentation for the "rstk::chart" module - a wrapper around tklib’s Plotchart library.

The following examples should help in getting started with using plotchart through rstk - but also see the API documentation.

For more information on plotchart itself, see:

  • the plotchart documentation, which, although out of date in places, should be read in parallel with these notes;

  • the examples provided below, many of which translate the tcl examples; or even

  • the tklib/plotchart source.

Note Many options in plotchart are not available in rstk.

1. Getting Started

For installation, see rstk - you must have "tklib" installed to use this module.

All of the examples here (and more) are available for download:

After unzipping the archive, the file structure is:

$ tree .
.
├── Cargo.toml
├── examples
│   ├── bar_chart.rs
│   ├── box_chart.rs
|   ... etc

To run the bar-chart example, use:

$ cargo run --example bar_chart

The "rstk" crate will be installed when you run your first example.

Important To save some repetition, examples written without main will assume the following template:
use rstk::*;

fn main() {
    let root = rstk::start_wish().unwrap();

    // PASTE THE EXAMPLE HERE                   1

    rstk::mainloop();
}
1 Replace this comment with the example code.

2. Plotchart

The TkPlotchart trait is used by all charts and provides methods for:

  • setting the title text;

  • setting axis titles, definitions and presence of tickmarks;

  • balloon text (text with a pointer to a location on the chart);

  • plain text (text placed directly on the chart);

  • control over the chart legend;

  • saving the chart to a postscript file;

  • and others …​

Some top-level functions, like after and windowing_system, are listed under widget.

3. Bar Chart

Bar charts can display bars vertically or horizontally.

3.1. Vertical bar chart

Documentation on:

example Example ("bar_chart.rs"):

root.title("Bar Chart from rstk");

let canvas = rstk::make_canvas(&root);
canvas.height(400);
canvas.width(400);
canvas.background("white");
canvas.grid().layout();

let bar_chart = rstk::make_bar_chart(&canvas,                     1
                                     &["2018", "2019", "2020", "2021"],
                                     (0.0, 30.0, 5.0),
                                     rstk::BarSeries::Count(3),
                                     0.0);
bar_chart.title("Book Reading History", rstk::Justify::Centre);   2
bar_chart.x_title("Year");
bar_chart.v_title("Number of Books");

bar_chart.show_values(true);                                      3
bar_chart.plot("person-1", &[3.0, 6.0, 9.0, 12.0], "blue");       4
bar_chart.plot("person-2", &[10.0, 7.0, 11.0, 15.0], "red");
bar_chart.plot("person-3", &[25.0, 18.0, 21.0, 22.0], "green");

bar_chart.legend_position(rstk::Position::TopRight);
bar_chart.legend("person-1", "Mary");                             5
bar_chart.legend("person-2", "Peter");
bar_chart.legend("person-3", "Shalini");
1 Creates an instance of the bar chart, with years to label the x-axis and a min-max-step triple to define the y-axis values. It’s important to get the number of series right, for the spacing.
2 These three lines set the title and axis-titles for the plot.
3 Tells the bar chart to display the values above the bars.
4 Add each set of values in turn - the "series" label is for reference only.
5 We only get a legend if we attach a name to the "series" label.
rstk bar chart example
Figure 1. Example of bar chart

3.2. Horizontal Bar Chart

Documentation on:

example Example ("horizontal_bar_chart.rs"):

root.title("Horizontal Bar Chart from rstk");

let canvas = rstk::make_canvas(&root);
canvas.height(400);
canvas.width(400);
canvas.background("white");
canvas.grid().layout();

let bar_chart = rstk::make_horizontal_bar_chart(&canvas,
                                                (0.0, 50.0, 5.0),
                                                &["2018", "2019", "2020", "2021"],
                                                rstk::BarSeries::Stacked);  1
bar_chart.title("Book Reading History", rstk::Justify::Centre);
bar_chart.x_title("Number of Books");
bar_chart.v_title("Year");

bar_chart.show_values(true);
bar_chart.plot("type-1", &[3.0, 6.0, 9.0, 12.0], "blue");
bar_chart.plot("type-2", &[10.0, 7.0, 11.0, 15.0], "red");
bar_chart.plot("type-3", &[25.0, 18.0, 21.0, 22.0], "green");

bar_chart.legend_position(rstk::Position::BottomRight);                     2
bar_chart.legend_spacing(12);
bar_chart.legend("type-1", "Computing");
bar_chart.legend("type-2", "Fiction");
bar_chart.legend("type-3", "Technical");
1 This time, stack the bars.
2 Adjust the position and spacing of items in the legend, for clarity.
rstk horizontal bar chart example
Figure 2. Example of horizontal bar chart

4. Box Plot

Box plots can display bars vertically or horizontally.

Documentation on:

example Example ("box_plot.rs"):

root.title("Box Plot in rstk");
let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.grid().layout();

let box_plot = rstk::make_horizontal_box_plot(&canvas,
                                              (0.0, 40.0, 5.0),
                                              &["A", "B", "C", "D", "E"]);
box_plot.title("Box plot example", rstk::Justify::Left);

box_plot.plot("data1", "A", &[0.0, 1.0, 2.0, 5.0, 7.0, 1.0, 4.0, 5.0, 0.6,  1
                              5.0, 5.5]);
box_plot.plot("data2", "C", &[2.0, 2.0, 3.0, 6.0, 1.5, 3.0]);
box_plot.plot("data3", "E", &[2.0, 3.0, 3.0, 4.0, 7.0, 8.0, 9.0, 9.0, 10.0,
                              10.0, 11.0, 11.0, 11.0, 14.0, 15.0, 17.0, 17.0,
                              20.0, 24.0, 29.0]);
1 The box-plot function determines its plotted form from a list of numbers.
rstk box plot example
Figure 3. Example of box plot

5. Gantt Chart

Documentation on:

After calling the above constructor, use a "builder" style to add options to the chart, before calling plot to finally construct it. The options are defined in:

example Example ("gantt_chart.rs"):

root.title("Gantt Chart in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let gantt = rstk::make_gantt_chart(&canvas, "1 March 2021", "10 April 2021")  1
    .num_items(5)
    .ylabel_width(15)
    .plot();
gantt.milestone("Vacation Start", "20 March 2021", "red");                    2
let task_1 = gantt.task("rust", "1 March 2021", "15 March 2021", 30);         3
let task_2 = gantt.task("chart", "15 March 2021", "31 March 2021", 80);
let task_3 = gantt.task("examples", "7 March 2021", "31 March 2021", 50);
let task_4 = gantt.task("documentation", "15 March 2021", "31 March 2021", 20);

gantt.connect(&task_1, &task_2);                                              4
gantt.connect(&task_3, &task_4);

gantt.draw_line("1 Mar", "1 March 2021", "blue");                             5
gantt.draw_line("8 Mar", "8 March 2021", "green");
gantt.draw_line("15 Mar", "15 March 2021", "green",);
gantt.draw_line("22 Mar", "22 March 2021", "green");
gantt.draw_line("1 Apr", "1 April 2021", "blue");

gantt.title("Learning Rust/Tk", rstk::Justify::Right);
gantt.uncompleted_colour("red");
1 It is useful to give the chart more space than your tasks, to fit the information.
2 Milestones are set for specific dates.
3 Tasks are set with a start and end date, and a percentage completed.
4 Connect tasks, to show a dependency.
5 Lines are useful to highlight times.
rstk gantt chart example
Figure 4. Example Gantt Chart

6. Histogram

Documentation on:

After calling the above constructor, use a "builder" style to add options to the chart, before calling plot to finally construct it. The options are defined in:

Once constructed, Tk’s "dataconfig" functions are replaced by methods in TkChartSeries.

example Example ("histogram.rs"):

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let histogram = rstk::make_histogram(&canvas, (0.0, 10.0, 1.0), (0.0, 100.0, 5.0))
    .plot();
histogram.title("Example Histogram", rstk::Justify::Centre);
histogram.v_title("Cumulative Frequency");
histogram.x_title("Event");
histogram.series_colour("data", "green");
histogram.series_style("data", rstk::HistogramStyle::Filled);
histogram.series_fill_colour("data", "green");

for i in (2..=10).step_by(2) {
    let i = i as f64;
    histogram.plot_cumulative("data", (i, i*3.0));          1
}
1 Notice that i defines the right hand edge value of each bar.
rstk histogram example
Figure 5. Example of a histogram

7. Isometric Plot

Documentation on:

example Example ("isometric_plot.rs"):

root.title("Isometric Plot in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let iso_plot = rstk::make_isometric_plot(&canvas,
                                         (0.0, 100.0),
                                         (0.0, 200.0),
                                         rstk::StepSize::NoAxes);
iso_plot.rectangle((10.0, 10.0), (50.0, 50.0), "green");
iso_plot.filled_rectangle((20.0, 20.0), (40.0, 40.0), "red");
iso_plot.filled_circle((70.0, 70.0), 40.0, "yellow");
iso_plot.circle((70.0, 70.0), 42.0, "black");
rstk isometric plot example
Figure 6. Example of isometric plot

8. Pie Chart

Documentation on:

example Example ("pie_chart.rs"):

root.title("Pie Chart in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(500);
canvas.height(300);
canvas.background("white");
canvas.grid().layout();

let pie_chart = rstk::make_pie_chart(&canvas);
pie_chart.title("Books Read per Category", rstk::Justify::Centre);
pie_chart.plot(&[("Computing", 3.0), ("Fiction", 10.0), ("Technical", 25.0)]);  1
pie_chart.explode(0);                                                           2
1 List of (label value) pairs defines the three slices
2 Highlights the first slice
rstk pie chart example
Figure 7. Example of a pie chart

8.1. Spiral Pie

A variation on the pie chart, where the values specify a radius instead of an angle.

Documentation on:

example Example ("spiral_pie_chart.rs"):

root.title("Spiral Pie Chart in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(500);
canvas.height(300);
canvas.background("white");
canvas.grid().layout();

let spiral_pie = rstk::make_spiral_pie_chart(&canvas);
spiral_pie.colours(&["yellow", "blue", "red"]);           1

spiral_pie.title("Books Read per Category", rstk::Justify::Centre);
spiral_pie.plot(&[("Computing", 3.0), ("Fiction", 10.0), ("Technical", 25.0)]);
1 Specify the colours to use, in slice order.
rstk spiral pie chart example
Figure 8. Example of a spiral pie chart

9. Polar Plot

Documentation on:

example Example ("polar_plot.rs"):

root.title("Polar Plot in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(300);
canvas.background("white");
canvas.grid().layout();

let polar_plot = rstk::make_polar(&canvas, (3.0, 1.0));

polar_plot.series_colour("line", "blue");                                 1
polar_plot.series_drawing_mode("line", rstk::DrawingMode::Both);
polar_plot.series_symbol("line", rstk::Symbol::Cross, 5);

for i in 0..30 {
    let i = i as f64;
    let r = i/10.0;
    let a = i*24.0;

    polar_plot.plot("line", (r, a));
}

polar_plot.draw_labelled_dot((2.0, 60.0), "Mark", rstk::Location::North); 2
1 The line drawn on a polar plot can be configured with colour and symbol type
2 Draw a label/dot on the plot
rstk polar plot example
Figure 9. Example of a polar plot

10. Radial Chart

Documentation on:

example Example ("radial_chart.rs"):

root.title("Radial Chart in rstk");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let radial_chart = rstk::make_radial_chart(&canvas,                         1
                                           &["Mon", "Tue", "Wed", "Thu", "Fri"],
                                           10.0,
                                           rstk::RadialStyle::Lines);
radial_chart.plot(&[5.0, 8.0, 4.0, 7.0, 10.0], "green", 2);                 2
radial_chart.plot(&[2.0, 4.0, 1.0, 3.0, 5.0], "blue", 2);
1 Constructor gives labels for the spokes and scale is used for radius.
2 Plot adds a line of data to the chart.
rstk radial chart example
Figure 10. Example of a radial chart

11. Right Axis

Documentation on:

example Example ("right_axis.rs"):

root.title("Right axis example");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let xy_plot = rstk::make_x_y(&canvas, (0.0, 10.0, 1.0), (0.0, 100.0, 10.0))
    .plot();
let right_axis = rstk::make_right_axis(&canvas, (0.0, 3.0, 0.5));     1

xy_plot.v_title("y = x*x");
right_axis.v_title("y = sqrt(x)");                                    2

xy_plot.series_colour("squares", "blue");
right_axis.series_colour("roots", "green");                           3
right_axis.series_drawing_mode("roots", rstk::DrawingMode::Both);
right_axis.series_symbol("roots", rstk::Symbol::UpFilled, 5);

for i in 0..10 {
    let i = i as f64;
    xy_plot.plot("squares", (i, i*i));
    right_axis.plot("roots", (i, i.sqrt()));                          4
}
1 Right axis is added to an existing plot, but has its own y-axis range.
2 Separate labels can be used for the main plot and the right axis.
3 And values plotted against the right axis can have their own configuration.
4 Use the plot method to add data to the right-axis plot.
rstk right axis example
Figure 11. Example of right axis

12. Status Timeline

Documentation on:

example Example ("plotdemos18.rs") is a conversion of a tklib example:

use rstk::*;
use rand::Rng;

fn rand(upper: f64) -> f64 {
    rand::thread_rng().gen_range(0.0..upper)
}

fn main() {
    let root = rstk::start_wish().unwrap();
    root.title("plotdemos18.rs");

    let canvas = rstk::make_canvas(&root);
    canvas.width(600);
    canvas.height(400);
    canvas.background("white");
    canvas.grid().layout();

    let devices = vec!["e1", "e2", "e12231", "r1", "r2"];
    let timeline = rstk::make_status_timeline(&canvas,                1
                                              (0.0, 7200.0, 900.0),
                                              &devices,
                                              false);
    timeline.title("Operational over time", rstk::Justify::Centre);

    // add randomised data
    let mut last_i = 0.0;
    let mut i = rand(10.0);
    while i < 7200.0 {
        for item in devices.iter() {
            timeline.plot(&item, last_i, i,                           2
                          if rand(1.0) > 0.5 { "red" } else { "green" });
        }
        last_i = i;
        i += rand(600.0);
    }

    // add labelled vertical lines
    for x in (0..7200).step_by(900) {
        let text = format!("{:02}h:{:02}", x/3600, x % 60);
        if (x % 3600) == 0 {
            timeline.draw_line(&text, x as f64, "black",              3
                               rstk::ChartDash::Lines, 2.0);
        } else {
            timeline.draw_line("", x as f64, "grey", rstk::ChartDash::Dots3, 2.0);
        }
    }

    rstk::mainloop();
}
1 Creates the chart with labelled y-axis, and no x-axis showing
2 Adds a random 'patch' to the plot, in given series
3 Draws a vertical line, using given style
rstk status timeline
Figure 12. Example of status-timeline

13. Ternary Diagram

Documentation on:

example Example ("ternary_diagram.rs"), translation of tcl "test_ternary.tcl":

root.title("Ternary Diagram from rstk");

let canvas = rstk::make_canvas(&root);
canvas.height(500);
canvas.width(500);
canvas.grid().layout();

let ternary_diagram = rstk::make_ternary_diagram(&canvas, false, 5);
ternary_diagram.corner_titles("Component A", "Component B", "Component C");   1
ternary_diagram.plot("data", (50.0, 25.0, 25.0), "1", rstk::Direction::West); 2
ternary_diagram.plot("data", (20.0, 25.0, 55.0), "2", rstk::Direction::East); 3
ternary_diagram.draw_line("data", &[(0.0, 80.0, 20.0), (10.0, 20.0, 70.0)]);  4

ternary_diagram.series_colour("area", "green");                               5
ternary_diagram.series_smooth("area", true);
ternary_diagram.draw_filled_polygon("area", &[(0.0, 70.0, 30.0),
                                              (10.0, 20.0, 70.0),
                                              (0.0, 0.0, 100.0)]);
ternary_diagram.plot("area1", (0.0, 70.0, 30.0), "", rstk::Direction::West);
ternary_diagram.plot("area1", (10.0, 20.0, 70.0), "", rstk::Direction::West);
ternary_diagram.plot("area1", (0.0, 0.0, 100.0), "", rstk::Direction::West);

ternary_diagram.draw_ticklines("grey");                                       6
1 Labels the three corners of the diagram
2 Places a labelled 'dot' at given coordinates
3 Optional direction arranges label relative to the dot
4 Lines defined as points, each point a triple of values
5 Set some drawing properties
6 Adding ticklines results in a triangular grid over the diagram
rstk ternary diagram
Figure 13. Example of a ternary diagram

14. 3D Bar Chart

Documentation on:

example Example ("threed_bar_chart.rs"):

root.title("3D Bar Chart");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let bar_3d = rstk::make_3d_bar_chart(&canvas, (0.0, 60.0, 5.0), 7);

bar_3d.title("Number of Moons per Planet", rstk::Justify::Centre);
bar_3d.label_font(&rstk::TkFont {                                   1
    family: String::from("Times"),
    size: 8,
    ..Default::default()
});
bar_3d.show_background(true);

bar_3d.plot("Earth", 1.0, "blue");
bar_3d.plot("Mars", 2.0, "red");
bar_3d.plot("Jupiter", 53.0, "orange");
bar_3d.plot("Saturn", 53.0, "yellow");
bar_3d.plot("Uranus", 27.0, "green");
bar_3d.plot("Neptune", 13.0, "cyan");
bar_3d.plot("Pluto", 5.0, "grey");
1 Set some properties of the display.
rstk 3d bar chart
Figure 14. Example of a 3D bar chart

15. 3D Plot

Documentation on:

example Example (incomplete) taken from "plotdemos1.rs":

fn square(n: f64) -> f64 { n*n }
fn cowboyhat(x: f64, y: f64) -> f64 {
    let x1 = x/9.0;
    let y1 = y/9.0;

    3.0 * (1.0 - (square(x1) + square(y1))) * (1.0 - (square(x1) + square(y1)))
}

// upper plot in image below
fn canvas_10(root: &rstk::TkTopLevel) -> rstk::TkCanvas {
    let mut data: Vec<Vec<f64>> = vec![];
    for r in (-10..=10).step_by(2) {
        let r = r as f64;
        let mut row = vec![];

        for c in 0..=10 {
            let c = c as f64;
            row.push(cowboyhat(r, c));
        }
        data.push(row);
    }

    let canvas = make_white_canvas(root, 400, 300);

    let s = rstk::make_3d_plot(&canvas,                     1
                               (0.0, 10.0, 3.0),
                               (-10.0, 10.0, 10.0),
                               (0.0, 10.0, 2.5));
    s.title("3D Plot", rstk::Justify::Centre);
    s.plot_data(&data);                                     2

    canvas
}

// -- lower plot in image below
fn canvas_12(root: &rstk::TkTopLevel) -> rstk::TkCanvas {
    let canvas = make_white_canvas(root, 400, 250);

    let s = rstk::make_3d_plot_with_labels(&canvas,
                                           (0.0, 10.0, 3.0),
                                           (-10.0, 10.0, 10.0),
                                           (0.0, 10.0, 2.5),
                                           &["A", "B", "C"]);
    s.title("3D Plot - data", rstk::Justify::Centre);
    s.colours("green", "black");
    s.interpolate_data(&[[1.0, 2.0, 1.0, 0.0],            3
                       [1.1, 3.0, 1.1, -0.5],
                       [3.0, 1.0, 4.0, 5.0]],
                       &[0.0, 0.5, 1.0, 1.5, 2.0]);
    canvas
}
1 Creates a 3D plot with given axes
2 Uses a function to create an 11x11 array of values
3 Interpolate colours the data, according to the given contours
rstk threed plot example
Figure 15. Example of 3D plots

16. 3D Ribbon Plot

Documentation on:

example Example taken from "plotdemos15.rs" - not complete code:

let y_scale = (0.0, 40.0, 5.0);         // Y axis is miles along route
let z_scale = (900.0, 1300.0, 100.0);   // Z axis is altitude
// each duple is distance along route (Y) and its altitude (Z)
let yz = [
    ( 0.0,  971.0), ( 0.2,  977.0), ( 0.3,  981.0), ( 0.8, 1010.0), ( 1.0, 1022.0), ( 1.6, 1060.0),

  // SNIP 32 LINES!
];

let canvas2 = rstk::make_canvas(&root);
canvas2.width(800);
canvas2.height(400);
canvas2.background("#aaeeff");
canvas2.configure("border", "0");
canvas2.configure("highlightthickness", "0");
canvas2.pack().side(rstk::PackSide::Top).fill(rstk::PackFill::Both).expand(true).layout();

let s2 = rstk::make_3d_ribbon_plot(&canvas2, y_scale, z_scale); 1
s2.plot(&yz);                                                   2
1 Creates the ribbon-plot using axis definitions for y and z
2 Draws the ribbon, using values for y-z pairs
threed ribbon plot example
Figure 16. Example of 3D ribbon plot

17. Time Chart

Documentation on:

After calling the above constructor, use a "builder" style to add options to the chart, before calling plot to finally construct it. The options are defined in:

example Example taken from tcl "plotdemos7.tcl" ("time-chart-example.lisp"):

let canvas = make_canvas(&root);
canvas.width(400);
canvas.height(200);
canvas.background("white");
canvas.grid().layout();

let s = rstk::make_time_chart(&canvas,
                              "1 january 2004",                     1
                              "31 december 2004")
    .num_items(4)
    .plot();

s.period("Spring", ("1 march 2004", "1 june 2004"), "green");       2
s.period("Summer", ("1 june 2004", "1 september 2004"), "yellow");
s.add_period(("21 summer 2004", "21 october 2004"), "blue");        3
s.draw_line("1 jan", "1 january 2004", "black");                    4
s.draw_line("1 mar", "1 april 2004", "black");
s.draw_line("1 jul", "1 july 2004", "black");
s.draw_line("1 oct", "1 october 2004", "black");
s.milestone("Longest day", "21 july 2004", "black");                5
s.add_milestone("21 december 2004", "black");                       6

s.title("Seasons (northern hemisphere)", rstk::Justify::Centre);
1 Start/end times must be in appropriate format.
2 A "period" is denoted as a filled rectangle, with given start/end times, and each period is on a new line with given label.
3 Periods are added on to the previous period.
4 Vertical lines are drawn using a time coordinate for the horizontal position.
5 Milestones, like periods, are each put on a new line with given label.
6 Adding a milestone adds it to the line of the previous milestone.
time chart example
Figure 17. Example of time chart

18. TX Plot

Documentation on:

After calling the above constructor, use a "builder" style to add options to the chart, before calling plot to finally construct it. The options are defined in:

Once constructed, Tk’s "dataconfig" functions are replaced by methods in TkChartSeries.

example Example ("tx_plot.rs"):

root.title("tx_plot example");

let canvas = rstk::make_canvas(&root);
canvas.width(500);
canvas.height(200);
canvas.background("white");
canvas.grid().layout();

let tx_plot = rstk::make_tx(&canvas,
                            ("2001-01-01", "2015-01-01", 1461),     1
                            (-10.0, 20.0, 10.0))
    .plot();

tx_plot.series_colour("min", "red");                                2
tx_plot.series_colour("max", "blue");

tx_plot.x_title("Time");
tx_plot.v_title("Temperature");

tx_plot.legend_position(rstk::Position::BottomRight);
tx_plot.legend("min", "Minimum Temperature");
tx_plot.legend("max", "Maximum Temperature");

tx_plot.plot("min", ("2001-01-01", -3.0));                          3
tx_plot.plot("min", ("2004-01-01", 4.0));
tx_plot.plot("min", ("2007-01-01", 2.0));
tx_plot.plot("min", ("2010-01-01", -1.0));
tx_plot.plot("min", ("2013-01-01", 2.0));
tx_plot.plot("min", ("2014-01-01", 5.0));

tx_plot.plot("max", ("2001-01-01", 10.0));
tx_plot.plot("max", ("2004-01-01", 12.0));
tx_plot.plot("max", ("2007-01-01", 8.0));
tx_plot.plot("max", ("2010-01-01", 6.0));
tx_plot.plot("max", ("2013-01-01", 15.0));
tx_plot.plot("max", ("2014-01-01", 18.0));
1 For tx-plot, the (min max step) are two dates and a time period
2 Uses the generic functions to set up properties of the chart
3 And the plot command takes a date as its first coordinate.
rstk tx plot example
Figure 18. Example of TX plot

19. XY Plot

There are five related charts:

  • xy-plot is the parent chart type, with four variants, depending on whether the axis value uses log

  • strip-chart is a variant where the x-axis will grow if new points exceed the current bounds.

Documentation on:

The five chart types differ in appearance, but are all constructed in a similar way and have the same methods.

After calling the above constructors, use a "builder" style to add options to the chart, before calling plot to finally construct it. The options are defined in:

Once constructed, Tk’s "dataconfig" functions are replaced by methods in TkChartSeries, and the "dotconfig" functions by TkChartDots

example Example ("xy_plot_1.rs"):

root.title("xy_plot example 1");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(400);
canvas.background("white");
canvas.grid().layout();

let xy = rstk::make_x_y(&canvas, (-10.0, 10.0, 2.0), (-100.0, 100.0, 20.0)) 1
    .plot();
xy.title("Two Functions", rstk::Justify::Centre);                           2
xy.x_title("Input x");
xy.v_title("Output y");
xy.series_colour("square", "blue");                                         3
xy.series_colour("cube", "green");

xy.legend_position(rstk::Position::BottomRight);                            4
xy.legend("square", "x*x");
xy.legend("cube", "x*x*x");

for n in 0..20 {
    let i = (n-10) as f64;
    xy.plot("square", (i, i*i));                                            5
    xy.plot("cube", (i, i*i*i));
}

xy.balloon((0.0, 0.0), "crossover point", rstk::Direction::NorthWest);      6
xy.plaintext_colour("red");                                                 7
xy.plaintext((6.0, 80.0), "diverging", rstk::Direction::NorthWest);         8
xy.save("xy-plot.ps");                                                      9
1 Create an xy-plot with x-axis [-10,10] and y-axis [-100,100]
2 Use general functions to label the title and axes
3 series_CONFIG methods are used to set properties for the two lines
4 Set up the legend, to appropriately label each line
5 Plot a given (x, y) item
6 Add some text, pointing to location of the chart
7 Change colour for plaintext
8 …​ and display some text at given location
9 Save the plot to a postscript file
rstk xy plot 1
Figure 19. Example xy plot

example Example of plotting using rchart - taken from "plotdemos6.rs":

// use rand::Rng; // uses the `rand` crate

root.title("xy_plot example 2 - plotdemos6: rchart");

let canvas = rstk::make_canvas(&root);
canvas.width(400);
canvas.height(200);
canvas.background("white");
canvas.grid().layout();

let chart = rstk::make_x_y(&canvas, (0.0, 100.0, 10.0), (0.0, 50.0, 10.0))
    .plot();
chart.title("R-chart (arbitrary data)", rstk::Justify::Centre);

chart.series_colour("series1", "green");
for x in (1..50).step_by(3) {
    let y = 20.0 + rand::thread_rng().gen_range(1.0..3.0);
    chart.rchart("series1", (x as f64, y));
}
// now some data outside the expected range
chart.rchart("series1", (50.0, 41.0));
chart.rchart("series1", (52.0, 42.0));
chart.rchart("series1", (54.0, 39.0));

// and continue with the well-behaved series
for x in (57..100).step_by(3) {
    let y = 20.0 + rand::thread_rng().gen_range(1.0..3.0);
    chart.rchart("series1", (x as f64, y));
}
rstk xy plot 2
Figure 20. Example xy plot using rchart

example Example of a contour plot - taken from "plotdemos5.rs":

root.title("xy_plot example 3 - from plotdemos5");

let canvas = rstk::make_canvas(&root);
canvas.width(500);
canvas.height(500);
canvas.background("white");
canvas.grid().layout();

let x = [
    [0.0, 100.0, 200.0],
    [0.0, 100.0, 200.0],
    [0.0, 100.0, 200.0],
    [0.0, 100.0, 200.0],
];
let y = [
    [0.0, 0.0, 0.0],
    [30.0, 30.0, 30.0],
    [60.0, 60.0, 60.0],
    [90.0, 90.0, 90.0],
];
let f = [
    [0.0, 1.0, 10.0],
    [0.0, 30.0, 30.0],
    [10.0, 60.0, 60.0],
    [30.0, 90.0, 90.0],
];
let contours = [
    0.0,
    5.2631578947,
    10.5263157895,
    15.7894736842,
    21.0526315789,
    26.3157894737,
    31.5789473684,
    36.8421052632,
    42.1052631579,
    47.3684210526,
    52.6315789474,
    57.8947368421,
    63.1578947368,
    68.4210526316,
    73.6842105263,
    78.9473684211,
    84.2105263158,
    89.4736842105,
    94.7368421053,
    100.0,
    105.263157895,
];
let x_limits = (0.0, 200.0, 50.0);
let y_limits = (0.0, 100.0, 20.0);

let chart = rstk::make_x_y(&canvas, x_limits, y_limits).plot();
chart.title(
    "Contour Demo: contourlines (default colourmap)",
    rstk::Justify::Centre,
);
chart.draw_contour_fill(&x, &y, &f, &contours);           1
chart.draw_grid(&x, &y);
1 x y f define the (x, y) point and its value. contours defines the boundaries between the colours.
rstk xy plot 3
Figure 21. Example contour plot