apache-echarts-wrapper 0.0.12

Wrapper for Apache ECharts server-side rendering
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::axis_typing::{AxisInfo, AxisKindMarker, ValueSerializeWrapper};
use crate::common::Percent;

/// Root object for ECharts configuration
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EChartOptions<X:AxisKindMarker,Y:AxisKindMarker> {
    /// Chart title options
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) title: Option<Title>,

    /// Grid positioning and style options
    #[serde(skip_serializing_if = "Option::is_none")]
    pub grid: Option<Grid>,

    /// Tooltip options
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tooltip: Option<Tooltip>,

    /// Legend options
    #[serde(skip_serializing_if = "Option::is_none")]
    pub legend: Option<Legend>,

    /// Dataset component for providing data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) dataset: Option<Vec<DatasetComponent<X,Y>>>,

    /// X-axis options (Cartesian charts)
    pub x_axis: Axis<X>,

    /// Y-axis options (Cartesian charts)
    pub y_axis: Axis<Y>,

    /// Series data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) series: Option<Vec<Series<X,Y>>>,

    /// Additional raw options not covered by this binding
    #[serde(flatten)]
    pub extra: Option<Value>,
}

/// Keyword positions supported by ECharts
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum PositionKeyword {
    Left,
    Right,
    Top,
    Bottom,
    Middle,
    Center,
    Auto
}


/// Position enum supporting keyword, numeric px, percent, or other strings
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Position {
    /// Predefined keyword position
    Keyword(PositionKeyword),
    /// Numeric pixel value
    Number(f64),
    /// Percentage value (e.g., 50 => "50%")
    Percent(Percent)
}

/// Title component
#[derive(Serialize, Deserialize, Debug, Clone,Default)]
#[serde(rename_all = "camelCase")]
pub struct Title {
    /// Main title text
    pub text: Option<String>,

    /// Subtitle text
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_text: Option<String>,

    /// Link for title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link: Option<String>,

    /// Left position (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub left: Option<Position>,

    /// Top position (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top: Option<Position>,

    /// Right position (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub right: Option<Position>,

    /// Bottom position (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bottom: Option<Position>,

    /// Additional raw title options
    #[serde(flatten)]
    pub extra: Option<Value>,
}

impl Title {
    pub fn new(text: &str) -> Title {
        Self{
            text: Some(text.to_string()),
            sub_text: None,
            link: None,
            left: None,
            top: None,
            right: None,
            bottom: None,
            extra: None,
        }
    }
}

