summaryrefslogtreecommitdiff
path: root/blocks/eda-frontend/src/utils/Sigbuilder-graph.js
blob: 63b6162a19e218e530ad9d793356720ae52cbe0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import Highcharts from 'highcharts'
import mxGraphFactory from 'mxgraph'

import { getmethod, showModalWindow } from './dependencies'

const {
  mxEvent
} = new mxGraphFactory()

export const graphSigbuilder = ''
export let sigbuilderGraph = ''

let wind = ''

// Function to create a chart with responsive points for Sigbuilder
function createDraggablePointsChartSigbuilder (graphParameters, pointsHistory, xmin, xmax, ymin, ymax, chartType, points, method, xmaxtitle, step, stepname) {
  graphParameters.mtd = method
  const subtitle = updateSubtitleForSigbuilderGraph(points, graphParameters.mtd, xmaxtitle, graphParameters.PeriodicOption)
  pointsHistory.push(graphParameters.graphPoints.slice())

  sigbuilderGraph = Highcharts.chart('drag_sig_chart', {
    chart: {
      type: chartType,
      animation: false,
      events: {
        click: function (e) {
          const xValue = e.xAxis[0].value
          const yValue = e.yAxis[0].value
          addPointsOnChart(graphParameters, pointsHistory, xValue, yValue)
        }
      }
    },
    tooltip: {
      enabled: false
    },
    title: {
      text: ''
    },
    subtitle: {
      text: subtitle
    },

    yAxis: {
      title: {
        text: 'Output'
      },
      min: parseFloat(ymin),
      max: parseFloat(ymax),
      gridLineWidth: 1,
      gridLineDashStyle: 'dash'
    },

    xAxis: {
      title: {
        text: 'time'
      },
      min: parseFloat(xmin),
      max: parseFloat(xmax),
      gridLineWidth: 1,
      gridLineDashStyle: 'dash'
    },

    plotOptions: {
      series: {
        point: {
          events: {
            drag: function (e) {
              if (e.y >= ymax) {
                this.y = ymax
                return false
              }
              if (e.y <= ymin) {
                this.y = ymin
                return false
              }
              if (e.x >= xmax) {
                this.x = xmax
                return false
              }
              if (e.x <= xmin) {
                this.x = xmin
                return false
              }
            },
            drop: function () {
              pointsHistory.push(graphParameters.graphPoints.slice())
            },
            dblclick: function (e) {
              const graphObject = e
              editPointsValue(graphObject, graphParameters, pointsHistory)
            },
            contextmenu: function (e) {
              const graphObject = e
              removePointsFromChart(graphObject, graphParameters, pointsHistory)
            }
          },
          stickyTracking: false
        },
        column: {
          stacking: 'normal'
        },
        marker: {
          enabled: true,
          symbol: 'url(../images/plus-icon.png)'
        }
      }
    },
    series: [{
      draggableX: true,
      draggableY: true,
      showInLegend: false,
      data: graphParameters.graphPoints,
      step,
      name: stepname
    }]
  })
}

export function updateSubtitleForSigbuilderGraph (points, method, xmaxtitle, periodicFlag) {
  let subTitle = ''
  if (periodicFlag === 'y') {
    subTitle = '<b>' + points + ' points, Method: ' + getmethod(method) + ', periodic, T = ' + xmaxtitle + '</b>'
  } else {
    subTitle = '<b>' + points + ' points, Method: ' + getmethod(method) + ', aperiodic</b>'
  }
  return subTitle
}

