main.rs 2.0 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
    let datasets: Vec<Vec<scalar::Dataset>> = js_datasets.into_serde().unwrap();
P
Peter Pan 已提交
17 18
    let result: Vec<Vec<scalar::Smoothed>> = scalar::transform(&datasets, smoothing);
    JsValue::from_serde(&result).unwrap()
19 20 21
}

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

#[wasm_bindgen]
pub fn scalar_axis_range(js_datasets: &JsValue, outlier: bool) -> JsValue {
    utils::set_panic_hook();
    let datasets: Vec<Vec<scalar::Smoothed>> = js_datasets.into_serde().unwrap();
    let result = scalar::axis_range(&datasets, outlier);
P
Peter Pan 已提交
34
    JsValue::from_serde(&result).unwrap()
35 36
}

P
Peter Pan 已提交
37
#[wasm_bindgen]
P
Peter Pan 已提交
38
pub fn histogram_transform(js_data: &JsValue, mode: &str) -> JsValue {
P
Peter Pan 已提交
39 40
    utils::set_panic_hook();
    let data: Vec<histogram::Data> = js_data.into_serde().unwrap();
P
Peter Pan 已提交
41 42 43 44 45 46 47 48 49 50
    match mode {
        "overlay" => {
            let result = histogram::transform_overlay(&data);
            JsValue::from_serde(&result).unwrap()
        }
        "offset" => {
            let result = histogram::transform_offset(&data);
            JsValue::from_serde(&result).unwrap()
        }
        _ => JsValue::null(),
51 52 53 54
    }
}

#[wasm_bindgen]
P
Peter Pan 已提交
55
pub fn high_dimensional_divide(
56 57 58
    js_points: &JsValue,
    js_labels: &JsValue,
    visibility: bool,
P
Peter Pan 已提交
59
    keyword: &str,
60 61 62 63
) -> 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 已提交
64
    let result: high_dimensional::DividedPoints =
P
Peter Pan 已提交
65 66
        high_dimensional::divide(&points, &labels, visibility, keyword);
    JsValue::from_serde(&result).unwrap()
67
}