Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Billy-Sheppard/chart-js-rs

Rust Chart.js connector crate
https://github.com/Billy-Sheppard/chart-js-rs

chart rust wasm webassembly

Last synced: about 2 months ago
JSON representation

Rust Chart.js connector crate

Awesome Lists containing this project

README

        


Material Bread logo

# Chart.js types API in Rust
[![crates.io](https://img.shields.io/crates/v/chart-js-rs.svg)](https://crates.io/crates/chart-js-rs)
[![docs.rs](https://docs.rs/chart-js-rs/badge.svg)](https://docs.rs/chart-js-rs)

***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](https://github.com/Pauan/rust-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:
```toml
[dependencies.chart-js-rs]
git = "https://github.com/Billy-Sheppard/chart-js-rs"
```

### Rust:
```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:
```html

...

import init from 'wasm.js';

async function run() {
await init();
}

run();

...

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
};

```


# 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](https://www.chartjs.org/docs/latest/developers/plugins.html)

`.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](https://www.chartjs.org/docs/latest/).

`FnWithArgs` is used, for example, in implementing conditional line segment colouring, according to the [docs](https://www.chartjs.org/docs/latest/samples/line/segments.html).
It can also be used to leaverage logic written in Rust, to calculate outputs for ChartJS.
```rust
#[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!
}
}
]
}
}
```