webrust 2.0.0

Python-like Rust for Web Applications - A bridge between Python simplicity and Rust power
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
// webrust/src/io/chart.rs
//! # Module `io::chart` — Minimal, fast ECharts wrapper for WebRust
//!
//! This module exposes a tiny, allocation-lean API to render common charts in
//! the browser using ECharts. It is designed to be:
//! - **Simple:** one constructor per chart type (`line`, `bar`, `pie`, `radar`, …).
//! - **Zero-setup:** charts auto-mount and render when dropped.
//! - **Compact:** minimal JS per chart, no global state beyond an internal id counter.
//!
//! ## Quick start
//! ```rust,no_run
//! use webrust::prelude::*;
//!
//! #[gui(Arial 14px black !white)]
//! fn main() {
//!     let months = ["Jan","Feb","Mar","Apr","May"];
//!     let sales  = [120.0, 200.0, 150.0, 300.0, 250.0];
//!
//!     line(&months, &sales);      // category line
//!     bar(&months, &sales);       // category bar
//!     pie(&["A","B","C"], &[60.0,30.0,10.0]);
//!
//!     let skills = [85.0,90.0,75.0,95.0,80.0];
//!     let inds   = [("Technical",100.0),("Communication",100.0),("Leadership",100.0),
//!                   ("Innovation",100.0),("Quality",100.0)];
//!     radar(&skills, &inds);      // interactive radar
//! }
//! ```
//!
//! ## Chart constructors
//! Each constructor returns a `Chart` that renders automatically when it goes out
//! of scope (on `Drop`). You can optionally position/size a chart with `.at(x,y)`
//! (Cartesian coordinates managed by WebRust) and `.size(w,h)` (pixels).
//!
//! **Category (x = labels, y = values)**
//! - `line(labels, values)` — Polyline (tooltip on axis).
//! - `curve(labels, values)` — Smoothed line.
//! - `area(labels, values)` — Filled line area.
//! - `bar(labels, values)` — Vertical bars.
//!
//! **XY pairs (numeric x,y)**
//! - `line_xy(x, y)` / `curve_xy(x, y)` / `area_xy(x, y)` — Use numeric x axis.
//! - `scatter(x, y)` — Dots with item tooltip.
//!
//! **Compositions**
//! - `pie(labels, values)` — Standard pie (`values` are % or any totals).
//! - `doughnut(labels, values)` — Pie with a hole (default inner radius 50%).
//! - `funnel(stages)` — Descending funnel (`&[(&str, f64)]`).
//! - `gauge(value)` — 0–100 radial gauge with % tooltip.
//!
//! **Radar**
//! - `radar(values, indicators)` — One-series radar with custom indicators
//!   (`indicators: &[(&str, f64)]` where `max` is the axis maximum).
//!   The chart shows clean axis names by default and reveals `name: value %` near
//!   the corresponding key when hovering the vertex (no clutter, one value at a time).
//!
//! **Matrix / Heatmap**
//! - `matrix(data, xlabels, ylabels)` — Heatmap with auto visual scale.
//!
//! **Tree / Treemap**
//! - `treemap(items)` — Accepts flat items or path-like `"A/B/C"` entries.
//! - `tree(input)` — Accepts:
//!   - `&serde_json::Value` (object becomes a tree),
//!   - `&str` (JSON string or newline-separated paths),
//!   - `&[&str]` (path slices).
//!
//! **Function sampling**
//! - `function(f, x0, x1, step)` — Samples `f64 -> f64` and renders a smooth curve.
//!
//! ## Layout helpers
//! - `.at(x, y)` — Places the chart at world coordinates (converted by WebRust).
//! - `.size(w, h)` — Sets container size (px). Without `.size`, a sensible
//!   default height is applied depending on the responsive container class.
//!
//! ## Rendering model
//! - Charts render via ECharts once the GUI is ready (async init in an inline `<script>`).
//! - A unique DOM id is generated using an atomic counter; no global mutable Rust state
//!   is exposed.
//! - `Drop` injects the container `<div>` + the minimal JS to configure the instance.
//!
//! ## Styling
//! - Charts inherit the global theme from `#[gui(...)]` (font, base color, background)
//!   through the surrounding page styles.
//! - Axis/label font sizes are tuned for compact layouts (e.g., `fontSize: 7` on axes).
//! - Radar axes show names in black; the hovered key/value is highlighted for clarity.
//!
//! ## Data contracts & panics
//! - All constructors expect **matching lengths** between label/value vectors where
//!   applicable. Mismatches or missing internals will cause a panic via `unwrap()`.
//! - Radar requires `values.len() == indicators.len()`.
//! - `matrix` requires consistent row lengths; `xlabels.len()` must match columns,
//!   `ylabels.len()` must match rows.
//!
//! ## Performance & footprint
//! - JS emitted per chart is small and self-contained; no shared registries.
//! - The Rust side avoids unnecessary cloning beyond converting inputs to owned
//!   vectors (to survive `Drop` scheduling).
//!
//! ## Accessibility & behavior
//! - Tooltips are concise; radar shows a single value near the hovered vertex
//!   to reduce overload.
//! - Charts resize responsively; a resize listener updates layout.
//!
//! ## Troubleshooting
//! - **Blank chart:** ensure ECharts is available in the page (WebRust GUI loads it).
//! - **Panic at runtime:** verify inputs (vector lengths, percentages, matrix dimensions).
//! - **Small texts:** increase container size with `.size(w,h)` or change GUI font size.
//!
//! ---


