leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Advanced Chart Types
//!
//! Implementation of advanced chart types including:
//! - Radar Charts (multi-dimensional polar coordinates)
//! - Sankey Diagrams (flow visualization)
//! - Treemaps (hierarchical rectangles)
//! - Violin Plots (distribution visualization)

use crate::chart_config::*;
use crate::webgpu_renderer::{WebGpuError, WebGpuRenderer};
use std::collections::HashMap;

/// Radar Chart Data Point
#[derive(Debug, Clone)]
pub struct RadarDataPoint {
    pub category: String,
    pub value: f64,
    pub max_value: f64,
}

/// Sankey Diagram Node
#[derive(Debug, Clone)]
pub struct SankeyNode {
    pub id: String,
    pub name: String,
    pub value: f64,
}

/// Sankey Diagram Link
#[derive(Debug, Clone)]
pub struct SankeyLink {
    pub source: String,
    pub target: String,
    pub value: f64,
}

/// Treemap Node
#[derive(Debug, Clone)]
pub struct TreemapNode {
    pub id: String,
    pub name: String,
    pub value: f64,
    pub children: Vec<TreemapNode>,
}

/// Violin Plot Data Point
#[derive(Debug, Clone)]
pub struct ViolinDataPoint {
    pub category: String,
    pub values: Vec<f64>,
}

/// Radar Chart Configuration
#[derive(Debug, Clone)]
pub struct RadarChartConfig {
    pub base_config: BaseChartConfig,
    pub categories: Vec<String>,
    pub max_value: f64,
    pub show_grid: bool,
    pub show_labels: bool,
    pub fill_area: bool,
    pub stroke_width: f32,
    pub color: String,
}

impl Default for RadarChartConfig {
    fn default() -> Self {
        Self {
            base_config: BaseChartConfig {
                width: 800,
                height: 600,
                title: "Radar Chart".to_string(),
                x_label: "".to_string(),
                y_label: "".to_string(),
                show_grid: true,
                background_color: "#ffffff".to_string(),
                text_color: "#000000".to_string(),
            },
            categories: vec![
                "A".to_string(),
                "B".to_string(),
                "C".to_string(),
                "D".to_string(),
                "E".to_string(),
            ],
            max_value: 100.0,
            show_grid: true,
            show_labels: true,
            fill_area: true,
            stroke_width: 2.0,
            color: "#3b82f6".to_string(),
        }
    }
}

/// Sankey Diagram Configuration
#[derive(Debug, Clone)]
pub struct SankeyConfig {
    pub base_config: BaseChartConfig,
    pub nodes: Vec<SankeyNode>,
    pub links: Vec<SankeyLink>,
    pub node_width: f32,
    pub node_padding: f32,
    pub link_opacity: f32,
    pub color_scheme: ColorScheme,
}

impl Default for SankeyConfig {
    fn default() -> Self {
        Self {
            base_config: BaseChartConfig {
                width: 800,
                height: 600,
                title: "Sankey Diagram".to_string(),
                x_label: "".to_string(),
                y_label: "".to_string(),
                show_grid: false,
                background_color: "#ffffff".to_string(),
                text_color: "#000000".to_string(),
            },
            nodes: vec![],
            links: vec![],
            node_width: 20.0,
            node_padding: 10.0,
            link_opacity: 0.6,
            color_scheme: ColorScheme::Viridis,
        }
    }
}

/// Treemap Configuration
#[derive(Debug, Clone)]
pub struct TreemapConfig {
    pub base_config: BaseChartConfig,
    pub root_node: TreemapNode,
    pub padding: f32,
    pub border_width: f32,
    pub border_color: String,
    pub color_scheme: ColorScheme,
    pub show_labels: bool,
    pub label_threshold: f64,
}

impl Default for TreemapConfig {
    fn default() -> Self {
        Self {
            base_config: BaseChartConfig {
                width: 800,
                height: 600,
                title: "Treemap".to_string(),
                x_label: "".to_string(),
                y_label: "".to_string(),
                show_grid: false,
                background_color: "#ffffff".to_string(),
                text_color: "#000000".to_string(),
            },
            root_node: TreemapNode {
                id: "root".to_string(),
                name: "Root".to_string(),
                value: 100.0,
                children: vec![],
            },
            padding: 2.0,
            border_width: 1.0,
            border_color: "#ffffff".to_string(),
            color_scheme: ColorScheme::Viridis,
            show_labels: true,
            label_threshold: 0.05,
        }
    }
}

