svghmi/widget_xygraph.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 19 Oct 2022 20:53:01 +0200
branchwxPython4
changeset 3635 5107a2712b17
parent 3509 b5ca17732b1e
child 3691 9289fdda0222
permissions -rw-r--r--
Update README.md
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     1
// widget_xygraph.ysl2
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     2
widget_desc("XYGraph") {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     3
    longdesc
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     4
    ||
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     5
    XYGraph draws a cartesian trend graph re-using styles given for axis,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     6
    grid/marks, legends and curves.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     7
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     8
    Elements labeled "x_axis" and "y_axis" are svg:groups containg:
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
     9
     - "axis_label" svg:text gives style an alignment for axis labels.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    10
     - "interval_major_mark" and "interval_minor_mark" are svg elements to be
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    11
       duplicated along axis line to form intervals marks.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    12
     - "axis_line"  svg:path is the axis line. Paths must be intersect and their
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    13
       bounding box is the chart wall.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    14
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    15
    Elements labeled "curve_0", "curve_1", ... are paths whose styles are used
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    16
    to draw curves corresponding to data from variables passed as HMI tree paths.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    17
    "curve_0" is mandatory. HMI variables outnumbering given curves are ignored.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    18
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    19
    ||
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    20
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    21
    shortdesc > Cartesian trend graph showing values of given variables over time
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    22
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    23
    path name="value" count="1+" accepts="HMI_INT,HMI_REAL" > value
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    24
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    25
    arg name="xrange" accepts="int,time" > X axis range expressed either in samples or duration.
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    26
    arg name="xformat" count="optional" accepts="string" > format string for X label
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    27
    arg name="yformat" count="optional" accepts="string" > format string for Y label
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    28
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    29
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    30
widget_class("XYGraph") {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    31
    ||
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    32
        frequency = 1;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    33
        init() {
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    34
            let x_duration_s;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    35
            [x_duration_s,
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    36
             this.x_format, this.y_format] = this.args;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    37
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    38
            let timeunit = x_duration_s.slice(-1);
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    39
            let factor = {
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    40
                "s":1,
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    41
                "m":60,
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    42
                "h":3600,
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    43
                "d":86400}[timeunit];
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    44
            if(factor == undefined){
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    45
                this.max_data_length = Number(x_duration_s);
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    46
                this.x_duration = undefined;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    47
            }else{
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    48
                let duration = factor*Number(x_duration_s.slice(0,-1));
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    49
                this.max_data_length = undefined;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    50
                this.x_duration = duration*1000;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    51
            }
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    52
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
    53
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    54
            // Min and Max given with paths are meant to describe visible range,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    55
            // not to clip data.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    56
            this.clip = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    57
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    58
            let y_min = Infinity, y_max = -Infinity;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    59
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    60
            // Compute visible Y range by merging fixed curves Y ranges
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    61
            for(let minmax of this.minmaxes){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    62
               if(minmax){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    63
                   let [min,max] = minmax;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    64
                   if(min < y_min)
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    65
                       y_min = min;
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    66
                   if(max > y_max)
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    67
                       y_max = max;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    68
               }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    69
            }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    70
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    71
            if(y_min !== Infinity && y_max !== -Infinity){
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    72
               this.fixed_y_range = true;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    73
            } else {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    74
               this.fixed_y_range = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    75
            }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    76
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    77
            this.ymin = y_min;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    78
            this.ymax = y_max;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    79
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    80
            this.curves = [];
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    81
            this.init_specific();
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    82
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    83
            this.reference = new ReferenceFrame(
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    84
                [[this.x_interval_minor_mark_elt, this.x_interval_major_mark_elt],
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    85
                 [this.y_interval_minor_mark_elt, this.y_interval_major_mark_elt]],
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    86
                [this.x_axis_label_elt, this.y_axis_label_elt],
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
    87
                [this.x_axis_line_elt, this.y_axis_line_elt],
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    88
                [this.x_format, this.y_format]);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    89
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    90
            let max_stroke_width = 0;
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    91
            for(let curve of this.curves){
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    92
                if(curve.style.strokeWidth > max_stroke_width){
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    93
                    max_stroke_width = curve.style.strokeWidth;
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    94
                }
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    95
            }
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    96
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    97
            this.Margins=this.reference.getLengths().map(length => max_stroke_width/length);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
    98
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
    99
            // create <clipPath> path and attach it to widget
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   100
            let clipPath = document.createElementNS(xmlns,"clipPath");
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   101
            let clipPathPath = document.createElementNS(xmlns,"path");
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   102
            let clipPathPathDattr = document.createAttribute("d");
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   103
            clipPathPathDattr.value = this.reference.getClipPathPathDattr();
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   104
            clipPathPath.setAttributeNode(clipPathPathDattr);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   105
            clipPath.appendChild(clipPathPath);
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   106
            clipPath.id = randomId();
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   107
            this.element.appendChild(clipPath);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   108
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   109
            // assign created clipPath to clip-path property of curves
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   110
            for(let curve of this.curves){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   111
                curve.setAttribute("clip-path", "url(#" + clipPath.id + ")");
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   112
            }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   113
3509
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   114
            this.curves_data = [];
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   115
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   116
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   117
        dispatch(value,oldval, index) {
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   118
            // TODO: get PLC time instead of browser time
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   119
            let time = Date.now();
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   120
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   121
            // naive local buffer impl. 
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   122
            // data is updated only when graph is visible
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   123
            // TODO: replace with separate recording
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   124
3509
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   125
            if(this.curves_data[index] === undefined){
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   126
                this.curves_data[index] = [];
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   127
            }
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   128
            this.curves_data[index].push([time, value]);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   129
            let data_length = this.curves_data[index].length;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   130
            let ymin_damaged = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   131
            let ymax_damaged = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   132
            let overflow;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   133
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   134
            if(this.max_data_length == undefined){
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   135
                let peremption = time - this.x_duration;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   136
                let oldest = this.curves_data[index][0][0]
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   137
                this.xmin = peremption;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   138
                if(oldest < peremption){
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   139
                    // remove first item
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   140
                    overflow = this.curves_data[index].shift()[1];
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   141
                    data_length = data_length - 1;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   142
                }
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   143
            } else {
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   144
                if(data_length > this.max_data_length){
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   145
                    // remove first item
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   146
                    [this.xmin, overflow] = this.curves_data[index].shift();
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   147
                    data_length = data_length - 1;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   148
                } else {
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   149
                    if(this.xmin == undefined){
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   150
                        this.xmin = time;
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   151
                    }
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   152
                }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   153
            }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   154
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   155
            this.xmax = time;
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   156
            let Xrange = this.xmax - this.xmin;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   157
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   158
            if(!this.fixed_y_range){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   159
                ymin_damaged = overflow <= this.ymin;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   160
                ymax_damaged = overflow >= this.ymax;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   161
                if(value > this.ymax){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   162
                    ymax_damaged = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   163
                    this.ymax = value;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   164
                }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   165
                if(value < this.ymin){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   166
                    ymin_damaged = false;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   167
                    this.ymin = value;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   168
                }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   169
            }
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   170
            let Yrange = this.ymax - this.ymin;
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   171
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   172
            // apply margin by moving min and max to enlarge range
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   173
            let [xMargin,yMargin] = zip(this.Margins, [Xrange, Yrange]).map(([m,l]) => m*l);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   174
            [[this.dxmin, this.dxmax],[this.dymin,this.dymax]] =
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   175
                [[this.xmin-xMargin, this.xmax+xMargin],
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   176
                 [this.ymin-yMargin, this.ymax+yMargin]];
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   177
            Xrange += 2*xMargin;
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   178
            Yrange += 2*yMargin;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   179
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   180
            // recompute curves "d" attribute
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   181
            // FIXME: use SVG getPathData and setPathData when available.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   182
            //        https://svgwg.org/specs/paths/#InterfaceSVGPathData
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   183
            //        https://github.com/jarek-foksa/path-data-polyfill
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   184
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   185
            let [base_point, xvect, yvect] = this.reference.getBaseRef();
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   186
            this.curves_d_attr =
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   187
                zip(this.curves_data, this.curves).map(([data,curve]) => {
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   188
                    let new_d = data.map(([x,y], i) => {
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   189
                        // compute curve point from data, ranges, and base_ref
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   190
                        let xv = vectorscale(xvect, (x - this.dxmin) / Xrange);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   191
                        let yv = vectorscale(yvect, (y - this.dymin) / Yrange);
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   192
                        let px = base_point.x + xv.x + yv.x;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   193
                        let py = base_point.y + xv.y + yv.y;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   194
                        if(!this.fixed_y_range){
3505
a27b5862e363 SVGHMI: Add support for fixed X range (duration) on XY garph.
Edouard Tisserant
parents: 3490
diff changeset
   195
                            // update min and max from curve data if needed
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   196
                            if(ymin_damaged && y < this.ymin) this.ymin = y;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   197
                            if(ymax_damaged && y > this.ymax) this.ymax = y;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   198
                        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   199
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   200
                        return " " + px + "," + py;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   201
                    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   202
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   203
                    new_d.unshift("M ");
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   204
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   205
                    return new_d.join('');
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   206
                });
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   207
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   208
            // computed curves "d" attr is applied to svg curve during animate();
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   209
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   210
            this.request_animate();
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   211
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   212
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   213
        animate(){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   214
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   215
            // move elements only if enough data
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   216
            if(this.curves_data.some(data => data.length > 1)){
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   217
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   218
                // move marks and update labels
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   219
                this.reference.applyRanges([[this.dxmin, this.dxmax],
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   220
                                            [this.dymin, this.dymax]]);
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   221
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   222
                // apply computed curves "d" attributes
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   223
                for(let [curve, d_attr] of zip(this.curves, this.curves_d_attr)){
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   224
                    curve.setAttribute("d", d_attr);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   225
                }
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   226
            }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   227
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   228
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   229
    ||
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   230
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   231
3509
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   232
def "func:check_curves_label_consistency" {
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   233
    param "curve_elts";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   234
    param "number_to_check";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   235
    const "res" choose {
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   236
        when "$curve_elts[@inkscape:label = concat('curve_', string($number_to_check))]"{
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   237
            if "$number_to_check > 0"{
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   238
                value "func:check_curves_label_consistency($curve_elts, $number_to_check - 1)";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   239
            }
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   240
        }
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   241
        otherwise {
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   242
            value "concat('missing curve_', string($number_to_check))";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   243
        }
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   244
    }
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   245
    result "$res";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   246
}
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   247
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   248
widget_defs("XYGraph") {
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   249
    labels("/x_interval_minor_mark /x_axis_line /x_interval_major_mark /x_axis_label");
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   250
    labels("/y_interval_minor_mark /y_axis_line /y_interval_major_mark /y_axis_label");
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   251
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   252
    |     init_specific() {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   253
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   254
    // collect all curve_n labelled children
3509
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   255
    const "curves","$hmi_element/*[regexp:test(@inkscape:label,'^curve_[0-9]+$')]";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   256
    const "curves_error", "func:check_curves_label_consistency($curves,count($curves)-1)";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   257
    if "string-length($curves_error)"
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   258
        error > XYGraph id="«@id»", label="«@inkscape:label»" : «$curves_error»
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   259
    foreach "$curves" {
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   260
        const "label","@inkscape:label";
3509
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   261
        const "_id","@id";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   262
        const "curve_num", "substring(@inkscape:label, 7)";
b5ca17732b1e SVGHMI: XY graph build produces error message if provided curve_N are not consistent.
Edouard Tisserant
parents: 3505
diff changeset
   263
    |         this.curves[«$curve_num»] = id("«@id»"); /* «@inkscape:label» */
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   264
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   265
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   266
    |     }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   267
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   268
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   269
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   270
emit "declarations:XYGraph"
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   271
||
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   272
function lineFromPath(path_elt) {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   273
    let start = path_elt.getPointAtLength(0);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   274
    let end = path_elt.getPointAtLength(path_elt.getTotalLength());
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   275
    return [start, new DOMPoint(end.x - start.x , end.y - start.y)];
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   276
};
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   277
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   278
function vector(p1, p2) {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   279
    return new DOMPoint(p2.x - p1.x , p2.y - p1.y);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   280
};
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   281
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   282
function vectorscale(p1, p2) {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   283
    return new DOMPoint(p2 * p1.x , p2 * p1.y);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   284
};
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   285
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   286
function vectorLength(p1) {
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   287
    return Math.sqrt(p1.x*p1.x + p1.y*p1.y);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   288
};
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   289
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   290
function randomId(){
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   291
    return Date.now().toString(36) + Math.random().toString(36).substr(2);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   292
}
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   293
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   294
function move_elements_to_group(elements) {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   295
    let newgroup = document.createElementNS(xmlns,"g");
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   296
    newgroup.id = randomId();
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   297
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   298
    for(let element of elements){
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   299
        let parent = element.parentElement;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   300
        if(parent !== null)
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   301
            parent.removeChild(element);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   302
        newgroup.appendChild(element);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   303
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   304
    return newgroup;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   305
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   306
function getLinesIntesection(l1, l2) {
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   307
    let [l1start, l1vect] = l1;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   308
    let [l2start, l2vect] = l2;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   309
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   310
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   311
    /*
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   312
    Compute intersection of two lines
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   313
    =================================
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   314
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   315
                          ^ l2vect
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   316
                         /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   317
                        /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   318
                       /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   319
    l1start ----------X--------------> l1vect
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   320
                     / intersection
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   321
                    /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   322
                   /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   323
                   l2start
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   324
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   325
	*/
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   326
    let [x1, y1, x3, y3] = [l1start.x, l1start.y, l2start.x, l2start.y];
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   327
	let [x2, y2, x4, y4] = [x1+l1vect.x, y1+l1vect.y, x3+l2vect.x, y3+l2vect.y];
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   328
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   329
	// line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   330
	// Determine the intersection point of two line segments
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   331
	// Return FALSE if the lines don't intersect
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   332
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   333
    // Check if none of the lines are of length 0
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   334
    if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) {
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   335
        return false
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   336
    }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   337
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   338
    denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   339
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   340
    // Lines are parallel
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   341
    if (denominator === 0) {
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   342
        return false
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   343
    }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   344
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   345
    let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   346
    let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   347
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   348
    // Return a object with the x and y coordinates of the intersection
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   349
    let x = x1 + ua * (x2 - x1)
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   350
    let y = y1 + ua * (y2 - y1)
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   351
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   352
    return new DOMPoint(x,y);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   353
};
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   354
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   355
class ReferenceFrame {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   356
    constructor(
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   357
        // [[Xminor,Xmajor], [Yminor,Ymajor]]
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   358
        marks,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   359
        // [Xlabel, Ylabel]
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   360
        labels,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   361
        // [Xline, Yline]
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   362
        lines,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   363
        // [Xformat, Yformat] printf-like formating strings
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   364
        formats
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   365
    ){
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   366
        this.axes = zip(labels,marks,lines,formats).map(args => new Axis(...args));
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   367
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   368
        let [lx,ly] = this.axes.map(axis => axis.line);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   369
        let [[xstart, xvect], [ystart, yvect]] = [lx,ly];
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   370
        let base_point = this.getBasePoint();
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   371
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   372
        // setup clipping for curves
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   373
        this.clipPathPathDattr =
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   374
            "m " + base_point.x + "," + base_point.y + " "
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   375
                 + xvect.x + "," + xvect.y + " "
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   376
                 + yvect.x + "," + yvect.y + " "
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   377
                 + -xvect.x + "," + -xvect.y + " "
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   378
                 + -yvect.x + "," + -yvect.y + " z";
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   379
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   380
        this.base_ref = [base_point, xvect, yvect];
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   381
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   382
        this.lengths = [xvect,yvect].map(v => vectorLength(v));
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   383
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   384
        for(let axis of this.axes){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   385
            axis.setBasePoint(base_point);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   386
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   387
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   388
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   389
    getLengths(){
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   390
        return this.lengths;
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   391
    }
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   392
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   393
	getBaseRef(){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   394
        return this.base_ref;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   395
	}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   396
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   397
    getClipPathPathDattr(){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   398
        return this.clipPathPathDattr;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   399
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   400
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   401
    applyRanges(ranges){
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   402
        let origin_moves = zip(ranges,this.axes).map(([range,axis]) => axis.applyRange(...range));
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   403
		zip(origin_moves.reverse(),this.axes).forEach(([vect,axis]) => axis.moveOrigin(vect));
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   404
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   405
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   406
    getBasePoint() {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   407
        let [[xstart, xvect], [ystart, yvect]] = this.axes.map(axis => axis.line);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   408
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   409
        /*
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   410
        Compute graph clipping region base point
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   411
        ========================================
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   412
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   413
        Clipping region is a parallelogram containing axes lines,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   414
        and whose sides are parallel to axes line respectively.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   415
        Given axes lines are not starting at the same point, hereafter is
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   416
        calculus of parallelogram base point.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   417
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   418
                              ^ given Y axis (yvect)
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   419
                   /         /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   420
                  /         /
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   421
                 /         /
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   422
         xstart *---------*--------------> given X axis (xvect)
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   423
               /         /origin
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   424
              /         /
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   425
             *---------*--------------
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   426
        base_point   ystart
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   427
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   428
        */
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   429
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   430
        let base_point = getLinesIntesection([xstart,yvect],[ystart,xvect]);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   431
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   432
        return base_point;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   433
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   434
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   435
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   436
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   437
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   438
class Axis {
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   439
    constructor(label, marks, line, format){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   440
        this.lineElement = line;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   441
        this.line = lineFromPath(line);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   442
        this.format = format;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   443
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   444
        this.label = label;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   445
        this.marks = marks;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   446
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   447
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   448
        // add transforms for elements sliding along the axis line
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   449
        for(let [elementname,element] of zip(["minor", "major", "label"],[...marks,label])){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   450
            for(let name of ["base","slide"]){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   451
                let transform = svg_root.createSVGTransform();
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   452
                element.transform.baseVal.insertItemBefore(transform,0);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   453
                this[elementname+"_"+name+"_transform"]=transform;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   454
            };
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   455
        };
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   456
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   457
        // group marks an labels together
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   458
        let parent = line.parentElement;
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   459
        this.marks_group = move_elements_to_group(marks);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   460
        this.marks_and_label_group = move_elements_to_group([this.marks_group, label]);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   461
        this.group = move_elements_to_group([this.marks_and_label_group,line]);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   462
        parent.appendChild(this.group);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   463
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   464
        // Add transforms to group
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   465
        for(let name of ["base","origin"]){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   466
            let transform = svg_root.createSVGTransform();
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   467
            this.group.transform.baseVal.appendItem(transform);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   468
            this[name+"_transform"]=transform;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   469
        };
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   470
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   471
        this.marks_and_label_group_transform = svg_root.createSVGTransform();
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   472
        this.marks_and_label_group.transform.baseVal.appendItem(this.marks_and_label_group_transform);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   473
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   474
        this.duplicates = [];
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   475
        this.last_duplicate_index = 0;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   476
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   477
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   478
    setBasePoint(base_point){
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   479
        // move Axis to base point
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   480
        let [start, _vect] = this.line;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   481
        let v = vector(start, base_point);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   482
        this.base_transform.setTranslate(v.x, v.y);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   483
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   484
        // Move marks and label to base point.
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   485
        // _|_______          _|________
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   486
        //  |  '  |     ==>    '
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   487
        //  |     0            0
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   488
        //  |                  |
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   489
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   490
        for(let [markname,mark] of zip(["minor", "major"],this.marks)){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   491
            let pos = vector(
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   492
                // Marks are expected to be paths
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   493
                // paths are expected to be lines
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   494
                // intersection with axis line is taken 
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   495
                // as reference for mark position
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   496
                getLinesIntesection(
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   497
                    this.line, lineFromPath(mark)),base_point);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   498
            this[markname+"_base_transform"].setTranslate(pos.x - v.x, pos.y - v.y);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   499
            if(markname == "major"){ // label follow major mark
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   500
                this.label_base_transform.setTranslate(pos.x - v.x, pos.y - v.y);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   501
            }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   502
        }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   503
    }
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   504
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   505
	moveOrigin(vect){
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   506
		this.origin_transform.setTranslate(vect.x, vect.y);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   507
	}
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   508
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   509
    applyRange(min, max){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   510
        let range = max - min;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   511
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   512
        // compute how many units for a mark
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   513
        //
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   514
        // - Units are expected to be an order of magnitude smaller than range,
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   515
        //   so that marks are not too dense and also not too sparse.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   516
        //   Order of magnitude of range is log10(range)
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   517
        //
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   518
        // - Units are necessarily power of ten, otherwise it is complicated to
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   519
        //   fill the text in labels...
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   520
        //   Unit is pow(10, integer_number )
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   521
        //
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   522
        // - To transform order of magnitude to an integer, floor() is used.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   523
        //   This results in a count of mark fluctuating in between 10 and 100.
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   524
        //
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   525
        // - To spare resources result is better in between 3 and 30,
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   526
        //   and log10(3) is substracted to order of magnitude to obtain this
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   527
        let unit = Math.pow(10, Math.floor(Math.log10(range)-Math.log10(3)));
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   528
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   529
        // TODO: for time values (ms), units may be :
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   530
        //       1       -> ms
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   531
        //       10      -> s/100
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   532
        //       100     -> s/10
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   533
        //       1000    -> s
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   534
        //       60000   -> min
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   535
        //       3600000 -> hour
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   536
        //       ...
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   537
        //
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   538
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   539
        // Compute position of origin along axis [0...range]
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   540
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   541
        // min < 0, max > 0, offset = -min
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   542
        // _____________|________________
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   543
        // ... -3 -2 -1 |0  1  2  3  4 ...
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   544
        // <--offset---> ^
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   545
        //               |_original
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   546
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   547
        // min > 0, max > 0, offset = 0
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   548
        // |________________
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   549
        // |6  7  8  9  10...
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   550
        //  ^
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   551
        //  |_original
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   552
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   553
        // min < 0, max < 0, offset = max-min (range)
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   554
        // _____________|_
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   555
        // ... -5 -4 -3 |-2
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   556
        // <--offset---> ^
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   557
        //               |_original
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   558
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   559
        let offset = (max>=0 && min>=0) ? 0 : (
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   560
                     (max<0 && min<0)   ? range : -min);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   561
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   562
        // compute unit vector
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   563
        let [_start, vect] = this.line;
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   564
        let unit_vect = vectorscale(vect, 1/range);
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   565
        let [mark_min, mark_max, mark_offset] = [min,max,offset].map(val => Math.round(val/unit));
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   566
        let mark_count = mark_max-mark_min;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   567
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   568
        // apply unit vector to marks and label
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   569
        // offset is a representing position of an 
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   570
        // axis along the opposit axis line, expressed in major marks units
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   571
        // unit_vect is unit vector
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   572
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   573
        //              ^
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   574
        //              | unit_vect
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   575
        //              |<--->
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   576
        //     _________|__________>
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   577
        //     ^  |  '  |  '  |  '
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   578
        //     |yoffset |     1 
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   579
        //     |        |
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   580
        //     v xoffset|
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   581
        //     X<------>|
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   582
        // base_point
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   583
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   584
        // move major marks and label to first positive mark position
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   585
        // let v = vectorscale(unit_vect, unit);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   586
        // this.label_slide_transform.setTranslate(v.x, v.y);
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   587
        // this.major_slide_transform.setTranslate(v.x, v.y);
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   588
        // move minor mark to first half positive mark position
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   589
        let v = vectorscale(unit_vect, unit/2);
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   590
        this.minor_slide_transform.setTranslate(v.x, v.y);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   591
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   592
        // duplicate marks and labels as needed
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   593
        let current_mark_count = this.duplicates.length;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   594
        for(let i = current_mark_count; i <= mark_count; i++){
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   595
            // cloneNode() label and add a svg:use of marks in a new group
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   596
            let newgroup = document.createElementNS(xmlns,"g");
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   597
            let transform = svg_root.createSVGTransform();
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   598
            let newlabel = this.label.cloneNode(true);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   599
            let newuse = document.createElementNS(xmlns,"use");
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   600
            let newuseAttr = document.createAttribute("href");
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   601
            newuseAttr.value = "#"+this.marks_group.id;
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   602
            newuse.setAttributeNode(newuseAttr);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   603
            newgroup.transform.baseVal.appendItem(transform);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   604
            newgroup.appendChild(newlabel);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   605
            newgroup.appendChild(newuse);
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   606
            this.duplicates.push([transform,newgroup]);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   607
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   608
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   609
        // move marks and labels, set labels
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   610
        // 
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   611
        // min > 0, max > 0, offset = 0
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   612
        //         ^
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   613
        //         |________>
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   614
        //        '| |  '  |
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   615
        //         | 6     7
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   616
        //         X
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   617
        //     base_point
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   618
        //
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   619
        // min < 0, max > 0, offset = -min
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   620
        //              ^
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   621
        //     _________|__________>
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   622
        //     '  |  '  |  '  |  '
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   623
        //       -1     |     1 
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   624
        //       offset |
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   625
        //     X<------>|
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   626
        // base_point
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   627
        //
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   628
        // min < 0, max < 0, offset = range
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   629
        //                 ^
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   630
        //     ____________|    
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   631
        //      '  |  '  | |'
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   632
        //        -5    -4 |
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   633
        //         offset  |
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   634
        //     X<--------->|
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   635
        // base_point
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   636
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   637
        let duplicate_index = 0;
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   638
        for(let mark_index = 0; mark_index <= mark_count; mark_index++){
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   639
            let val = (mark_min + mark_index) * unit;
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   640
            let vec = vectorscale(unit_vect, val - min);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   641
            let text = this.format ? sprintf(this.format, val) : val.toString();
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   642
            if(mark_index == mark_offset){
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   643
                // apply offset to original marks and label groups
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   644
                this.marks_and_label_group_transform.setTranslate(vec.x, vec.y);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   645
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   646
                // update original label text
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   647
                this.label.getElementsByTagName("tspan")[0].textContent = text;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   648
            } else {
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   649
                let [transform,element] = this.duplicates[duplicate_index++];
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   650
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   651
                // apply unit vector*N to marks and label groups
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents: 3474
diff changeset
   652
                transform.setTranslate(vec.x, vec.y);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   653
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   654
                // update label text
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   655
                element.getElementsByTagName("tspan")[0].textContent = text;
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   656
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   657
                // Attach to group if not already
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   658
                if(element.parentElement == null){
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   659
                    this.group.appendChild(element);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   660
                }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   661
            }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   662
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   663
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   664
        let save_duplicate_index = duplicate_index;
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   665
        // dettach marks and label from group if not anymore visible
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   666
        for(;duplicate_index < this.last_duplicate_index; duplicate_index++){
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   667
            let [transform,element] = this.duplicates[duplicate_index];
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   668
            this.group.removeChild(element);
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   669
        }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   670
3490
4f252e8d6759 SVGHMI: more fixes for XY graph + add XY graph test.
Edouard Tisserant
parents: 3488
diff changeset
   671
        this.last_duplicate_index = save_duplicate_index;
3488
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   672
6ef4ffcf9761 SVGHMI: multiple fixes in XY graph widget. WIP.
Edouard Tisserant
parents: 3484
diff changeset
   673
		return vectorscale(unit_vect, offset);
3470
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   674
    }
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   675
}
b36754171535 SVGHMI: Add premature implementation of XY chart.
Edouard Tisserant
parents:
diff changeset
   676
||