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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
//! Advanced Chart Types for leptos-helios
//!
//! This module implements advanced chart types identified in the feature gap analysis:
//! - Heatmaps with color mapping and clustering
//! - Treemaps with hierarchical data visualization
//! - Sankey diagrams with flow visualization
//!
//! Following TDD methodology: Red -> Green -> Refactor

use std::collections::HashMap;
use std::time::Duration;

/// Heatmap chart for visualizing 2D data with color mapping
#[derive(Debug, Clone)]
pub struct HeatmapChart {
    /// 2D data matrix
    data: Vec<Vec<f64>>,
    /// Color scheme for mapping values to colors
    color_scheme: ColorScheme,
    /// Clusters of similar values
    clusters: Vec<Cluster>,
    /// Selected cells
    selected_cells: std::collections::HashSet<(usize, usize)>,
    /// Min and max values for normalization
    min_value: f64,
    max_value: f64,
    /// Cell dimensions for rendering
    cell_width: f64,
    cell_height: f64,
}

impl HeatmapChart {
    /// Create a new heatmap chart
    pub fn new(data: Vec<Vec<f64>>) -> Self {
        let (min_val, max_val) = Self::calculate_min_max(&data);
        Self {
            data: data.clone(),
            color_scheme: ColorScheme::Viridis,
            clusters: Vec::new(),
            selected_cells: std::collections::HashSet::new(),
            min_value: min_val,
            max_value: max_val,
            cell_width: 0.0,
            cell_height: 0.0,
        }
    }

    /// Get number of rows
    pub fn rows(&self) -> usize {
        self.data.len()
    }

    /// Get number of columns
    pub fn cols(&self) -> usize {
        if self.data.is_empty() {
            0
        } else {
            self.data[0].len()
        }
    }

    /// Get value at specific position
    pub fn get_value(&self, row: usize, col: usize) -> f64 {
        self.data[row][col]
    }

    /// Set color scheme
    pub fn set_color_scheme(&mut self, scheme: ColorScheme) {
        self.color_scheme = scheme;
    }

    /// Get color for specific cell
    pub fn get_color(&self, row: usize, col: usize) -> String {
        let value = self.get_value(row, col);
        let normalized = (value - self.min_value) / (self.max_value - self.min_value);
        self.color_scheme.get_color(normalized)
    }

    /// Check if color is valid
    pub fn is_valid_color(&self, color: String) -> bool {
        color.starts_with('#') && color.len() == 7
    }

    /// Enable clustering of similar values
    pub fn enable_clustering(&mut self, num_clusters: usize) {
        self.clusters = self.perform_clustering(num_clusters);
    }

    /// Get clusters
    pub fn get_clusters(&self) -> &Vec<Cluster> {
        &self.clusters
    }

    /// Get cell at screen position
    pub fn get_cell_at_position(&self, x: f64, y: f64) -> Option<(usize, usize)> {
        if self.cell_width == 0.0 || self.cell_height == 0.0 {
            return None;
        }

        let col = (x / self.cell_width) as usize;
        let row = (y / self.cell_height) as usize;

        if row < self.rows() && col < self.cols() {
            Some((row, col))
        } else {
            None
        }
    }

    /// Select a cell
    pub fn select_cell(&mut self, row: usize, col: usize) {
        self.selected_cells.insert((row, col));
    }

    /// Check if cell is selected
    pub fn is_cell_selected(&self, row: usize, col: usize) -> bool {
        self.selected_cells.contains(&(row, col))
    }

    /// Export as PNG
    pub fn export_png(&self, width: u32, height: u32) -> Vec<u8> {
        // Placeholder implementation
        vec![0u8; (width * height * 4) as usize]
    }

    /// Export as SVG
    pub fn export_svg(&self, width: u32, height: u32) -> String {
        let mut svg = format!(
            r#"<svg width="{}" height="{}" xmlns="http://www.w3.org/2000/svg">"#,
            width, height
        );

        let cell_w = width as f64 / self.cols() as f64;
        let cell_h = height as f64 / self.rows() as f64;

        for row in 0..self.rows() {
            for col in 0..self.cols() {
                let color = self.get_color(row, col);
                let x = col as f64 * cell_w;
                let y = row as f64 * cell_h;

                svg.push_str(&format!(
                    r#"<rect x="{}" y="{}" width="{}" height="{}" fill="{}" />"#,
                    x, y, cell_w, cell_h, color
                ));
            }
        }

        svg.push_str("</svg>");
        svg
    }