/// Violin Plot Configuration
#[derive(Debug, Clone)]
pub struct ViolinConfig {
    pub base_config: BaseChartConfig,
    pub data: Vec<ViolinDataPoint>,
    pub bandwidth: f64,
    pub show_box_plot: bool,
    pub show_points: bool,
    pub point_size: f32,
    pub fill_opacity: f32,
    pub stroke_width: f32,
    pub color_scheme: ColorScheme,
}

impl Default for ViolinConfig {
    fn default() -> Self {
        Self {
            base_config: BaseChartConfig {
                width: 800,
                height: 600,
                title: "Violin Plot".to_string(),
                x_label: "Category".to_string(),
                y_label: "Value".to_string(),
                show_grid: true,
                background_color: "#ffffff".to_string(),
                text_color: "#000000".to_string(),
            },
            data: vec![],
            bandwidth: 0.5,
            show_box_plot: true,
            show_points: false,
            point_size: 3.0,
            fill_opacity: 0.7,
            stroke_width: 1.0,
            color_scheme: ColorScheme::Viridis,
        }
    }
}

/// Advanced Chart Renderer Trait
pub trait AdvancedChartRenderer {
    fn render_radar_chart(
        &mut self,
        config: &RadarChartConfig,
        data: &[RadarDataPoint],
    ) -> Result<WebGpuRenderResult, WebGpuError>;
    fn render_sankey_diagram(
        &mut self,
        config: &SankeyConfig,
    ) -> Result<WebGpuRenderResult, WebGpuError>;
    fn render_treemap(&mut self, config: &TreemapConfig)
        -> Result<WebGpuRenderResult, WebGpuError>;
    fn render_violin_plot(
        &mut self,
        config: &ViolinConfig,
    ) -> Result<WebGpuRenderResult, WebGpuError>;
}

impl AdvancedChartRenderer for WebGpuRenderer {
    fn render_radar_chart(
        &mut self,
        config: &RadarChartConfig,
        data: &[RadarDataPoint],
    ) -> Result<WebGpuRenderResult, WebGpuError> {
        // Convert polar coordinates to cartesian
        let center_x = config.base_config.width as f32 / 2.0;
        let center_y = config.base_config.height as f32 / 2.0;
        let radius = (config.base_config.width.min(config.base_config.height) as f32 / 2.0) * 0.8;

        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        // Generate vertices for radar chart
        for (i, point) in data.iter().enumerate() {
            let angle = 2.0 * std::f32::consts::PI * i as f32 / data.len() as f32;
            let normalized_value = (point.value / point.max_value) as f32;
            let x = center_x + radius * normalized_value * angle.cos();
            let y = center_y + radius * normalized_value * angle.sin();

            vertices.push([x, y, 0.0, 1.0]); // x, y, z, w
        }

        // Generate indices for line segments
        for i in 0..data.len() {
            let next_i = (i + 1) % data.len();
            indices.push(i as u32);
            indices.push(next_i as u32);
        }

        // Mock implementation - in real implementation, this would use WebGPU
        Ok(WebGpuRenderResult {
            render_time_ms: 2.5,
            memory_used_bytes: vertices.len() * 16 + indices.len() * 4,
            vertices_rendered: vertices.len(),
        })
    }

    fn render_sankey_diagram(
        &mut self,
        config: &SankeyConfig,
    ) -> Result<WebGpuRenderResult, WebGpuError> {
        // Calculate node positions and sizes
        let mut node_positions = HashMap::new();
        let mut total_height = 0.0;

        for node in &config.nodes {
            let height = (node.value / config.nodes.iter().map(|n| n.value).sum::<f64>()) as f32
                * config.base_config.height as f32;
            node_positions.insert(node.id.clone(), (0.0, total_height, height));
            total_height += height + config.node_padding;
        }

        // Generate vertices for nodes and links
        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        // Render nodes
        for node in &config.nodes {
            if let Some((x, y, height)) = node_positions.get(&node.id) {
                // Create rectangle for node
                let x1 = *x;
                let y1 = *y;
                let x2 = x1 + config.node_width;
                let y2 = y1 + *height;

                // Add rectangle vertices
                vertices.extend_from_slice(&[
                    [x1, y1, 0.0, 1.0],
                    [x2, y1, 0.0, 1.0],
                    [x2, y2, 0.0, 1.0],
                    [x1, y2, 0.0, 1.0],
                ]);

                // Add rectangle indices
                let base_idx = vertices.len() as u32 - 4;
                indices.extend_from_slice(&[
                    base_idx,
                    base_idx + 1,
                    base_idx + 2,
                    base_idx,
                    base_idx + 2,
                    base_idx + 3,
                ]);
            }
        }

        // Mock implementation - in real implementation, this would use WebGPU
        Ok(WebGpuRenderResult {
            render_time_ms: 5.2,
            memory_used_bytes: vertices.len() * 16 + indices.len() * 4,
            vertices_rendered: vertices.len(),
        })
    }

