Graph Module

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

FunctionArgumentsDescription
graph.scatter(xs, ys)array, arrayScatter plot from two numeric arrays (must be same length)
graph.line(xs, ys)array, arrayLine chart connecting (x, y) points with a polyline
graph.bar(labels, values)array, arrayVertical bar chart with category labels
graph.hist(data, [bins])array, [int]Histogram with configurable bin count (default: 10)
graph.area(xs, ys)array, arrayArea chart — line with filled region below (v0.1.4)

Configuration Functions

FunctionArgumentsDescription
graph.title(text)stringSet the title for the next plot
graph.xlabel(text)stringSet the x-axis label for the next plot
graph.ylabel(text)stringSet the y-axis label for the next plot
graph.grid(bool)boolToggle dashed grid lines on/off (v0.1.4)
graph.color(name)stringSet plot color — named ("red", "blue") or hex ("#ff0000") (v0.1.4)
graph.legend(labels)array of stringsAdd a legend box with color swatches (v0.1.4)

Output Functions

FunctionArgumentsDescription
graph.save(path)stringSave the current plot as an SVG file
graph.show()noneOpen 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 length
  • graph.hist() requires bins to be a positive integer
  • graph.grid() requires a boolean argument
  • graph.color() requires a string (color name or hex)
  • graph.legend() requires an array of strings
  • graph.save() and graph.show() throw an error if no plot has been created
  • graph.save() throws an error if the file cannot be written