16 releases

0.0.17 Feb 12, 2024
0.0.15 Jan 30, 2024
0.0.13 Nov 17, 2023
0.0.10 Jul 10, 2023
0.0.6 Nov 15, 2022

#98 in WebAssembly

Download history 16/week @ 2023-10-30 3/week @ 2023-11-06 27/week @ 2023-11-13 31/week @ 2023-11-20 53/week @ 2023-11-27 4/week @ 2023-12-04 14/week @ 2023-12-18 25/week @ 2023-12-25 12/week @ 2024-01-01 1/week @ 2024-01-08 7/week @ 2024-01-15 10/week @ 2024-01-22 111/week @ 2024-01-29 28/week @ 2024-02-05 142/week @ 2024-02-12

292 downloads per month

Apache-2.0

49KB
1K SLoC

Material Bread logo

Chart.js types API in Rust

crates.io docs.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 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:

    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 callbacks or functions you use render_mutate and refer to the JS below
    chart.to_chart().render_mutate();

    // else use render
    chart.to_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>
  // mutating charts
  function mutate_chart_object(v) { // must have this function name
    if (v.id === ("[YOUR CHART ID HERE]")) {
    // do any work here, this would prepend `$` to y1 axis tick labels
      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.render_mutate()?

.render() is for simple charts, that don't require any further changes done using javascript code.

.render_mutate() 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.

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 implimenting conditional line segment colouring, according to the docs.

  Scatter::</*...*/> {
    data: {
      datasets: vec![
        Dataset {
          // ...
          segment: Segment {
            borderColor: 
              FnWithArgs::new() // Create the Function
                .arg("ctx") // Add a named argument using a builder pattern
                .body("ctx.p0.parsed.y > ctx.p1.parsed.y ? 'red' : 'green'") // Add the function body, in this case make the line red if the slope is negative
          }
        }
      ]
    }
  }

Dependencies

~7–9.5MB
~189K SLoC