use serde::Serialize;
use std::sync::atomic::{AtomicUsize, Ordering};

static CHART_COUNTER: AtomicUsize = AtomicUsize::new(0);
fn next_chart_id() -> String { format!("chart_{}", CHART_COUNTER.fetch_add(1, Ordering::Relaxed) + 1) }

#[derive(Serialize, Clone)]
pub struct RadarIndicator { name: String, max: f64 }

#[derive(Serialize, Clone)]
pub struct Chart {
    kind: String,
    x: Option<Vec<f64>>,
    y: Option<Vec<f64>>,
    x_labels: Option<Vec<String>>,
    smooth: bool,
    pie_labels: Option<Vec<String>>,
    pie_values: Option<Vec<f64>>,
    indicators: Option<Vec<RadarIndicator>>,
    radar_values: Option<Vec<f64>>,
    gauge_val: Option<f64>,
    funnel_labels: Option<Vec<String>>,
    funnel_values: Option<Vec<f64>>,
    hole: Option<u32>,
    matrix: Option<Vec<Vec<f64>>>,
    matrix_xlabels: Option<Vec<String>>,
    matrix_ylabels: Option<Vec<String>>,
    treemap_data: Option<serde_json::Value>,
    tree_data: Option<serde_json::Value>,
    position: Option<(f64, f64)>,
    size: Option<(u32, u32)>,
}