    /// Calculate min and max values
    fn calculate_min_max(data: &[Vec<f64>]) -> (f64, f64) {
        let mut min_val = f64::INFINITY;
        let mut max_val = f64::NEG_INFINITY;

        for row in data {
            for &value in row {
                min_val = min_val.min(value);
                max_val = max_val.max(value);
            }
        }

        (min_val, max_val)
    }

    /// Perform clustering using k-means
    fn perform_clustering(&self, num_clusters: usize) -> Vec<Cluster> {
        let mut clusters = Vec::new();

        // Simple clustering based on value ranges
        let range = self.max_value - self.min_value;
        let cluster_size = range / num_clusters as f64;

        for i in 0..num_clusters {
            let start = self.min_value + i as f64 * cluster_size;
            let end = start + cluster_size;

            let mut count = 0;
            for row in &self.data {
                for &value in row {
                    if value >= start && value < end {
                        count += 1;
                    }
                }
            }

            clusters.push(Cluster { size: count });
        }

        clusters
    }
}

/// Treemap chart for hierarchical data visualization
#[derive(Debug, Clone)]
pub struct TreemapChart {
    /// Root node of the tree
    root: TreeNode,
    /// Current root for drill-down functionality
    current_root: TreeNode,
    /// Layout algorithm
    layout_algorithm: LayoutAlgorithm,
    /// Color strategy
    color_strategy: ColorStrategy,
    /// Selected nodes
    selected_nodes: std::collections::HashSet<String>,
    /// Node colors cache
    node_colors: HashMap<String, String>,
}

impl TreemapChart {
    /// Create a new treemap chart
    pub fn new(root: TreeNode) -> Self {
        let current_root = root.clone();
        Self {
            root,
            current_root,
            layout_algorithm: LayoutAlgorithm::Squarified,
            color_strategy: ColorStrategy::ByValue,
            selected_nodes: std::collections::HashSet::new(),
            node_colors: HashMap::new(),
        }
    }

    /// Get total number of nodes
    pub fn total_nodes(&self) -> usize {
        self.count_nodes(&self.root)
    }

    /// Get root node
    pub fn get_root(&self) -> &TreeNode {
        &self.root
    }

    /// Set layout algorithm
    pub fn set_layout_algorithm(&mut self, algorithm: LayoutAlgorithm) {
        self.layout_algorithm = algorithm;
    }

    /// Layout the treemap
    pub fn layout(&mut self, width: f64, height: f64) {
        let mut current_root = self.current_root.clone();
        self.layout_squarified(&mut current_root, 0.0, 0.0, width, height);
        self.current_root = current_root;
    }

    /// Get all nodes
    pub fn get_all_nodes(&self) -> Vec<&TreeNode> {
        self.collect_nodes(&self.current_root)
    }

    /// Set color strategy
    pub fn set_color_strategy(&mut self, strategy: ColorStrategy) {
        self.color_strategy = strategy;
        self.node_colors.clear();
    }

    /// Get node color
    pub fn get_node_color(&self, name: &str) -> String {
        self.node_colors
            .get(name)
            .cloned()
            .unwrap_or_else(|| "#808080".to_string())
    }

    /// Get all node colors
    pub fn get_all_node_colors(&self) -> Vec<String> {
        self.node_colors.values().cloned().collect()
    }

    /// Select a node
    pub fn select_node(&mut self, name: &str) {
        self.selected_nodes.insert(name.to_string());
    }

    /// Check if node is selected
    pub fn is_node_selected(&self, name: &str) -> bool {
        self.selected_nodes.contains(name)
    }

    /// Drill down to a node
    pub fn drill_down(&mut self, name: &str) {
        if let Some(node) = self.find_node(&self.current_root, name) {
            self.current_root = node.clone();
        }
    }

