aprender-viz 0.64.0

SIMD/GPU/WASM-accelerated visualization library for data science and ML
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! WebAssembly bindings for trueno-viz.
//!
//! Provides JavaScript-accessible functions for creating visualizations
//! in the browser without any JavaScript charting libraries.
//!
//! # Usage (JavaScript)
//!
//! ```javascript
//! import init, { scatter_plot, line_chart, histogram } from 'trueno-viz';
//!
//! await init();
//!
//! // Create a scatter plot and get PNG data
//! const pngData = scatter_plot(
//!     new Float32Array([1, 2, 3, 4, 5]),
//!     new Float32Array([2, 4, 3, 5, 4]),
//!     { width: 800, height: 600, color: '#4285F4' }
//! );
//!
//! // Display in an image element
//! const blob = new Blob([pngData], { type: 'image/png' });
//! document.getElementById('chart').src = URL.createObjectURL(blob);
//! ```

use batuta_common::display::WithDimensions;
use wasm_bindgen::prelude::*;

use crate::color::Rgba;
use crate::grammar::{Aes, Coord, GGPlot, Geom, Theme};
use crate::output::PngEncoder;
use crate::plots::{BinStrategy, Heatmap, Histogram, LineChart, LineSeries, ScatterPlot};
use crate::prompt;

// ============================================================================
// Initialization
// ============================================================================

/// Initialize the WASM module.
///
/// Call this before using any other functions.
#[wasm_bindgen(start)]
pub fn init() {
    // WASM module initialized
}

// ============================================================================
// Plot Options
// ============================================================================

/// Options for plot rendering.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct PlotOptions {
    width: u32,
    height: u32,
    color: String,
    background: String,
    title: Option<String>,
    point_size: f32,
    line_width: f32,
}

#[wasm_bindgen]
impl PlotOptions {
    /// Create default plot options.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            width: 800,
            height: 600,
            color: "#4285F4".to_string(),
            background: "#FFFFFF".to_string(),
            title: None,
            point_size: 5.0,
            line_width: 2.0,
        }
    }

    /// Set width in pixels.
    #[wasm_bindgen]
    pub fn width(mut self, width: u32) -> Self {
        self.width = width;
        self
    }

    /// Set height in pixels.
    #[wasm_bindgen]
    pub fn height(mut self, height: u32) -> Self {
        self.height = height;
        self
    }

    /// Set the primary color (hex format: #RRGGBB).
    #[wasm_bindgen]
    pub fn color(mut self, color: &str) -> Self {
        self.color = color.to_string();
        self
    }

    /// Set the background color (hex format: #RRGGBB).
    #[wasm_bindgen]
    pub fn background(mut self, bg: &str) -> Self {
        self.background = bg.to_string();
        self
    }

    /// Set the title.
    #[wasm_bindgen]
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }

    /// Set point size for scatter plots.
    #[wasm_bindgen]
    pub fn point_size(mut self, size: f32) -> Self {
        self.point_size = size;
        self
    }

    /// Set line width for line charts.
    #[wasm_bindgen]
    pub fn line_width(mut self, width: f32) -> Self {
        self.line_width = width;
        self
    }
}

impl Default for PlotOptions {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

fn parse_hex_color(hex: &str) -> Rgba {
    let hex = hex.trim_start_matches('#');
    if hex.len() == 6 {
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0);
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0);
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0);
        Rgba::new(r, g, b, 255)
    } else {
        Rgba::new(66, 133, 244, 255) // Default blue
    }
}

// ============================================================================
// Plot Functions
// ============================================================================

