Skip to content

Commit c1ce0fe

Browse files
0.0.43
1 parent ecd3322 commit c1ce0fe

File tree

5 files changed

+182
-71
lines changed

5 files changed

+182
-71
lines changed

Cargo.lock

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chart-js-rs"
3-
version = "0.0.42"
3+
version = "0.0.43"
44
edition = "2021"
55
authors = ["Billy Sheppard", "Luis Moreno"]
66
license = "Apache-2.0"

README.md

+2-20
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,8 @@ It can also be used to leaverage logic written in Rust, to calculate outputs for
115115
// ...
116116
segment: Segment {
117117
borderColor:
118-
FnWithArgs::new() // Create the Function
119-
.arg("ctx") // Add a named argument using a builder pattern, you can have as many arugments as required
120-
121-
// run rust fn takes your input params, output variable name, and function pointer
122-
// this will produce
123-
// const output = window.callbacks.add(1, 1);
124-
// return ctx.p0.parsed.y > ctx.p1.parsed.y ? 'red' : 'green'
125-
.run_rust_fn(&["1".into(), "1".into()], "output".into(), add)
126-
127-
// .body() can be used to add any other javascript
128-
.body("console.log(output)")
129-
130-
.return_value("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
131-
132-
// this will produce
133-
// function(ctx) {
134-
// const output = windows.callbacks.add(1, 1);
135-
// console.log(output);
136-
// return ctx.p0.parsed.y > ctx.p1.parsed.y ? 'red' : 'green'
137-
// }
118+
// todo!()
119+
// write some examples here!
138120
}
139121
}
140122
]

examples/src/lib.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@ impl Model {
197197
"x",
198198
ChartScale::new().scale_type("linear").ticks(
199199
ScaleTicks::new().callback(
200-
FnWithArgs::new()
201-
.args(["value", "index", "ticks"])
202-
.js_body(
203-
"if (index % 2 === 0) {
204-
var out = this.getLabelForValue(value)
205-
} else {
206-
var out = ''
207-
};",
208-
)
209-
.js_return_value("out"),
200+
// we can call rust functions in callbacks
201+
FnWithArgs::<3>::new()
202+
// we can override any arguments going in, in this case we must as rust cannot handle `this`.
203+
// Note: if you don't define your variables with ``.args([..])`, they get the default label of the letter of the alphabet they're the index of
204+
// 1st arg: `a`
205+
// 2nd arg: `b`
206+
// ...
207+
.js_body("var a = this.getLabelForValue(value);")
208+
// function pointer goes here - note that the count of arguments must equal the const param (3 in this case)
209+
.run_rust_fn(show_line_ticks),
210210
),
211211
),
212212
)])
@@ -491,6 +491,15 @@ impl Model {
491491
}
492492
}
493493

494+
#[wasm_bindgen]
495+
pub fn show_line_ticks(this: String, index: u32, _ticks: JsValue) -> String {
496+
if index % 2 == 0 {
497+
this
498+
} else {
499+
String::new()
500+
}
501+
}
502+
494503
#[wasm_bindgen(start)]
495504
pub async fn main_js() -> Result<(), JsValue> {
496505
std::panic::set_hook(Box::new(console_error_panic_hook::hook));

0 commit comments

Comments
 (0)