42 releases
0.0.45 | Oct 14, 2024 |
---|---|
0.0.38 | Sep 23, 2024 |
0.0.31 | Jun 25, 2024 |
0.0.19 | Mar 12, 2024 |
0.0.6 | Nov 15, 2022 |
#76 in WebAssembly
75 downloads per month
94KB
2K
SLoC
Chart.js types API in Rust
In Alpha, types added as needed, feel free to PR.
How to use
Check out the example folder for some code examples. The example uses WebAssembly and the dominator crate to produce charts. This library should be compatible with any WASM/HTML library.
The compiled webpage can be found here: https://billy-sheppard.github.io/chart-js-rs/examples/index.html
Cargo.toml:
[dependencies.chart-js-rs]
git = "https://github.com/Billy-Sheppard/chart-js-rs"
Rust:
use chart_js_rs::ChartExt;
let id = "[YOUR CHART ID HERE]";
let chart = chart_js_rs::scatter::Scatter {
id: id.to_string(),
options: ChartOptions { .. },
data: Dataset { .. },
..Default::default()
};
// to use any JS callbacks or functions you use render_mutate and refer to the JS below
chart.into_chart().mutate().render();
// to use any chart-js plugins, a few examples
chart.into_chart().plugins("[window['chartjs-plugin-autocolors']]").render(); // for autocolors and no mutating
chart.into_chart().mutate().plugins("[window['chartjs-plugin-autocolors']]").render(); // for autocolors and mutating
chart.into_chart().mutate().plugins("[ChartDataLabels, window['chartjs-plugin-autocolors']]").render(); // for datalabels, autocolors, and mutating
// else use render
chart.into_chart().render();
Your html file:
<script src="https://cdn.jsdelivr.net/npm/chart.js@^4"></script>
...
<script type="module">
import init from 'wasm.js';
async function run() {
await init();
}
run();
</script>
...
<script type="module">
import * as root from './chart_js_rs_example.js'
window.callbacks = root;
window.mutate_chart_object = function (v) {
if (v.id === ("[YOUR CHART ID HERE]")) {
v.options.scales.y1.ticks = {
callback:
function (value, _index, _values) {
return '$' + value.toFixed(2);
}
};
}
return v
};
</script>
Explainers
Whats the difference between my_chart.render()
and mychart.mutate().render()
?
.render()
is for simple charts, that don't require any further changes done using javascript code.
.mutate().render()
allows for chart objects to be accessible in your javascript file, so you can mutate the object however required, especially useful for ChartJS functions not yet available in this library.
.plugins("[MyPlugin]").mutate().render()
allows for ChartJS plugins to be used with your charts, the parameter is the direct string representation of the Javascript array containing your plugins. Docs
.plugins("[MyPlugin]").defaults("Chart.defaults.font.size = 20;").mutate().render()
allows for ChartJS defaults to be set.
How to use struct FnWithArgs
?
FnWithArgs
is a helper struct to allow serialization of javascript functions by encoding their body and arguments as a string. Then, as needed, the function can be rebuilt in JavaScipt, and called.
It is important then, that you know which variables are being parsed to the function. For this information, you can refer to the Chart.js documentation.
FnWithArgs
is used, for example, in implementing conditional line segment colouring, according to the docs.
It can also be used to leaverage logic written in Rust, to calculate outputs for ChartJS.
#[wasm_bindgen] // your function must be a wasm_bindgen export
pub fn add(a: u32, b: u32) -> u32 {
a + b
}
// ...
Scatter::</*...*/> {
data: {
datasets: vec![
Dataset {
// ...
segment: Segment {
borderColor:
// todo!()
// write some examples here!
}
}
]
}
}
Dependencies
~9–12MB
~218K SLoC