    /// Drill up to parent
    pub fn drill_up(&mut self) {
        // For simplicity, always go back to root
        self.current_root = self.root.clone();
    }

    /// Get current root
    pub fn get_current_root(&self) -> &TreeNode {
        &self.current_root
    }

    /// Animate to new tree state
    pub fn animate_to(&mut self, _new_root: TreeNode, _duration: Duration) -> Option<Animation> {
        // Placeholder implementation
        Some(Animation { progress: 0.0 })
    }

    /// Count nodes recursively
    fn count_nodes(&self, node: &TreeNode) -> usize {
        1 + node
            .children
            .iter()
            .map(|child| self.count_nodes(child))
            .sum::<usize>()
    }

    /// Collect all nodes recursively
    fn collect_nodes<'a>(&self, node: &'a TreeNode) -> Vec<&'a TreeNode> {
        let mut nodes = vec![node];
        for child in &node.children {
            nodes.extend(self.collect_nodes(child));
        }
        nodes
    }

    /// Find node by name
    fn find_node<'a>(&self, node: &'a TreeNode, name: &str) -> Option<&'a TreeNode> {
        if node.name == name {
            return Some(node);
        }

        for child in &node.children {
            if let Some(found) = self.find_node(child, name) {
                return Some(found);
            }
        }

        None
    }

    /// Layout using squarified algorithm
    fn layout_squarified(&mut self, node: &mut TreeNode, x: f64, y: f64, width: f64, height: f64) {
        node.rectangle = Rectangle {
            x,
            y,
            width,
            height,
        };

        if node.children.is_empty() {
            return;
        }

        // Simple layout: divide space proportionally
        let total_value: f64 = node.children.iter().map(|child| child.value).sum();
        let mut current_x = x;
        let mut current_y = y;

        for child in &mut node.children {
            let ratio = child.value / total_value;
            let child_width = width * ratio;
            let child_height = height * ratio;

            self.layout_squarified(child, current_x, current_y, child_width, child_height);

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

/// Sankey diagram for flow visualization
#[derive(Debug, Clone)]
pub struct SankeyDiagram {
    /// Nodes in the diagram
    nodes: Vec<SankeyNode>,
    /// Links between nodes
    links: Vec<SankeyLink>,
    /// Selected nodes
    selected_nodes: std::collections::HashSet<usize>,
    /// Highlighted links
    highlighted_links: std::collections::HashSet<usize>,
    /// Layout dimensions
    width: f64,
    height: f64,
}

impl SankeyDiagram {
    /// Create a new Sankey diagram
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            links: Vec::new(),
            selected_nodes: std::collections::HashSet::new(),
            highlighted_links: std::collections::HashSet::new(),
            width: 0.0,
            height: 0.0,
        }
    }

    /// Add a node
    pub fn add_node(&mut self, name: &str, value: f64) -> usize {
        let id = self.nodes.len();
        self.nodes.push(SankeyNode {
            id,
            name: name.to_string(),
            value,
            position: Point { x: 0.0, y: 0.0 },
        });
        id
    }

    /// Add a link between nodes
    pub fn add_link(&mut self, source: usize, target: usize, value: f64) -> usize {
        let id = self.links.len();
        self.links.push(SankeyLink {
            id,
            source,
            target,
            value,
            path: Vec::new(),
            width: 0.0,
        });
        id
    }

    /// Get number of nodes
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Get number of links
    pub fn link_count(&self) -> usize {
        self.links.len()
    }

    /// Get node value
    pub fn get_node_value(&self, node_id: usize) -> f64 {
        self.nodes[node_id].value
    }

    /// Layout the diagram
    pub fn layout(&mut self, width: f64, height: f64) {
        self.width = width;
        self.height = height;

        // Simple layout: arrange nodes in columns
        let num_columns = 3;
        let column_width = width / num_columns as f64;
        let total_rows = (self.nodes.len() + num_columns - 1) / num_columns;
        let row_height = height / total_rows as f64;

        for (i, node) in self.nodes.iter_mut().enumerate() {
            let column = i % num_columns;
            let row = i / num_columns;

            node.position = Point {
                x: column as f64 * column_width,
                y: row as f64 * row_height,
            };
        }

        // Calculate link paths and widths
        let max_flow: f64 = self.links.iter().map(|link| link.value).sum();
        for link in &mut self.links {
            link.width = (link.value / max_flow) * 50.0; // Max width of 50
            link.path = vec![
                self.nodes[link.source].position.clone(),
                self.nodes[link.target].position.clone(),
            ];
        }
    }

    /// Get all node IDs
    pub fn get_all_node_ids(&self) -> Vec<usize> {
        (0..self.nodes.len()).collect()
    }

    /// Get node position
    pub fn get_node_position(&self, node_id: usize) -> Point {
        self.nodes[node_id].position.clone()
    }

    /// Get all link IDs
    pub fn get_all_link_ids(&self) -> Vec<usize> {
        (0..self.links.len()).collect()
    }

    /// Get link path
    pub fn get_link_path(&self, link_id: usize) -> Vec<Point> {
        self.links[link_id].path.clone()
    }

    /// Get link width
    pub fn get_link_width(&self, link_id: usize) -> f64 {
        self.links[link_id].width
    }

    /// Get link direction
    pub fn get_link_direction(&self, link_id: usize) -> Direction {
        let link = &self.links[link_id];
        let source_pos = &self.nodes[link.source].position;
        let target_pos = &self.nodes[link.target].position;

        if target_pos.x > source_pos.x {
            Direction::Right
        } else if target_pos.x < source_pos.x {
            Direction::Left
        } else if target_pos.y > source_pos.y {
            Direction::Down
        } else if target_pos.y < source_pos.y {
            Direction::Up
        } else {
            Direction::Invalid
        }
    }

    /// Get node at position
    pub fn get_node_at_position(&self, x: f64, y: f64) -> Option<usize> {
        for (i, node) in self.nodes.iter().enumerate() {
            let dx = x - node.position.x;
            let dy = y - node.position.y;
            if dx.abs() < 20.0 && dy.abs() < 20.0 {
                return Some(i);
            }
        }
        None
    }

    /// Get link at position
    pub fn get_link_at_position(&self, x: f64, y: f64) -> Option<usize> {
        for (i, link) in self.links.iter().enumerate() {
            if link.path.len() >= 2 {
                let start = &link.path[0];
                let end = &link.path[1];

                // Simple point-to-line distance check
                let distance = self.point_to_line_distance(x, y, start.x, start.y, end.x, end.y);
                if distance < link.width / 2.0 {
                    return Some(i);
                }
            }
        }
        None
    }

    /// Select a node
    pub fn select_node(&mut self, node_id: usize) {
        self.selected_nodes.insert(node_id);
    }

    /// Check if node is selected
    pub fn is_node_selected(&self, node_id: usize) -> bool {
        self.selected_nodes.contains(&node_id)
    }

    /// Highlight a link
    pub fn highlight_link(&mut self, link_id: usize) {
        self.highlighted_links.insert(link_id);
    }

    /// Check if link is highlighted
    pub fn is_link_highlighted(&self, link_id: usize) -> bool {
        self.highlighted_links.contains(&link_id)
    }

    /// Animate to another diagram
    pub fn animate_to(&mut self, _other: &SankeyDiagram, _duration: Duration) -> Option<Animation> {
        // Placeholder implementation
        Some(Animation { progress: 0.0 })
    }

    /// Export as SVG
    pub fn export_svg(&self, width: u32, height: u32) -> String {
        let mut svg = format!(
            r#"<svg width="{}" height="{}" xmlns="http://www.w3.org/2000/svg">"#,
            width, height
        );

        // Draw links
        for link in &self.links {
            if link.path.len() >= 2 {
                let start = &link.path[0];
                let end = &link.path[1];
                svg.push_str(&format!(
                    "<path d=\"M {} {} L {} {}\" stroke=\"#999\" stroke-width=\"{}\" fill=\"none\" />",
                    start.x, start.y, end.x, end.y, link.width
                ));
            }
        }

        // Draw nodes
        for node in &self.nodes {
            svg.push_str(&format!(
                "<rect x=\"{}\" y=\"{}\" width=\"20\" height=\"20\" fill=\"#666\" />",
                node.position.x - 10.0,
                node.position.y - 10.0
            ));
        }

        svg.push_str("</svg>");
        svg
    }

    /// Export as JSON
    pub fn export_json(&self) -> String {
        format!(
            r#"{{"nodes": {}, "links": {}}}"#,
            serde_json::to_string(&self.nodes).unwrap_or_else(|_| "[]".to_string()),
            serde_json::to_string(&self.links).unwrap_or_else(|_| "[]".to_string())
        )
    }

    /// Set link value
    pub fn set_link_value(&mut self, link_id: usize, value: f64) {
        if link_id < self.links.len() {
            self.links[link_id].value = value;
        }
    }

    /// Calculate point to line distance
    fn point_to_line_distance(&self, px: f64, py: f64, x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
        let dx = x2 - x1;
        let dy = y2 - y1;
        let length = (dx * dx + dy * dy).sqrt();

        if length == 0.0 {
            return ((px - x1) * (px - x1) + (py - y1) * (py - y1)).sqrt();
        }

        let t = ((px - x1) * dx + (py - y1) * dy) / (length * length);
        let t = t.max(0.0).min(1.0);

        let closest_x = x1 + t * dx;
        let closest_y = y1 + t * dy;

        ((px - closest_x) * (px - closest_x) + (py - closest_y) * (py - closest_y)).sqrt()
    }
}