    fn render_treemap(
        &mut self,
        config: &TreemapConfig,
    ) -> Result<WebGpuRenderResult, WebGpuError> {
        // Recursive treemap layout algorithm
        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        fn layout_treemap(
            node: &TreemapNode,
            x: f32,
            y: f32,
            width: f32,
            height: f32,
            vertices: &mut Vec<[f32; 4]>,
            indices: &mut Vec<u32>,
            padding: f32,
        ) {
            if node.children.is_empty() {
                // Leaf node - create rectangle
                let x1 = x + padding;
                let y1 = y + padding;
                let x2 = x + width - padding;
                let y2 = y + height - padding;

                let base_idx = vertices.len() as u32;
                vertices.extend_from_slice(&[
                    [x1, y1, 0.0, 1.0],
                    [x2, y1, 0.0, 1.0],
                    [x2, y2, 0.0, 1.0],
                    [x1, y2, 0.0, 1.0],
                ]);

                indices.extend_from_slice(&[
                    base_idx,
                    base_idx + 1,
                    base_idx + 2,
                    base_idx,
                    base_idx + 2,
                    base_idx + 3,
                ]);
            } else {
                // Internal node - recursively layout children
                let total_value: f64 = node.children.iter().map(|c| c.value).sum();
                let mut current_x = x;
                let mut current_y = y;

                for child in &node.children {
                    let child_ratio = child.value / total_value;
                    let child_width = width * child_ratio as f32;
                    let child_height = height * child_ratio as f32;

                    layout_treemap(
                        child,
                        current_x,
                        current_y,
                        child_width,
                        child_height,
                        vertices,
                        indices,
                        padding,
                    );

                    current_x += child_width;
                    if current_x >= x + width {
                        current_x = x;
                        current_y += child_height;
                    }
                }
            }
        }

        layout_treemap(
            &config.root_node,
            0.0,
            0.0,
            config.base_config.width as f32,
            config.base_config.height as f32,
            &mut vertices,
            &mut indices,
            config.padding,
        );

        // Mock implementation - in real implementation, this would use WebGPU
        Ok(WebGpuRenderResult {
            render_time_ms: 3.8,
            memory_used_bytes: vertices.len() * 16 + indices.len() * 4,
            vertices_rendered: vertices.len(),
        })
    }

    fn render_violin_plot(
        &mut self,
        config: &ViolinConfig,
    ) -> Result<WebGpuRenderResult, WebGpuError> {
        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        let category_width = config.base_config.width as f32 / config.data.len() as f32;
        let max_value = config
            .data
            .iter()
            .flat_map(|d| &d.values)
            .fold(0.0_f64, |acc, &val| acc.max(val)) as f32;

        for (i, data_point) in config.data.iter().enumerate() {
            let category_x = i as f32 * category_width + category_width / 2.0;
            let center_y = config.base_config.height as f32 / 2.0;

            // Calculate kernel density estimation for violin shape
            let mut density_points = Vec::new();
            for &value in &data_point.values {
                let normalized_value =
                    (value as f32 / max_value) * (config.base_config.height as f32 * 0.4);
                density_points.push(normalized_value);
            }

            // Create violin shape vertices
            for (j, &density) in density_points.iter().enumerate() {
                let angle = 2.0 * std::f32::consts::PI * j as f32 / density_points.len() as f32;
                let x = category_x + density * angle.cos();
                let y = center_y + density * angle.sin();

                vertices.push([x, y, 0.0, 1.0]);
            }

            // Generate indices for violin shape
            let base_idx = vertices.len() as u32 - density_points.len() as u32;
            for j in 0..density_points.len() {
                let next_j = (j + 1) % density_points.len();
                indices.extend_from_slice(&[base_idx + j as u32, base_idx + next_j as u32]);
            }
        }

        // Mock implementation - in real implementation, this would use WebGPU
        Ok(WebGpuRenderResult {
            render_time_ms: 4.1,
            memory_used_bytes: vertices.len() * 16 + indices.len() * 4,
            vertices_rendered: vertices.len(),
        })
    }
}