function autoscaleFunctionalityForGraph (graphParameters, pointsHistory) {
  // Added for postive/maximum value autoscale functionality
  const maxXValueNew = sigbuilderGraph.xAxis[0].getExtremes().dataMax // get max x point's value
  const minXValueNew = sigbuilderGraph.xAxis[0].getExtremes().dataMin // get min x point's value
  const maxYValueNew = sigbuilderGraph.yAxis[0].getExtremes().dataMax // get max y point's value
  let minYValueNew = sigbuilderGraph.yAxis[0].getExtremes().dataMin // get min y point's value
  const diffX = ((Math.abs(maxXValueNew - minXValueNew)) / 100) * 10
  const diffY = ((Math.abs(maxYValueNew - minYValueNew)) / 100) * 10
  graphParameters.xmin = 0 // set min x axis value
  graphParameters.xmax = maxXValueNew + diffX // set max x axis value
  if (minYValueNew > 0) {
    minYValueNew = 0
  }
  graphParameters.ymin = minYValueNew - diffY // set min y axis value
  graphParameters.ymax = maxYValueNew + diffY // set max y axis value
  graphParameters.points = graphParameters.graphPoints.length
  createDraggablePointsChartSigbuilder(graphParameters, pointsHistory, graphParameters.xmin, graphParameters.xmax, graphParameters.ymin, graphParameters.ymax, graphParameters.chartType, graphParameters.points, graphParameters.mtd, graphParameters.xmaxTitle, graphParameters.step, graphParameters.stepname)
}

export function editPointsValue (graphObject, graphParameters, pointsHistory) {
  document.getElementById('messageLabel').innerHTML = ''
  // Making graph window inaccessible
  const graphWind = document.getElementById('graphcontentwind')
  graphWind.style.pointerEvents = 'none'
  // Create basic structure for the form
  const content = document.createElement('div')
  content.setAttribute('id', 'editCoordinates')

  // Add Form
  const myform = document.createElement('form')
  myform.method = 'post'
  myform.id = 'formEditCoordinate'
  myform.style.padding = '10px'

  const titlelabel = document.createElement('span')
  titlelabel.innerHTML = 'Enter new x and y'
  myform.appendChild(titlelabel)
  // Line break
  const linebreak = document.createElement('br')
  myform.appendChild(linebreak)
  myform.appendChild(linebreak)

  const keys = Object.keys(graphObject.point.options)
  const len = keys.length
  for (let i = 0; i < len; i++) {
    // Input Title
    const namelabel = document.createElement('label')
    namelabel.innerHTML = keys[i].toString()
    namelabel.style.marginLeft = '30px'
    myform.appendChild(namelabel)

    let value = 0
    if (((graphObject.point.options[keys[i]]).toString()).includes('.')) {
      value = (graphObject.point.options[keys[i]]).toFixed(6)
    } else {
      value = (graphObject.point.options[keys[i]])
    }
    // Input
    const input = document.createElement('input')
    input.name = 'edit_' + keys[i]
    input.value = value
    input.setAttribute('id', 'edit_' + keys[i])
    input.setAttribute('class', 'fieldInput')
    myform.appendChild(input)

    myform.appendChild(linebreak)
    myform.appendChild(linebreak)
  }

  myform.appendChild(linebreak)

  // Button - Cancel
  const cancelBtn = document.createElement('button')
  cancelBtn.style.cssFloat = 'right'
  cancelBtn.innerHTML = 'Cancel'
  cancelBtn.type = 'button'
  cancelBtn.name = 'Cancel'
  myform.appendChild(cancelBtn)

  // Button - OK
  const okBtn = document.createElement('button')
  okBtn.style.cssFloat = 'right'
  okBtn.style.marginRight = '20px'
  okBtn.innerHTML = 'OK'
  okBtn.type = 'button'
  okBtn.name = 'OK'

  myform.appendChild(okBtn)
  content.appendChild(myform)
  const height = 150
  wind = showModalWindow(graphSigbuilder, 'Scilab Multiple Values Request', content, 200, height)
  wind.addListener(mxEvent.DESTROY, function () {
    graphWind.style.pointerEvents = 'auto'
  })
  // Executes when button 'cancelBtn' is clicked
  cancelBtn.onclick = function () {
    document.getElementById('messageLabel').innerHTML = ''
    graphWind.style.pointerEvents = 'auto'
    wind.destroy()
  }
  // Executes when button 'okBtn' is clicked
  okBtn.onclick = function () {
    let xValue = parseFloat(document.getElementById('edit_x').value)
    if (xValue < 0) {
      xValue = 0
    }
    const yValue = parseFloat(document.getElementById('edit_y').value)
    const points = graphParameters.graphPoints
    const xArry = []
    for (let i = 0; i < points.length; i++) {
      xArry[i] = points[i][0]
    }
    xArry[points.length] = xValue
    const result = checkDuplicateXValues(xArry)
    const mtdCheck = [0, 1, 2].includes(graphParameters.mtd)
    if (result) {
      removePointsFromChart(graphObject, graphParameters, pointsHistory)
      addPointsOnChart(graphParameters, pointsHistory, xValue, yValue)
      autoscaleFunctionalityForGraph(graphParameters, pointsHistory)
      document.getElementById('messageLabel').innerHTML = ''
      graphWind.style.pointerEvents = 'auto'
      wind.destroy()
    } else {
      if (mtdCheck) {
        removePointsFromChart(graphObject, graphParameters, pointsHistory)
        addPointsOnChart(graphParameters, pointsHistory, xValue, yValue)
        autoscaleFunctionalityForGraph(graphParameters, pointsHistory)
        document.getElementById('messageLabel').innerHTML = ''
        graphWind.style.pointerEvents = 'auto'
        wind.destroy()
      } else {
        document.getElementById('messageLabel').innerHTML = 'ERROR IN SPLINE : ' + getmethod(graphParameters.mtd)
        wind.destroy()
        graphWind.style.pointerEvents = 'auto'
        throw new Error('incorrect')
      }
    }
  }
}