impl Chart {
    fn script(&self, div_id: &str) -> String {
        let start = format!(r#"<script>
requestAnimationFrame(function init(){{if(!window.echarts||!document.getElementById('{}')){{requestAnimationFrame(init);return;}}var c=echarts.init(document.getElementById('{}'));"#, div_id, div_id);
        let opts = match self.kind.as_str() {
            "line" => self.opt_line(false),
            "curve" => self.opt_line(true),
            "area" => self.opt_area(),
            "line_xy" => self.opt_line_xy(false),
            "curve_xy" => self.opt_line_xy(true),
            "area_xy" => self.opt_area_xy(),
            "bar" => self.opt_bar(),
            "scatter" => self.opt_scatter(),
            "pie" => self.opt_pie(false),
            "doughnut" => self.opt_pie(true),
            "radar" => self.opt_radar(div_id),
            "gauge" => self.opt_gauge(),
            "funnel" => self.opt_funnel(),
            "matrix" => self.opt_matrix(),
            "treemap" => self.opt_treemap(),
            "tree" => self.opt_tree(),
            _ => String::new(),
        };
        format!("{}{}\nwindow.addEventListener('resize',function(){{c.resize();}});}});</script>", start, opts)
    }

    fn ds_pairs(&self) -> String {
        let xs = self.x.as_ref().unwrap();
        let ys = self.y.as_ref().unwrap();
        serde_json::to_string(&xs.iter().zip(ys.iter()).map(|(a,b)| serde_json::json!([a,b])).collect::<Vec<_>>()).unwrap()
    }

    fn tooltip_xy_axis() -> &'static str {
        "formatter:(p)=>{if(!p||!p.length)return'';const d=p[0].data;return Array.isArray(d)?`(${d[0].toFixed(2)}, ${d[1].toFixed(2)})`:`(${p[0].axisValue}, ${p[0].data.toFixed(2)})`;}"
    }

    fn opt_line_xy(&self, smooth: bool) -> String {
        let ds = self.ds_pairs();
        let sm = if smooth || self.smooth { "true" } else { "false" };
        format!(r#"c.setOption({{
 tooltip:{{trigger:'axis',{}}},
 grid:{{left:30,right:10,top:10,bottom:20}},
 xAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}},axisLine:{{show:true}}}},
 dataset:{{source:{}}},
 series:[{{type:'line',encode:{{x:0,y:1}},smooth:{},lineStyle:{{width:2}}}}]
}});"#, Self::tooltip_xy_axis(), ds, sm)
    }

    fn opt_area_xy(&self) -> String {
        let ds = self.ds_pairs();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'axis',{}}},
 grid:{{left:30,right:10,top:10,bottom:20}},
 xAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 dataset:{{source:{}}},
 series:[{{type:'line',encode:{{x:0,y:1}},smooth:true,areaStyle:{{}},lineStyle:{{width:2}}}}]
}});"#, Self::tooltip_xy_axis(), ds)
    }

    fn opt_line(&self, smooth: bool) -> String {
        let labels = serde_json::to_string(self.x_labels.as_ref().unwrap()).unwrap();
        let values = serde_json::to_string(self.y.as_ref().unwrap()).unwrap();
        let sm = if smooth || self.smooth { "true" } else { "false" };
        format!(r#"c.setOption({{
 tooltip:{{trigger:'axis'}},
 grid:{{left:30,right:10,top:10,bottom:20}},
 xAxis:{{type:'category',data:{},axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 series:[{{type:'line',data:{},smooth:{},lineStyle:{{width:2}}}}]
}});"#, labels, values, sm)
    }

    fn opt_area(&self) -> String {
        let labels = serde_json::to_string(self.x_labels.as_ref().unwrap()).unwrap();
        let values = serde_json::to_string(self.y.as_ref().unwrap()).unwrap();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'axis'}},
 grid:{{left:30,right:10,top:10,bottom:20}},
 xAxis:{{type:'category',data:{},axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 series:[{{type:'line',data:{},smooth:true,areaStyle:{{}},lineStyle:{{width:2}}}}]
}});"#, labels, values)
    }

    fn opt_bar(&self) -> String {
        let labels = serde_json::to_string(self.x_labels.as_ref().unwrap()).unwrap();
        let values = serde_json::to_string(self.y.as_ref().unwrap()).unwrap();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'axis',axisPointer:{{type:'shadow'}}}},
 grid:{{left:30,right:10,top:10,bottom:20}},
 xAxis:{{type:'category',data:{},axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 series:[{{type:'bar',data:{}}}]
}});"#, labels, values)
    }

    fn opt_scatter(&self) -> String {
        let ds = self.ds_pairs();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'item',formatter:(p)=>`(${{p.data[0].toFixed(2)}}, ${{p.data[1].toFixed(2)}})`}},
 grid:{{left:45,right:10,top:15,bottom:35}},
 xAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 yAxis:{{type:'value',axisLabel:{{fontSize:7}}}},
 dataset:{{source:{}}},
 series:[{{type:'scatter',encode:{{x:0,y:1}},symbolSize:6}}]
}});"#, ds)
    }

    fn opt_pie(&self, doughnut: bool) -> String {
        let labels = self.pie_labels.as_ref().unwrap();
        let values = self.pie_values.as_ref().unwrap();
        let data = serde_json::to_string(&labels.iter().zip(values.iter()).map(|(n,v)| serde_json::json!({"name":n,"value":v})).collect::<Vec<_>>()).unwrap();
        let radius = if doughnut { format!(r#"["{}%","70%"]"#, self.hole.unwrap_or(50)) } else { r#""50%""#.into() };
        format!(r#"c.setOption({{
 tooltip:{{trigger:'item',formatter:'{{b}}: {{d}}%'}},
 legend:{{show:false}},
 series:[{{type:'pie',radius:{},center:['50%','50%'],avoidLabelOverlap:true,label:{{fontSize:7,formatter:'{{b}}'}},labelLine:{{length:5,length2:3}},data:{}}}]
}});"#, radius, data)
    }

    fn opt_radar(&self, div_id: &str) -> String {
        let inds = serde_json::to_string(self.indicators.as_ref().unwrap()).unwrap();
        let vals = serde_json::to_string(self.radar_values.as_ref().unwrap()).unwrap();
        let indicators = self.indicators.as_ref().unwrap();
        let values = self.radar_values.as_ref().unwrap();
        let mut names = Vec::new();
        let mut vals_list = Vec::new();
        for (i, ind) in indicators.iter().enumerate() {
            names.push(format!("'{}'", ind.name));
            vals_list.push(values[i].to_string());
        }
        let names_js = format!("[{}]", names.join(","));
        let vals_js = format!("[{}]", vals_list.join(","));

        format!(r#"c.setOption({{
 tooltip:{{show:false}},
 legend:{{show:false}},
 radar:{{indicator:{}}},
 series:[{{
  type:'radar',
  data:[{{value:{},name:'Data'}}],
  symbol:'circle',
  symbolSize:6,
  lineStyle:{{width:2}},
  areaStyle:{{opacity:0.25}}
 }}]
}});
var showValues=false;
var names={};
var values={};
document.getElementById('{}').addEventListener('mouseenter',function(){{
 showValues=true;
 c.setOption({{
  radar:{{
   indicator:names.map(function(n,i){{return{{name:n+': '+values[i]+' %',max:100}};}})
  }}
 }});
}});
document.getElementById('{}').addEventListener('mouseleave',function(){{
 showValues=false;
 c.setOption({{
  radar:{{
   indicator:names.map(function(n){{return{{name:n,max:100}};}})
  }}
 }});
}});"#, inds, vals, names_js, vals_js, div_id, div_id)
    }

    fn opt_gauge(&self) -> String {
        let v = self.gauge_val.unwrap_or(0.0);
        format!(r#"c.setOption({{
 tooltip:{{show:true,formatter:'{{c}}%'}},
 series:[{{type:'gauge',startAngle:200,endAngle:-20,min:0,max:100,progress:{{show:true,width:12}},pointer:{{show:true,length:'70%'}},axisLine:{{lineStyle:{{width:12}}}},detail:{{valueAnimation:true,formatter:'{{value}}%',fontSize:18}},data:[{{value:{}}}]}}]
}});"#, v)
    }

    fn opt_funnel(&self) -> String {
        let labels = self.funnel_labels.as_ref().unwrap();
        let values = self.funnel_values.as_ref().unwrap();
        let data = serde_json::to_string(&labels.iter().zip(values.iter()).map(|(n,v)| serde_json::json!({"name":n,"value":v})).collect::<Vec<_>>()).unwrap();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'item',formatter:'{{b}}: {{d}}%'}},
 legend:{{show:false}},
 series:[{{type:'funnel',left:'10%',top:10,bottom:10,width:'80%',sort:'descending',gap:2,label:{{show:true,position:'inside',fontSize:7}},labelLine:{{length:5}},itemStyle:{{borderColor:'#fff',borderWidth:1}},data:{}}}]
}});"#, data)
    }

    fn opt_matrix(&self) -> String {
        let m = self.matrix.as_ref().unwrap();
        let mut values = Vec::new();
        for (i,row) in m.iter().enumerate() { for (j,&v) in row.iter().enumerate() { values.push(serde_json::json!([j,i,v])); } }
        let data = serde_json::to_string(&values).unwrap();
        let xl = serde_json::to_string(self.matrix_xlabels.as_ref().unwrap()).unwrap();
        let yl = serde_json::to_string(self.matrix_ylabels.as_ref().unwrap()).unwrap();
        let vmax = m.iter().flat_map(|r| r.iter().copied()).fold(0.0, f64::max);
        format!(r#"c.setOption({{
 tooltip:{{position:'top'}},
 grid:{{height:'60%',top:'10%'}},
 xAxis:{{type:'category',data:{},splitArea:{{show:true}}}},
 yAxis:{{type:'category',data:{},splitArea:{{show:true}}}},
 visualMap:{{min:0,max:{},calculable:true,orient:'horizontal',left:'center',bottom:'5%'}},
 series:[{{type:'heatmap',data:{},label:{{show:false}}}}]
}});"#, xl, yl, vmax, data)
    }

    fn opt_treemap(&self) -> String {
        let data = serde_json::to_string(self.treemap_data.as_ref().unwrap()).unwrap();
        format!(r#"c.setOption({{
 toolbox:{{show:false,feature:{{}}}},
 tooltip:{{formatter:(p)=>p.name?`${{p.name}}: ${{p.value}}`:''}},
 series:[{{
    type:'treemap',
    data:{},
    leafDepth:3,
    label:{{show:true,fontSize:10}},
    upperLabel:{{show:true}},
    levels:[{{itemStyle:{{borderColor:'#fff',borderWidth:1}}}}],
    roam:false,
    breadcrumb:{{show:false}}
}}]
}});"#, data)
    }

    fn opt_tree(&self) -> String {
        let data = serde_json::to_string(self.tree_data.as_ref().unwrap()).unwrap();
        format!(r#"c.setOption({{
 tooltip:{{trigger:'item',triggerOn:'mousemove',formatter:(p)=>p.name||''}},
 series:[{{type:'tree',data:{},symbol:'circle',symbolSize:8,edgeShape:'polyline',left:'15%',right:'15%',top:'5%',bottom:'5%',label:{{position:'left',verticalAlign:'middle',align:'right'}},leaves:{{label:{{position:'right',verticalAlign:'middle',align:'left'}}}},expandAndCollapse:true,animationDuration:300,animationDurationUpdate:300}}]
}});"#, data)
    }

    pub fn at(mut self, x: f64, y: f64) -> Self { self.position = Some((x, y)); self }
    pub fn size(mut self, w: u32, h: u32) -> Self { self.size = Some((w, h)); self }
}

