main.rs 1.8 KB
Newer Older
1 2
use wasm_bindgen::prelude::*;

P
Peter Pan 已提交
3 4 5 6 7
mod high_dimensional;
mod histogram;
mod scalar;
mod utils;

8 9 10
#[macro_use]
extern crate serde_derive;

11 12
pub fn main() {}

13
#[wasm_bindgen]
P
Peter Pan 已提交
14
pub fn scalar_transform(js_datasets: &JsValue, smoothing: f64) -> JsValue {
15
    utils::set_panic_hook();
P
Peter Pan 已提交
16 17
    let datasets: Vec<Vec<scalar::Dataset>> = js_datasets.into_serde().unwrap();
    let result: Vec<Vec<scalar::Smoothed>> = scalar::transform(datasets, smoothing);
18 19 20 21
    return JsValue::from_serde(&result).unwrap();
}

#[wasm_bindgen]
P
Peter Pan 已提交
22
pub fn scalar_range(js_datasets: &JsValue, outlier: bool) -> JsValue {
23
    utils::set_panic_hook();
P
Peter Pan 已提交
24 25
    let datasets: Vec<Vec<scalar::Smoothed>> = js_datasets.into_serde().unwrap();
    let result = scalar::range(datasets, outlier);
26 27 28
    return JsValue::from_serde(&result).unwrap();
}

P
Peter Pan 已提交
29 30 31 32 33 34 35
#[wasm_bindgen]
pub fn histogram_transform(js_data: &JsValue, mode: String) -> JsValue {
    utils::set_panic_hook();
    let data: Vec<histogram::Data> = js_data.into_serde().unwrap();
    if mode == String::from("overlay") {
        let result = histogram::transform_overlay(data);
        return JsValue::from_serde(&result).unwrap();
36
    }
P
Peter Pan 已提交
37 38 39
    if mode == String::from("offset") {
        let result = histogram::transform_offset(data);
        return JsValue::from_serde(&result).unwrap();
40
    }
P
Peter Pan 已提交
41
    return JsValue::null();
42 43 44
}

#[wasm_bindgen]
P
Peter Pan 已提交
45
pub fn high_dimensional_divide(
46 47 48 49 50 51 52 53
    js_points: &JsValue,
    js_labels: &JsValue,
    visibility: bool,
    keyword: String,
) -> JsValue {
    utils::set_panic_hook();
    let points: Vec<Vec<f64>> = js_points.into_serde().unwrap();
    let labels: Vec<String> = js_labels.into_serde().unwrap();
P
Peter Pan 已提交
54 55
    let result: high_dimensional::DividedPoints =
        high_dimensional::divide(points, labels, visibility, keyword);
56 57
    return JsValue::from_serde(&result).unwrap();
}