/// Grid component
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Grid {
    /// Distance between grid and left side (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub left: Option<Position>,

    /// Distance between grid and right side (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub right: Option<Position>,

    /// Distance between grid and top side (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top: Option<Position>,

    /// Distance between grid and bottom side (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bottom: Option<Position>,

    /// Whether the grid area contain the axis labels
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contain_label: Option<bool>,

    /// Background color of the grid
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background_color: Option<Value>,

    /// Border color of the grid
    #[serde(skip_serializing_if = "Option::is_none")]
    pub border_color: Option<Value>,

    /// Border width of the grid
    #[serde(skip_serializing_if = "Option::is_none")]
    pub border_width: Option<f64>,

    /// Show the border of the grid
    #[serde(skip_serializing_if = "Option::is_none")]
    pub show: Option<bool>,

    /// z-index of the grid component
    #[serde(skip_serializing_if = "Option::is_none")]
    pub z: Option<i32>,

    /// z-level of the grid component
    #[serde(skip_serializing_if = "Option::is_none")]
    pub zlevel: Option<i32>,

    /// Additional raw grid options
    #[serde(flatten)]
    pub extra: Option<Value>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum TooltipTrigger {
    Item,
    Axis,
    None
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum AxisPointerType {
    Cross,
    Line,
    Shadow,
    None
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum AxisPointerTargetAxis{
    Auto,
    X,
    Y,
    Radius,
    Angle
}


#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AxisPointer{
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<AxisPointerType>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub snap : Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub animation : Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub axis: Option<AxisPointerTargetAxis>


}

/// Tooltip component
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Tooltip {
    pub show: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub show_delay: Option<i32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub hide_delay: Option<i32>,

    /// Trigger mode: item, axis
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger: Option<TooltipTrigger>,

    /// Tooltip formatter template or callback
    #[serde(skip_serializing_if = "Option::is_none")]
    pub formatter: Option<Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub axis_pointer: Option<AxisPointer>,

}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum LegendOrient {
    Horizontal,
    Vertical
}

/// Legend component
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Legend {
    /// Data items in the legend
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Vec<String>>,

    /// Legend orientation: vertical or horizontal
    #[serde(skip_serializing_if = "Option::is_none")]
    pub orient: Option<LegendOrient>,

    /// Left position (Keyword, numeric px, percent, or other)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub left: Option<Position>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub right: Option<Position>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub top: Option<Position>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub bottom: Option<Position>
}

impl Default for Legend {
    fn default() -> Self {
        Self{
            data: None,
            orient: None,
            left: None,
            right:  None,
            top:  None,
            bottom: None,
        }
    }
}


/// Axis types supported by ECharts
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AxisType {
    Value,
    Category,
    Time,
    Log
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct  NamedValuePair<X:AxisKindMarker,Y:AxisKindMarker>{
    value: (ValueSerializeWrapper<X>,ValueSerializeWrapper<Y>),
    name: String,
}

impl<X:AxisKindMarker,Y:AxisKindMarker> NamedValuePair<X,Y> {
    pub fn new(x: X, y: Y, name: String) -> Self {
        Self{
            value: (x.into(), y.into()),
            name,
        }
    }

}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct  NamedValue<X:AxisKindMarker>{
    value: ValueSerializeWrapper<X>,
    name: String,
}


/// Axis (cartesian)
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Axis<T:AxisKindMarker> {
    /// Axis type: value, category, time, log
    pub(crate) r#type: AxisType,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) inverse: Option<bool>,

    /// Axis name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) name: Option<String>,

    /// Data for category axis
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Vec<ValueSerializeWrapper<T>>>,

    /// Additional raw axis options
    #[serde(flatten)]
    pub extra: Option<Value>,
}

impl<T:AxisKindMarker> Default for Axis<T> {
    fn default() -> Self {
        Self{
            r#type: T::AxisType::AXIS_TYPE,
            inverse: None,
            name: None,
            data: None,
            extra: None,
        }
    }
}


impl<T:AxisKindMarker> Axis<T>{

    pub fn new_named(name: String)-> Self{
        Self{
            r#type:  T::AxisType::AXIS_TYPE,
            name: Some(name),
            inverse: None,
            data: None,
            extra: None
        }
    }

    pub fn new(name: String, is_log:bool, inverse: bool)-> Self{
        if T::AxisType::AXIS_TYPE == AxisType::Value && is_log {
            Self{
                r#type:  AxisType::Log,
                name: Some(name),
                inverse: Some(inverse),
                data: None,
                extra: None,
            }
        }else {
            Self{
                r#type:  T::AxisType::AXIS_TYPE,
                name: Some(name.to_string()),
                inverse: Some(inverse),
                data: None,
                extra: None,
            }
        }
    }
}


/// Available series types in ECharts
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum SeriesType {
    Line,
    Bar,
    Pie,
    Scatter,
    EffectScatter,
    Radar,
    Tree,
    Treemap,
    Sunburst,
    Boxplot,
    Candlestick,
    Heatmap,
    Map,
    Parallel,
    Lines,
    Graph,
    Sankey,
    Funnel,
    Gauge,
    PictorialBar,
    ThemeRiver,
    Custom
}


#[derive(Serialize,  Debug, Clone)]
#[serde(untagged)]
pub enum DataVariant<X:AxisKindMarker,Y:AxisKindMarker>{
    /// Single dimension data (string/int/float)
    Data(Vec<ValueSerializeWrapper<X>>),

    /// Two-dimensional data: [x, y]
    Pair(Vec<(ValueSerializeWrapper<X>,ValueSerializeWrapper<Y>)>),

    /// Single dimension data as an object with a name field
    Named(Vec<NamedValue<X>>),

    /// Two-dimensional data with the name: { value: [x, y], name: "str" }
    NamedPair(Vec<NamedValuePair<X,Y>>),
}

/// Internal enum to represent the data source for a series
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum SeriesDataSource<X:AxisKindMarker,Y:AxisKindMarker>{
    /// Direct data
    Data(DataVariant<X,Y>),
    /// Reference to a dataset by index
    DatasetIndex(usize)
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<(X, Y)>> for SeriesDataSource<X,Y>
{
    fn from(value: Vec<(X, Y)>) -> Self {
        SeriesDataSource::from_pairs(value)
    }
}

impl<const N: usize,X:AxisKindMarker,Y:AxisKindMarker> From<[(X, Y); N]> for SeriesDataSource<X,Y>
{
    fn from(value: [(X, Y); N]) -> Self {
        Self::Data(DataVariant::Pair(value.into_iter().map(|(x, y)| (x.into(), y.into())).collect()))
    }
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<(X, Y, String)>> for SeriesDataSource<X,Y>
{
    fn from(value: Vec<(X, Y, String)>) -> Self {
        SeriesDataSource::from_tuples_with_label(value)
    }
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<X>> for SeriesDataSource<X,Y>
{
    fn from(value: Vec<X>) -> Self {
        SeriesDataSource::from_values(value)
    }
}

impl<X:AxisKindMarker,Y:AxisKindMarker> SeriesDataSource<X,Y> {
    /// Creates a new SeriesDataSource that references a dataset by index
    pub fn from_dataset_index(index: usize) -> Self {
        Self::DatasetIndex(index)
    }

    /// Creates a SeriesDataSource with a simple array of values
    pub fn from_values(values: Vec<X>) -> Self {
        Self::Data(DataVariant::Data(values.into_iter().map(ValueSerializeWrapper::from).collect()))
    }

    /// Creates a SeriesDataSource with an array of [x,y] pairs
    pub fn from_pairs(pairs: Vec<(X,Y)>) -> Self {
        Self::Data(DataVariant::Pair(pairs.into_iter().map(|(x,y)| (x.into(),y.into())).collect()))
    }

   pub fn from_named_value_pairs(named_pairs: Vec<NamedValuePair<X,Y>>) -> Self{
       Self::Data(DataVariant::NamedPair(named_pairs))
   }


    /// Creates a SeriesDataSource with named values
    pub fn with_named_values(named_values: Vec<NamedValue<X>>) -> Self {
        Self::Data(DataVariant::Named(named_values))
    }

    pub fn from_labeled_values(values: Vec<(X, String)>) -> Self
    {
        let data_named_values = values.into_iter().map(|(x,label)| NamedValue{
            value: x.into(),
            name: label
        }).collect();
        Self::with_named_values(data_named_values)
    }

    /// Creates a SeriesDataSource with named pairs
    pub fn from_tuples_with_label(values: Vec<(X, Y, String)>) -> Self
    {
        let data_named_pairs = values.into_iter()
            .map(|(x, y,label) | NamedValuePair::new(x.into(), y.into(),label))
            .collect();
        Self::from_named_value_pairs(data_named_pairs)
    }

}


#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum DataPointSymbol {
    Circle,
    Rect,
    RoundRect,
    Triangle,
    Diamond,
    Pin,
    Arrow,
    None
}

/// Series definition
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Series<X:AxisKindMarker,Y:AxisKindMarker> {
    /// Chart type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<SeriesType>,

    /// Series name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub smooth: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub area_style: Option<AreaStyle>,

    /// Data array
    #[serde(flatten)]
    pub data: SeriesDataSource<X,Y>,

    /// if showSymbol is false symbol will still show on hover
    /// if the tooltip trigger is an axis
    #[serde(skip_serializing_if = "Option::is_none")]
    pub show_symbol: Option<bool>,

    ///if the show symbol is none, line won't react on hover anymore
    #[serde(skip_serializing_if = "Option::is_none")]
    pub symbol: Option<DataPointSymbol>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub symbol_size: Option<usize>,

    /// Additional raw series options
    #[serde(flatten)]
    pub extra: Option<Value>,
}

impl<X:AxisKindMarker,Y:AxisKindMarker> Series <X,Y>{
    pub fn new(name:String, r#type: SeriesType, data: SeriesDataSource<X,Y>) -> Series<X,Y> {
        Self{
            r#type: Some(r#type),
            name: Some(name),
            smooth: None,
            area_style: None,
            data,
            show_symbol: None,
            symbol: None,
            symbol_size: None,
            extra: None,
        }
    }
}


#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum AxisFillOrigin{
    Auto,
    Start,
    End,
    Number
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AreaStyle{
    #[serde(skip_serializing_if = "Option::is_none")]
    color: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    origin: Option<AxisFillOrigin>
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Transform{
    pub transform: Vec<DatasetTransform>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_dataset_index: Option<usize>
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Source<X:AxisKindMarker,Y:AxisKindMarker>{
    pub source: Vec<(ValueSerializeWrapper<X>,ValueSerializeWrapper<Y>)>,
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<(X,Y)>> for Source<X,Y>{
    fn from(value: Vec<(X,Y)>) -> Self {
        Self{
            source: value.into_iter().map(|(x,y)| (x.into(),y.into())).collect()
        }
    }
}

impl<const N:usize,X:AxisKindMarker,Y:AxisKindMarker> From<[(X,Y);N]> for Source<X,Y>
{
    fn from(value: [(X,Y);N]) -> Self {
        Self{
            source: value.into_iter().map(|(x,y)| (x.into(),y.into())).collect()
        }
    }
}


#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LabelledSource<X:AxisKindMarker,Y:AxisKindMarker>{
    pub source: Vec<(ValueSerializeWrapper<X>,ValueSerializeWrapper<Y>,String)>,
}





/// Dataset component for providing and transforming data
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum DatasetComponent<X:AxisKindMarker,Y:AxisKindMarker> {
    Source(Source<X,Y>),
    LabelledSource(LabelledSource<X,Y>),
    Transform(Transform)
}


impl<const N:usize,X:AxisKindMarker,Y:AxisKindMarker> From<[(X,Y);N]> for DatasetComponent<X,Y>
{
    fn from(value: [(X,Y);N]) -> Self {
        DatasetComponent::Source(value.into())
    }
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<(X,Y)>> for DatasetComponent<X,Y>
{
    fn from(value: Vec<(X, Y)>) -> Self {
        DatasetComponent::src(value)
    }
}


impl<const N:usize,X:AxisKindMarker,Y:AxisKindMarker>  From<[(X,Y,String);N]> for DatasetComponent<X,Y>
{
    fn from(value: [(X,Y,String);N] ) -> Self {
        Self::LabelledSource(
            LabelledSource{
                source: value.into_iter().map(|(x,y,label)| (x.into(),y.into(),label)).collect()
            }
        )
    }
}

impl<X:AxisKindMarker,Y:AxisKindMarker> From<Vec<(X,Y,String)>> for DatasetComponent<X,Y>
{
    fn from(value: Vec<(X, Y, String)>) -> Self {
        DatasetComponent::labelled_source(value)
    }
}


impl<X:AxisKindMarker,Y:AxisKindMarker> DatasetComponent<X,Y> {
    pub fn tr(transform: DatasetTransform, index: usize) -> Self{
        Self::Transform(Transform {
            transform: vec![transform],
            from_dataset_index: Some(index),
        })
    }

    pub fn trs(transform: Vec<DatasetTransform>, index: usize) -> Self{
        Self::Transform(
            Transform {
                transform,
                from_dataset_index: Some(index)
            }
        )
    }

    pub fn src(source: Vec<(X,Y)>) -> Self {
        Self::Source(source.into())
    }

    /// Constructor for the labeled source. Always put the label last
    pub fn labelled_source(source: Vec<(X,Y,String)>) -> Self {
        Self::LabelledSource(
            LabelledSource{
                source: source.into_iter().map(|(x,y,label)| (x.into(),y.into(),label)).collect()
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum DatasetTransformType {
    Filter,
    Sort,
    Boxplot,
    #[serde(rename = "ecStat:regression")]
    Regression,
    #[serde(rename = "ecStat:clustering")]
    Clustering
}

/// Transform applied to a dataset
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DatasetTransform {
    /// Transform type
    r#type: DatasetTransformType,

    /// Transform configuration
    config: DatasetTransformConfig,

}


impl DatasetTransform {

    pub fn regression(config: RegressionConfig) -> Self {
        Self{
            r#type: DatasetTransformType::Regression,
            config: DatasetTransformConfig::Regression(config)
        }
    }

    pub fn clustering(clustering_config: ClusteringConfig)->Self{
        Self{
            r#type: DatasetTransformType::Clustering,
            config: DatasetTransformConfig::Clustering(clustering_config)
        }
    }

    pub fn sort(sort_config: SortConfig) -> Self {
        Self{
            r#type: DatasetTransformType::Sort,
            config: DatasetTransformConfig::Sort(sort_config)
        }
    }

}


#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
enum DatasetTransformConfig {
    Regression(RegressionConfig),
    Clustering(ClusteringConfig),
    Sort(SortConfig)
}


/// regression methods supported by ecStat
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum RegressionMethod {
    Linear,
    Exponential,
    Logarithmic,
    Polynomial,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SortConfig {
    dimension: u8,
    order : Order
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Order {
    Asc,
    Desc
}


/// Configuration for regression transforms
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RegressionConfig {
    /// Regression method
    pub method: RegressionMethod,

    /// Polynomial order (only used when the method is Polynomial)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<u8>,

    /// Additional raw regression config options
    #[serde(flatten)]
    pub extra: Option<Value>,
}



/// Configuration for clustering transforms
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClusteringConfig {

    /// The number of clusters to generate (must be > 1).
    pub cluster_count: u8,

    ///dimension to which the cluster index will be written
    pub output_cluster_index_dimension: u8,

    /// dimensions of source data that will be used in calculation of a cluster
    pub dimensions : Option<Vec<usize>>,

}