export function removePointsFromChart (graphObject, graphParameters, pointsHistory) {
  const counter = graphObject.point.index
  sigbuilderGraph.series[0].data[counter].remove()
  pointsHistory.push(graphParameters.graphPoints.slice())
  graphParameters.points = sigbuilderGraph.series[0].data.length
  graphParameters.xmaxTitle = sigbuilderGraph.xAxis[0].getExtremes().dataMax.toFixed(6)
  sigbuilderGraph.setTitle(null, { text: updateSubtitleForSigbuilderGraph(graphParameters.points, graphParameters.mtd, graphParameters.xmaxTitle, graphParameters.PeriodicOption) })
}

export function addPointsOnChart (graphParameters, pointsHistory, xValue, yValue) {
  document.getElementById('messageLabel').innerHTML = ''
  if (xValue === 0 && yValue === 0) {
    graphParameters.flag_for_zeros = true
  }
  const points = graphParameters.graphPoints
  const xArry = []
  for (let i = 0; i < points.length; i++) {
    xArry[i] = points[i][0]
  }
  xArry[points.length] = xValue
  const result = checkDuplicateXValues(xArry)
  const mtdCheck = [0, 1, 2].includes(graphParameters.mtd)
  if (result) {
    sigbuilderGraph.series[0].addPoint([xValue, yValue])
    pointsHistory.push(graphParameters.graphPoints.slice())
    graphParameters.points = sigbuilderGraph.series[0].data.length
    graphParameters.xmaxTitle = sigbuilderGraph.xAxis[0].getExtremes().dataMax.toFixed(6)
    sigbuilderGraph.setTitle(null, { text: updateSubtitleForSigbuilderGraph(graphParameters.points, graphParameters.mtd, graphParameters.xmaxTitle, graphParameters.PeriodicOption) })
  } else {
    if (mtdCheck) {
      sigbuilderGraph.series[0].addPoint([xValue, yValue])
      pointsHistory.push(graphParameters.graphPoints.slice())
      graphParameters.points = sigbuilderGraph.series[0].data.length
      graphParameters.xmaxTitle = sigbuilderGraph.xAxis[0].getExtremes().dataMax.toFixed(6)
      sigbuilderGraph.setTitle(null, { text: updateSubtitleForSigbuilderGraph(graphParameters.points, graphParameters.mtd, graphParameters.xmaxTitle, graphParameters.PeriodicOption) })
    } else {
      document.getElementById('messageLabel').innerHTML = 'ERROR IN SPLINE : ' + getmethod(graphParameters.mtd)
      throw new Error('incorrect')
    }
  }
}

function checkDuplicateXValues (xxArry) {
  const arrayForCompare = []
  const result = xxArry.slice(0).every(function (item, index, array) {
    if (arrayForCompare.indexOf(item) > -1) {
      array.length = 0
      return false
    } else {
      arrayForCompare.push(item)
      return true
    }
  })
  return result
}