Skip to main content

Crate rustlavel_chart

Crate rustlavel_chart 

Source
Expand description

rustlavel-chart: charts, drawn in the browser, described in Rust.

This package vendors somebody else’s library, and that is unusual here. Chart.js is 69 KB gzipped and renders to a <canvas>; drawing the same thing from scratch would mean writing an axis scaler, a tick formatter, a legend layout engine and a hit-tester, and the result would still not have hover, tooltips or zoom. The copy lives in assets/, under its own MIT licence, and is served from this application’s own origin — see plugin.

The Rust half never draws anything. It builds the configuration Chart.js reads, which is a JSON object, and hands it to the page:

ⓘ
let chart = Chart::line("Revenue")
    .labels(["Jan", "Feb", "Mar"])
    .series(Series::new("2026", [1_200.0, 1_800.0, 1_500.0]))
    .series(Series::new("2025", [900.0, 1_100.0, 1_400.0]));

Ok(view("dashboard", context! { revenue: chart.to_json() })?)
<canvas data-chart="{{ revenue }}"></canvas>

§Why an attribute, and why {{ }}

The configuration travels in an ordinary escaped attribute. {{ }} turns every " into &quot; and every < into &lt;, the browser reverses that when it reads dataset.chart, and the round trip is exact — so a label reading </script> is a label, not an escape.

This is the whole reason there is no inline <script> anywhere in this package. A page that writes <script>new Chart(…)</script> needs script-src 'unsafe-inline', and a policy with unsafe-inline does not stop injected script — which is the one thing a Content-Security-Policy is for. The auth kit ships default-src 'self' with no unsafe-inline, and everything here is built to satisfy that policy rather than to ask for an exception to it.

Re-exports§

pub use chart::Axis;
pub use chart::Chart;
pub use chart::Kind;
pub use chart::Legend;
pub use palette::Palette;
pub use plugin::CHART_JS_PATH;
pub use plugin::Charts;
pub use plugin::INIT_JS_PATH;
pub use series::Series;

Modules§

chart
The configuration Chart.js reads.
palette
Colours that survive both themes.
plugin
Serving the library, from this application’s own origin.
series
One line, or one set of bars.

Enums§

Error
Every failure the framework itself can produce.
Json
A parsed JSON value.

Type Aliases§

Result
The framework-wide result type.