// Supporting types
#[derive(Debug, Clone)]
pub struct TreeNode {
    pub name: String,
    pub value: f64,
    pub children: Vec<TreeNode>,
    pub rectangle: Rectangle,
}

impl TreeNode {
    pub fn new(name: &str, value: f64) -> Self {
        Self {
            name: name.to_string(),
            value,
            children: Vec::new(),
            rectangle: Rectangle {
                x: 0.0,
                y: 0.0,
                width: 0.0,
                height: 0.0,
            },
        }
    }

    pub fn add_child(&mut self, child: TreeNode) {
        self.children.push(child);
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn value(&self) -> f64 {
        self.value
    }

    pub fn get_rectangle(&self) -> &Rectangle {
        &self.rectangle
    }
}

#[derive(Debug, Clone)]
pub struct Rectangle {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

#[derive(Debug, Clone)]
pub struct Cluster {
    size: usize,
}

impl Cluster {
    pub fn size(&self) -> usize {
        self.size
    }
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct SankeyNode {
    id: usize,
    name: String,
    value: f64,
    position: Point,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct SankeyLink {
    id: usize,
    source: usize,
    target: usize,
    value: f64,
    path: Vec<Point>,
    width: f64,
}

#[derive(Debug, Clone)]
pub struct Animation {
    progress: f64,
}

impl Animation {
    pub fn get_progress(&self) -> f64 {
        self.progress
    }
}

#[derive(Debug, Clone)]
pub struct ChartRenderer;

impl ChartRenderer {
    pub fn new() -> Self {
        Self
    }

    pub fn can_render<T>(&self, _chart: &T) -> bool {
        true // All chart types can be rendered
    }
}

#[derive(Debug, Clone)]
pub enum ColorScheme {
    Default,
    Viridis,
    Plasma,
    Inferno,
    Magma,
}

impl ColorScheme {
    pub fn get_color(&self, normalized_value: f64) -> String {
        match self {
            ColorScheme::Viridis => {
                // Simplified Viridis color mapping
                let r = (normalized_value * 255.0) as u8;
                let g = ((1.0 - normalized_value) * 255.0) as u8;
                let b = 128;
                format!("#{:02x}{:02x}{:02x}", r, g, b)
            }
            _ => "#808080".to_string(),
        }
    }
}

#[derive(Debug, Clone)]
pub enum LayoutAlgorithm {
    Squarified,
    SliceAndDice,
    Strip,
}

#[derive(Debug, Clone)]
pub enum ColorStrategy {
    ByValue,
    ByDepth,
    ByCategory,
}

#[derive(Debug, Clone)]
pub enum Direction {
    Up,
    Down,
    Left,
    Right,
    Invalid,
}

impl Direction {
    pub fn is_valid(&self) -> bool {
        !matches!(self, Direction::Invalid)
    }
}