The graph module provides plotting and data visualization. It generates SVG vector graphics files — no external dependencies required. SVG files can be opened in any web browser or vector graphics editor.
Overview
All functions are accessed via the graph. prefix. Plot configuration functions (graph.title, graph.xlabel, graph.ylabel) set labels for the next plot you create. Use graph.save(path) to write the SVG to disk, or graph.show() to open it in the default viewer.
Plot Functions
| Function | Arguments | Description |
|---|
graph.scatter(xs, ys) | array, array | Scatter plot from two numeric arrays (must be same length) |
graph.line(xs, ys) | array, array | Line chart connecting (x, y) points with a polyline |
graph.bar(labels, values) | array, array | Vertical bar chart with category labels |
graph.hist(data, [bins]) | array, [int] | Histogram with configurable bin count (default: 10) |
graph.area(xs, ys) | array, array | Area chart — line with filled region below (v0.1.4) |
Configuration Functions
| Function | Arguments | Description |
|---|
graph.title(text) | string | Set the title for the next plot |
graph.xlabel(text) | string | Set the x-axis label for the next plot |
graph.ylabel(text) | string | Set the y-axis label for the next plot |
graph.grid(bool) | bool | Toggle dashed grid lines on/off (v0.1.4) |
graph.color(name) | string | Set plot color — named ("red", "blue") or hex ("#ff0000") (v0.1.4) |
graph.legend(labels) | array of strings | Add a legend box with color swatches (v0.1.4) |
Output Functions
| Function | Arguments | Description |
|---|
graph.save(path) | string | Save the current plot as an SVG file |
graph.show() | none | Open the current plot in the default SVG viewer |
Examples
Scatter plot
let xs = [1, 2, 3, 4, 5, 6, 7, 8];
let ys = [2, 4, 5, 4, 5, 7, 8, 9];
graph.title("My Scatter Plot");
graph.xlabel("X");
graph.ylabel("Y");
graph.scatter(xs, ys);
graph.save("plot.svg");
Line chart
let months = [1, 2, 3, 4, 5, 6];
let temps = [5, 8, 12, 18, 22, 25];
graph.title("Temperature Over Time");
graph.xlabel("Month");
graph.ylabel("Temperature (C)");
graph.line(months, temps);
graph.save("temp.svg");
Bar chart
let labels = ["Mon", "Tue", "Wed", "Thu", "Fri"];
let values = [23, 45, 12, 67, 34];
graph.title("Daily Sales");
graph.xlabel("Day");
graph.ylabel("Units Sold");
graph.bar(labels, values);
graph.save("sales.svg");
Histogram
let data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5];
graph.title("Data Distribution");
graph.xlabel("Value");
graph.ylabel("Frequency");
graph.hist(data, 5);
graph.save("hist.svg");
Area chart with color, grid, and legend (v0.1.4)
let months = [1, 2, 3, 4, 5, 6];
let revenue = [10, 15, 13, 20, 25, 30];
graph.title("Monthly Revenue");
graph.xlabel("Month");
graph.ylabel("Revenue ($K)");
graph.color("green");
graph.grid(true);
graph.legend(["Revenue"]);
graph.area(months, revenue);
graph.save("revenue.svg");
Complete example
// Generate and save multiple plot types
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let ys = [2, 4, 5, 4, 5, 7, 8, 9, 10, 12];
graph.title("Scatter Plot Demo");
graph.xlabel("X Axis");
graph.ylabel("Y Axis");
graph.scatter(xs, ys);
graph.save("scatter.svg");
print("Scatter plot saved");
let temps = [10, 12, 15, 18, 22, 25, 28, 27, 24, 20, 16, 12];
let months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
graph.title("Monthly Temperature");
graph.xlabel("Month");
graph.ylabel("Temperature (C)");
graph.line(months, temps);
graph.save("temperature.svg");
print("Line chart saved");
Error handling
- All plot functions require numeric arrays — non-numeric values throw an error
graph.scatter(), graph.line(), and graph.area() require xs and ys to be the same lengthgraph.hist() requires bins to be a positive integergraph.grid() requires a boolean argumentgraph.color() requires a string (color name or hex)graph.legend() requires an array of stringsgraph.save() and graph.show() throw an error if no plot has been createdgraph.save() throws an error if the file cannot be written