/// Create a scatter plot and return PNG data.
///
/// # Arguments
///
/// * `x` - X coordinates as Float32Array
/// * `y` - Y coordinates as Float32Array
/// * `options` - Plot options (optional)
///
/// # Returns
///
/// PNG image data as Uint8Array
#[wasm_bindgen]
pub fn scatter_plot(
    x: &[f32],
    y: &[f32],
    options: Option<PlotOptions>,
) -> Result<Vec<u8>, JsValue> {
    let opts = options.unwrap_or_default();
    let color = parse_hex_color(&opts.color);

    let plot = ScatterPlot::new()
        .x(x)
        .y(y)
        .color(color)
        .size(opts.point_size)
        .dimensions(opts.width, opts.height)
        .build()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let fb = plot.to_framebuffer().map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Create a line chart and return PNG data.
///
/// # Arguments
///
/// * `x` - X coordinates as Float32Array
/// * `y` - Y coordinates as Float32Array
/// * `options` - Plot options (optional)
///
/// # Returns
///
/// PNG image data as Uint8Array
#[wasm_bindgen]
pub fn line_chart(x: &[f32], y: &[f32], options: Option<PlotOptions>) -> Result<Vec<u8>, JsValue> {
    let opts = options.unwrap_or_default();
    let color = parse_hex_color(&opts.color);

    let plot = LineChart::new()
        .add_series(LineSeries::new("data").data(x, y).color(color).thickness(opts.line_width))
        .dimensions(opts.width, opts.height)
        .build()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let fb = plot.to_framebuffer().map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Create a histogram and return PNG data.
///
/// # Arguments
///
/// * `data` - Data values as Float32Array
/// * `bins` - Number of bins (optional, defaults to 10)
/// * `options` - Plot options (optional)
///
/// # Returns
///
/// PNG image data as Uint8Array
#[wasm_bindgen]
pub fn histogram(
    data: &[f32],
    bins: Option<usize>,
    options: Option<PlotOptions>,
) -> Result<Vec<u8>, JsValue> {
    let opts = options.unwrap_or_default();
    let color = parse_hex_color(&opts.color);
    let num_bins = bins.unwrap_or(10);

    let plot = Histogram::new()
        .data(data)
        .bins(BinStrategy::Fixed(num_bins))
        .color(color)
        .dimensions(opts.width, opts.height)
        .build()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let fb = plot.to_framebuffer().map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Create a heatmap and return PNG data.
///
/// # Arguments
///
/// * `data` - Flattened 2D data as Float32Array (row-major)
/// * `rows` - Number of rows
/// * `cols` - Number of columns
/// * `options` - Plot options (optional)
///
/// # Returns
///
/// PNG image data as Uint8Array
#[wasm_bindgen]
pub fn heatmap(
    data: &[f32],
    rows: usize,
    cols: usize,
    options: Option<PlotOptions>,
) -> Result<Vec<u8>, JsValue> {
    let opts = options.unwrap_or_default();

    let plot = Heatmap::new()
        .data(data, rows, cols)
        .dimensions(opts.width, opts.height)
        .build()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let fb = plot.to_framebuffer().map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Create a plot from a text prompt and return PNG data.
///
/// # Arguments
///
/// * `prompt` - Text prompt (e.g., "scatter x=[1,2,3] y=[4,5,6] color=blue")
///
/// # Returns
///
/// PNG image data as Uint8Array
///
/// # Example
///
/// ```javascript
/// const png = from_prompt("scatter x=[1,2,3,4,5] y=[2,4,3,5,4] color=red size=8");
/// ```
#[wasm_bindgen]
pub fn from_prompt(prompt_str: &str) -> Result<Vec<u8>, JsValue> {
    let fb = prompt::from_prompt(prompt_str).map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Create a Grammar of Graphics plot and return PNG data.
///
/// # Arguments
///
/// * `x` - X coordinates
/// * `y` - Y coordinates
/// * `geom` - Geometry type: "point", "line", "bar", "area"
/// * `theme` - Theme: "grey", "minimal", "dark", "classic"
/// * `options` - Plot options
///
/// # Returns
///
/// PNG image data as Uint8Array
#[wasm_bindgen]
pub fn ggplot(
    x: &[f32],
    y: &[f32],
    geom: &str,
    theme: &str,
    options: Option<PlotOptions>,
) -> Result<Vec<u8>, JsValue> {
    let opts = options.unwrap_or_default();
    let color = parse_hex_color(&opts.color);

    let geom_layer = match geom {
        "point" => Geom::point().aes(Aes::new().color_value(color).size_value(opts.point_size)),
        "line" => Geom::line(),
        "bar" => Geom::bar().aes(Aes::new().color_value(color)),
        "area" => Geom::area().aes(Aes::new().color_value(color)),
        _ => Geom::point().aes(Aes::new().color_value(color)),
    };

    let theme_obj = match theme {
        "minimal" => Theme::minimal(),
        "dark" => Theme::dark(),
        "classic" => Theme::classic(),
        "bw" => Theme::bw(),
        "void" => Theme::void(),
        _ => Theme::grey(),
    };

    let plot = GGPlot::new()
        .data_xy(x, y)
        .geom(geom_layer)
        .aes(Aes::new().color_value(color))
        .theme(theme_obj)
        .coord(Coord::cartesian())
        .dimensions(opts.width, opts.height)
        .build()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let fb = plot.to_framebuffer().map_err(|e| JsValue::from_str(&e.to_string()))?;

    PngEncoder::to_bytes(&fb).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Get the library version.
#[wasm_bindgen]
pub fn version() -> String {
    env!("CARGO_PKG_VERSION").to_string()
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_hex_color() {
        let color = parse_hex_color("#FF0000");
        assert_eq!(color.r, 255);
        assert_eq!(color.g, 0);
        assert_eq!(color.b, 0);
    }

    #[test]
    fn test_parse_hex_color_no_hash() {
        let color = parse_hex_color("00FF00");
        assert_eq!(color.r, 0);
        assert_eq!(color.g, 255);
        assert_eq!(color.b, 0);
    }

    #[test]
    fn test_plot_options_default() {
        let opts = PlotOptions::new();
        assert_eq!(opts.width, 800);
        assert_eq!(opts.height, 600);
    }

    #[test]
    fn test_plot_options_builder() {
        let opts = PlotOptions::new().width(1024).height(768).color("#FF5500").point_size(10.0);

        assert_eq!(opts.width, 1024);
        assert_eq!(opts.height, 768);
        assert_eq!(opts.color, "#FF5500");
        assert!((opts.point_size - 10.0).abs() < 0.01);
    }
}