impl crate::layout::grid::Sizable for Chart { fn set_size(&mut self, size: (u32, u32)) { self.size = Some(size); } }

impl Drop for Chart {
    fn drop(&mut self) {
        let id = next_chart_id();
        let div = if let Some((x, y)) = self.position {
            let (left, top) = crate::layout::coord::to_screen_coords(x, y);
            let (w, h) = self.size.unwrap_or((400, 300));
            format!(r#"<div style="position:absolute;left:{}px;top:{}px;width:{}px;height:{}px;transform:translate(-50%,-50%);"><div id="{}" style="width:100%;height:100%"></div></div>"#, left, top, w, h, id)
        } else {
            let (class, h) = match self.size {
                Some((w, h)) if w <= 160 || h <= 160 => ("chart-container chart-small", h),
                Some((w, h)) if w <= 220 || h <= 220 => ("chart-container chart-medium", h),
                Some((_, h)) => ("chart-container", h),
                None => ("chart-container", 400),
            };
            format!(r#"<div class="{}"><div id="{}" style="height:{}px"></div></div>"#, class, id, h)
        };
        crate::io::gui::add_output(format!("{}{}", div, self.script(&id)));
    }
}

fn base(kind: &str) -> Chart {
    Chart {
        kind: kind.into(),
        x: None,
        y: None,
        x_labels: None,
        smooth: false,
        pie_labels: None,
        pie_values: None,
        indicators: None,
        radar_values: None,
        gauge_val: None,
        funnel_labels: None,
        funnel_values: None,
        hole: None,
        matrix: None,
        matrix_xlabels: None,
        matrix_ylabels: None,
        treemap_data: None,
        tree_data: None,
        position: None,
        size: None,
    }
}

pub fn line_xy(x: &[f64], y: &[f64]) -> Chart { let mut c = base("line_xy"); c.x = Some(x.to_vec()); c.y = Some(y.to_vec()); c }
pub fn curve_xy(x: &[f64], y: &[f64]) -> Chart { let mut c = base("curve_xy"); c.x = Some(x.to_vec()); c.y = Some(y.to_vec()); c.smooth = true; c }
pub fn area_xy(x: &[f64], y: &[f64]) -> Chart { let mut c = base("area_xy"); c.x = Some(x.to_vec()); c.y = Some(y.to_vec()); c }

pub fn line(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("line"); c.x_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.y = Some(values.to_vec()); c }
pub fn curve(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("curve"); c.x_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.y = Some(values.to_vec()); c.smooth = true; c }
pub fn area(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("area"); c.x_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.y = Some(values.to_vec()); c }
pub fn bar(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("bar"); c.x_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.y = Some(values.to_vec()); c }

pub fn scatter(x: &[f64], y: &[f64]) -> Chart { let mut c = base("scatter"); c.x = Some(x.to_vec()); c.y = Some(y.to_vec()); c }
pub fn pie(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("pie"); c.pie_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.pie_values = Some(values.to_vec()); c }
pub fn doughnut(labels: &[&str], values: &[f64]) -> Chart { let mut c = base("doughnut"); c.pie_labels = Some(labels.iter().map(|s| (*s).to_string()).collect()); c.pie_values = Some(values.to_vec()); c.hole = Some(50); c }

pub fn radar(values: &[f64], indicators: &[(&str, f64)]) -> Chart {
    let mut c = base("radar");
    c.radar_values = Some(values.to_vec());
    c.indicators = Some(indicators.iter().map(|(n,m)| RadarIndicator { name:(*n).to_string(), max:*m }).collect());
    c
}

pub fn gauge(value: f64) -> Chart { let mut c = base("gauge"); c.gauge_val = Some(value); c }

pub fn funnel(stages: &[(&str, f64)]) -> Chart {
    let mut c = base("funnel");
    c.funnel_labels = Some(stages.iter().map(|(n,_)| (*n).to_string()).collect());
    c.funnel_values = Some(stages.iter().map(|(_,v)| *v).collect());
    c
}

fn path_push(root: &mut serde_json::Value, path: &str, val: Option<f64>) {
    let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
    let mut cur = root;
    for (i, part) in parts.iter().enumerate() {
        let is_last = i == parts.len() - 1;
        cur = {
            let children = cur.get_mut("children").and_then(|c| c.as_array_mut()).unwrap();
            if is_last {
                children.push(serde_json::json!({"name":part.to_string(),"value":val.unwrap_or(1.0)}));
                return;
            } else {
                let pos = children.iter().position(|n| n.get("name").and_then(|s| s.as_str()) == Some(part));
                let idx = match pos {
                    Some(i) => i,
                    None => { children.push(serde_json::json!({"name":part.to_string(),"children":[] })); children.len()-1 }
                };
                children.get_mut(idx).unwrap()
            }
        };
    }
}

pub fn treemap(items: &[(&str, f64)]) -> Chart {
    let mut root = serde_json::json!([]);
    let has_path = items.iter().any(|(n,_)| n.contains('/'));
    if has_path {
        let mut r = serde_json::json!({"name":"root","children":[]});
        for (name,val) in items { path_push(&mut r, name, Some(*val)); }
        root.as_array_mut().unwrap().push(r);
    } else {
        for (n,v) in items { root.as_array_mut().unwrap().push(serde_json::json!({"name":n.to_string(),"value":v})); }
    }
    let mut c = base("treemap");
    c.treemap_data = Some(root);
    c
}

fn json_to_tree(val: &serde_json::Value) -> Option<serde_json::Value> {
    match val {
        serde_json::Value::Object(map) => {
            let mut result = Vec::new();
            for (key, value) in map {
                match value {
                    serde_json::Value::Null => {
                        result.push(serde_json::json!({"name": key}));
                    }
                    serde_json::Value::Object(_) => {
                        if let Some(children) = json_to_tree(value) {
                            result.push(serde_json::json!({
                                "name": key,
                                "children": children
                            }));
                        }
                    }
                    _ => {
                        result.push(serde_json::json!({"name": key}));
                    }
                }
            }
            Some(serde_json::Value::Array(result))
        }
        _ => None
    }
}

pub fn tree<T: TreeInput>(input: T) -> Chart { input.to_tree_chart() }

pub trait TreeInput { fn to_tree_chart(self) -> Chart; }

impl TreeInput for &serde_json::Value {
    fn to_tree_chart(self) -> Chart {
        let tree_data = if let Some(children) = json_to_tree(self) {
            children
        } else {
            serde_json::json!([])
        };
        let mut c = base("tree");
        c.tree_data = Some(tree_data);
        c
    }
}

impl TreeInput for &str {
    fn to_tree_chart(self) -> Chart {
        match serde_json::from_str::<serde_json::Value>(self) {
            Ok(val) => (&val).to_tree_chart(),
            Err(_) => {
                let paths: Vec<&str> = self.lines().map(|s| s.trim()).filter(|s| !s.is_empty()).collect();
                paths.as_slice().to_tree_chart()
            }
        }
    }
}

impl TreeInput for &[&str] {
    fn to_tree_chart(self) -> Chart {
        let mut temp_root = serde_json::json!({"name":"root","children":[]});
        for p in self { path_push(&mut temp_root, p, None); }
        let children = temp_root.get("children").and_then(|c| c.as_array()).unwrap_or(&vec![]).clone();
        let mut c = base("tree");
        c.tree_data = Some(serde_json::Value::Array(children));
        c
    }
}

pub fn matrix(data: &[Vec<f64>], xlabels: &[&str], ylabels: &[&str]) -> Chart {
    let mut c = base("matrix");
    c.matrix = Some(data.to_vec());
    c.matrix_xlabels = Some(xlabels.iter().map(|s| (*s).to_string()).collect());
    c.matrix_ylabels = Some(ylabels.iter().map(|s| (*s).to_string()).collect());
    c
}

pub fn function<F: Fn(f64)->f64>(f: F, x0: f64, x1: f64, step: f64) -> Chart {
    let mut x = Vec::new();
    let mut y = Vec::new();
    let mut t = x0;
    while t <= x1 {
        x.push(t);
        y.push(f(t));
        t += step;
    }
    curve_xy(&x, &y)
}