/// Helper function to count treemap nodes recursively
pub fn count_treemap_nodes(node: &TreemapNode) -> usize {
    1 + node.children.iter().map(count_treemap_nodes).sum::<usize>()
}

/// Sample data generators for testing and examples
pub mod sample_data {
    use super::*;

    /// Creates sample radar chart data for testing and examples
    pub fn create_sample_radar_data() -> Vec<RadarDataPoint> {
        vec![
            RadarDataPoint {
                category: "Performance".to_string(),
                value: 85.0,
                max_value: 100.0,
            },
            RadarDataPoint {
                category: "Usability".to_string(),
                value: 92.0,
                max_value: 100.0,
            },
            RadarDataPoint {
                category: "Features".to_string(),
                value: 78.0,
                max_value: 100.0,
            },
            RadarDataPoint {
                category: "Support".to_string(),
                value: 88.0,
                max_value: 100.0,
            },
            RadarDataPoint {
                category: "Price".to_string(),
                value: 95.0,
                max_value: 100.0,
            },
        ]
    }

    /// Creates sample sankey diagram data with nodes and links
    pub fn create_sample_sankey_data() -> (Vec<SankeyNode>, Vec<SankeyLink>) {
        let nodes = vec![
            SankeyNode {
                id: "source1".to_string(),
                name: "Source 1".to_string(),
                value: 100.0,
            },
            SankeyNode {
                id: "source2".to_string(),
                name: "Source 2".to_string(),
                value: 80.0,
            },
            SankeyNode {
                id: "intermediate".to_string(),
                name: "Intermediate".to_string(),
                value: 0.0,
            },
            SankeyNode {
                id: "target1".to_string(),
                name: "Target 1".to_string(),
                value: 0.0,
            },
            SankeyNode {
                id: "target2".to_string(),
                name: "Target 2".to_string(),
                value: 0.0,
            },
        ];

        let links = vec![
            SankeyLink {
                source: "source1".to_string(),
                target: "intermediate".to_string(),
                value: 60.0,
            },
            SankeyLink {
                source: "source2".to_string(),
                target: "intermediate".to_string(),
                value: 50.0,
            },
            SankeyLink {
                source: "intermediate".to_string(),
                target: "target1".to_string(),
                value: 70.0,
            },
            SankeyLink {
                source: "intermediate".to_string(),
                target: "target2".to_string(),
                value: 40.0,
            },
        ];

        (nodes, links)
    }

    /// Creates sample hierarchical treemap data for visualization
    pub fn create_sample_treemap_data() -> TreemapNode {
        TreemapNode {
            id: "root".to_string(),
            name: "Root".to_string(),
            value: 100.0,
            children: vec![
                TreemapNode {
                    id: "category1".to_string(),
                    name: "Category 1".to_string(),
                    value: 40.0,
                    children: vec![
                        TreemapNode {
                            id: "item1".to_string(),
                            name: "Item 1".to_string(),
                            value: 25.0,
                            children: vec![],
                        },
                        TreemapNode {
                            id: "item2".to_string(),
                            name: "Item 2".to_string(),
                            value: 15.0,
                            children: vec![],
                        },
                    ],
                },
                TreemapNode {
                    id: "category2".to_string(),
                    name: "Category 2".to_string(),
                    value: 35.0,
                    children: vec![
                        TreemapNode {
                            id: "item3".to_string(),
                            name: "Item 3".to_string(),
                            value: 20.0,
                            children: vec![],
                        },
                        TreemapNode {
                            id: "item4".to_string(),
                            name: "Item 4".to_string(),
                            value: 15.0,
                            children: vec![],
                        },
                    ],
                },
                TreemapNode {
                    id: "category3".to_string(),
                    name: "Category 3".to_string(),
                    value: 25.0,
                    children: vec![],
                },
            ],
        }
    }

    /// Creates sample violin plot data for statistical visualization
    pub fn create_sample_violin_data() -> Vec<ViolinDataPoint> {
        vec![
            ViolinDataPoint {
                category: "Group A".to_string(),
                values: vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
            },
            ViolinDataPoint {
                category: "Group B".to_string(),
                values: vec![2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0],
            },
            ViolinDataPoint {
                category: "Group C".to_string(),
                values: vec![3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0],
            },
        ]
    }
}