adaptive-linechart 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/linechart.js CHANGED
@@ -1,19 +1,27 @@
1
- // Version 1.2.2
2
- // The oject represent line-chart. Constructor create HTML code for the chart. The chart
3
- // uses external CSS styles .nowrap .linechart, .chart-title, .chart-live-data, .chart-legend,
4
- // .chart-legent-item, .chart-legent-color. The chart uses global variable isMobil (Boolean).
5
- // If false, show live data by mouse over. By click on item in legend called external function
6
- // processSelectLegendItem(chartId, amount-of-lines, parId). Only positive values allowed.
7
- // Labels on axe-X and -Y will be created automaticaly. The chart can show maximal 10 lines
8
- // in different colors.
9
- // @consructor:
10
- // @param container - Reference to parent element for chart. If null use HTML BODY as parent element.
11
- // @param chartId - String. Identifier for chart. Optional. If not defined use random integer.
12
- // @param width - Integer. Width in pixel. Optional. Default use available width in parent element.
13
- // @param title - String. Title of chart. Optional. If null don't create title.
14
- // @param className - String. Class for div of chart. Optinal. Default "linechart"
1
+ // Comments https://jsdoc.app/
2
+
3
+ /**
4
+ * @version 1.3.0
5
+ * @author Andrej Koslov
6
+ */
7
+
8
+ /**
9
+ * The oject represent the chart. Constructor create object and HTML code for the chart. The chart
10
+ * uses external CSS styles .nowrap .linechart, .chart-title, .chart-live-data, .chart-legend,
11
+ * .chart-legent-item, .chart-legent-color. The chart uses global variable isMobil (Boolean). If
12
+ * false, show live data by mouse over. The line create from LineData (see above) Labels on axis X
13
+ * and Y will be created automaticaly. The chart can show maximal 10 lines in different colors.
14
+ * @class
15
+ * @param reference to parent element for chart. If null use HTML BODY as parent element.
16
+ * @param chartId {number} identifier for chart. Optional. If not defined use random number.
17
+ * @param width {number} width in pixel. Optional. Default use available width in parent element.
18
+ * @param title {string} title of chart. Optional. If null don't create title.
19
+ * @param className {string} class for div of chart. Optional. Default "linechart"
20
+ * @param axisXType {string} for time based values on axis X set "time" (Default). For number based
21
+ * values "numer".
22
+ */
15
23
  class LineChart {
16
- constructor(container, chartId, width, title, className) {
24
+ constructor(container, chartId, width, title, className, axisXType) {
17
25
  if(container == null)
18
26
  container = document.getElementsByTagName("BODY")[0];
19
27
  if(chartId == null)
@@ -30,16 +38,14 @@ class LineChart {
30
38
  chart_id: chartId,
31
39
  style: 'float:left; width:' + width + 'px; padding-bottom:8px;'
32
40
  });
33
- // Create title if string for title not null.
41
+ // Create title for chart if parameter title is not null.
34
42
  if (title != null) {
35
- this.title = addNewElement(this.chartDiv, 'label', {
43
+ let titleElement = addNewElement(this.chartDiv, 'label', {
36
44
  class: 'chart-title nowrap',
37
- style: 'width:' + (width - 20) + 'px; text-align:center; display:block; margin-left:10px; margin-top:10px',
45
+ style: 'width:'+ (width - 20)+ 'px; text-align:center; display:block; margin-left:10px; margin-top:10px',
38
46
  });
39
- this.title.innerHTML = title;
40
- } else {
41
- this.title = null;
42
- }
47
+ titleElement.innerHTML = title;
48
+ }
43
49
  // Calculate real width of canvas based on width of chart-div. Think about margin
44
50
  // of canvas 30 = 17(margin-left) + 13(margin-right)
45
51
  var canvasWidth = width - 30;
@@ -65,25 +71,39 @@ class LineChart {
65
71
  }
66
72
  }
67
73
  // Add canvas and save reference to this one.
68
- this.canvas = addNewElement(this.chartDiv, 'canvas', {
74
+ this._canvas = addNewElement(this.chartDiv, 'canvas', {
69
75
  id: 'chart_' + chartId+ '_canvas',
70
76
  width: canvasWidth,
71
77
  height: canvasHeight,
72
78
  style: 'margin: 15px 17px 3px 13px;'
73
79
  });
80
+ // Set type of axis-X "number" or "time".
81
+ if(axisXType != null || (typeof axisXType) == "undefined" ) {
82
+ if(axisXType == "number") {
83
+ this.axisXType = "number";
84
+ } else if(axisXType == "time") {
85
+ this.axisXType = "time";
86
+ } else {
87
+ logger.warn(`Unknow value axisXType=${axisXType}. Set default "time".`)
88
+ this.axisXType = "time";
89
+ }
90
+ } else {
91
+ this.axisXType == "time";
92
+ }
74
93
  // Used to show live data by mousemove over the canvas.
75
- this.canvas.dataset.chartId = chartId.toString();
94
+ this._canvas.dataset.axisXType = this.axisXType;
95
+ this._canvas.dataset.chartId = chartId.toString();
76
96
  // Save reference to context.
77
- this.context = this.canvas.getContext('2d');
97
+ this._context = this._canvas.getContext('2d');
78
98
  // I don't know how it work, but this command disable/reduce antialiasing!
79
99
  // It works only if lines are 1 pixel! Need if canvas created or size changed
80
100
  // and before draw on canvas.
81
- this.context.translate(0.5, 0.5);
101
+ this._context.translate(0.5, 0.5);
82
102
  // Create element to show data by mousemove and set default '-'.
83
103
  if (!isMobil) {
84
104
  // Add events processing for canvas and show live data (value and data-time)
85
105
  // By leave the canvas remove data.
86
- this.canvas.addEventListener("mouseout", function (event) {
106
+ this._canvas.addEventListener("mouseout", function (event) {
87
107
  // Select element which show data.
88
108
  var element = document.getElementById('chart_' + this.dataset.chartId+'_livedata');
89
109
  if (element == null)
@@ -91,7 +111,7 @@ class LineChart {
91
111
  element.innerText = '-';
92
112
  });
93
113
  // By mousemove over canvas show value and date-time in special element.
94
- this.canvas.addEventListener('mousemove', function (event) {
114
+ this._canvas.addEventListener('mousemove', function (event) {
95
115
  // Select element which show data.
96
116
  var element = document.getElementById('chart_' + this.dataset.chartId+'_livedata');
97
117
  if (element == null)
@@ -101,41 +121,49 @@ class LineChart {
101
121
  element.innerText = '-';
102
122
  return;
103
123
  }
104
- // Calculate position on axe-X to value (return if invalid result) and reduce amount of decimal digits.
105
- var curVal = this.dataset.maxVal - event.offsetY / parseFloat(this.dataset.coefVal);
106
- if (isNaN(curVal)) {
124
+ // Calculate position on axe-y to value (return if invalid result) and reduce amount of decimal digits.
125
+ var curValY = this.dataset.maxVal - event.offsetY / parseFloat(this.dataset.coefVal);
126
+ if (isNaN(curValY)) {
107
127
  element.innerText = '-';
108
128
  return;
109
129
  }
110
- if (curVal < 100) {
111
- curVal = curVal.toFixed(2);
130
+ if (curValY < 100) {
131
+ curValY = curValY.toFixed(2);
112
132
  }
113
- else if (curVal < 200) {
114
- curVal = curVal.toFixed(1);
133
+ else if (curValY < 200) {
134
+ curValY = curValY.toFixed(1);
115
135
  }
116
136
  else {
117
- curVal = Math.round(curVal);
137
+ curValY = Math.round(curValY);
118
138
  }
119
- // Calculate position on axe-Y to data-time. If result invalid return.
120
- var curTs = parseInt(this.dataset.minTs) + Math.round((event.offsetX - parseInt(this.dataset.paddingLeft)) / parseFloat(this.dataset.coefTs));
121
- if (isNaN(curTs)) {
122
- element.innerText = '-';
123
- return;
139
+
140
+ // Format of data depend on type of axis-x
141
+ if(this.dataset.axisXType == "time") {
142
+ // Calculate position on axis-x to data-time. If result invalid return.
143
+ var curTs = parseInt(this.dataset.minTs) + Math.round((event.offsetX - parseInt(this.dataset.paddingLeft)) / parseFloat(this.dataset.coefTs));
144
+ if (isNaN(curTs)) {
145
+ element.innerText = '-';
146
+ return;
147
+ }
148
+ // From Unix-Timestamp create data object that contains separate elements of data-time.
149
+ curTs = new Date(curTs * 1000).separate();
150
+ // Set current value and data-time.
151
+ element.innerHTML = 'Date <b>'+ curTs.mday+ '.'+ curTs.month+ '.'+ curTs.year+ '</b> Time <b>'+ curTs.hour+ ':'+ curTs.min+
152
+ '</b> Value <b>'+ curValY+ '</b>';
153
+ } else {
154
+ var curValX = parseInt(this.dataset.minTs) + Math.round((event.offsetX - parseInt(this.dataset.paddingLeft)) / parseFloat(this.dataset.coefTs));
155
+ element.innerHTML = 'Axis-X <b>'+ curValX+ '</b> Axis-Y <b>' + curValY + '</b>';
124
156
  }
125
- // From Unix-Timestamp create data object that contains separate elements of data-time.
126
- curTs = new Date(curTs * 1000).separate();
127
- // Set current value and data-time.
128
- element.innerHTML = 'Date <b>' + curTs.mday + '.' + curTs.month + '.' + curTs.year + '</b> Time <b>' + curTs.hour + ':' + curTs.min + '</b> Value <b>' + curVal + '</b>';
129
157
  });
130
158
  addNewElement(this.chartDiv, 'div', {
131
- class: "chart-live-data",
159
+ class: "chart-live-data, nowrap",
132
160
  id: 'chart_' + chartId+ '_livedata',
133
161
  style: "margin-left: 12px; margin-right: 12px; margin-bottom: 4px; max-width:95%;",
134
162
  innerText: '-'
135
163
  });
136
164
  }
137
165
  // Add div for legend. Each item in the legend is separate sub-div.
138
- this.legendDiv = addNewElement(this.chartDiv, 'div', {
166
+ this._legendDiv = addNewElement(this.chartDiv, 'div', {
139
167
  class: 'chart-legend',
140
168
  id: 'chart_' + chartId+ '_legend'
141
169
  });
@@ -143,42 +171,45 @@ class LineChart {
143
171
  this.chartId = chartId;
144
172
  // Array of objects that represent lines.
145
173
  this.lines = [];
146
- // Integer. Begin of time that show chart. Sec. Unix Time-stamp.
147
- this.minTs = 99999999999999;
148
- // Integer. End of time that show chart. Sec. Unix Time-stamp.
149
- this.maxTs = 0;
150
- // Minimal measuring value.
151
- this.minVal = Number.MAX_SAFE_INTEGER;
152
- // Maximal measuring value.
153
- this.maxVal = Number.MIN_SAFE_INTEGER;
154
- // Difference between two labels on axe Y. Real value, not a pix!
155
- this.axeYLabelStep = null;
156
- // Default and minimal distance (pixel) between labels on axe-y.
157
- this.axeYLabelHeight = 30;
158
- // Default and minimal distance (pixel) between labels on axe-x.
159
- this.axeXLabelWidth = 80;
160
- // Coefficient to convert time-stamp to coordinate X.
161
- this.coefTs = null;
162
- // Coefficient to convert values to coordinate Y.
163
- this.coefVal = null;
174
+ // Integer. Begin of time that show chart. Sec. Unix Time-stamp. Later calculate
175
+ // from data.
176
+ this._minTs = Number.MAX_SAFE_INTEGER;
177
+ // Integer. End of time that show chart. Sec. Unix Time-stamp. Later calculate
178
+ // from data.
179
+ this._maxTs = 0;
180
+ // Minimal measuring value. Later calculate from data.
181
+ this._minVal = Number.MAX_VALUE;
182
+ // Maximal measuring value. Later calculate from data.
183
+ this._maxVal = Number.MIN_VALUE;
184
+ // Distance between two labels on axis Y. Real value, not a pix! Later calculate
185
+ // from data.
186
+ this._axeYLabelStep = null;
187
+ // Default and minimal distance (pixel) between labels on axe-y. Later calculate
188
+ // from data.
189
+ this._axeYLabelHeight= 30;
190
+ // Default distance (pixel) between labels on axe-x. Later calculate
191
+ // from data suitable value.
192
+ this._axeXLabelWidth = 80;
193
+ // Coefficient to convert time-stamp to coordinate X. Later calculate from data.
194
+ this._coefTs = null;
195
+ // Coefficient to convert values to coordinate Y. Later calculate from data.
196
+ this._coefVal = null;
164
197
  // Padding in canvas where to place labels. Later adjust to real width of labels.
165
- this.padding = { left: 50, bottom: 35 };
166
- // Indicate selected in calender end-date. This parmeter used in SingleChart.
167
- this.endDate = null;
168
- // Average step. Seconds. If is valid number used to calculate step by drawing the chart.
169
- // If NaN, not used. This variable may be set manual.
198
+ this._padding = { left: 50, bottom: 35 };
199
+ // Instead automatic, the averadge step on axis X may be set manual. Several measured
200
+ // values are combined to flatten peaks in the graph. Value in seconds. Optional.
201
+ // If NaN, not used.
170
202
  this.avgStep = NaN;
171
203
 
204
+
172
205
  // End consctructor object LineChart.
173
206
  }
174
207
 
175
- // Add data for new line. Automaticaly create new item in legend.
176
- // @parameter lineData Object. The object represent data for one line in chart.
177
- // line.obj_name:"Proxy Server"
178
- // line.par_name:"CpuTotal"
179
- // line.par_id:"2452"
180
- // line.values: Example data-string "161111145730/3.48;161111145843/3.50;161111150013/"
181
- // @return Boolean. True if no problem detected. Else false.
208
+ /**
209
+ * Add new line and create new item in legend.
210
+ * @parameter lineData Object. The object represent data for one line in chart.
211
+ * @return Boolean. True if no problem detected. Else false.
212
+ */
182
213
  addLine (lineData) {
183
214
  if (lineData.values == null) {
184
215
  logger.error('Function LineChart.newLine(). DataString is null.');
@@ -196,14 +227,9 @@ class LineChart {
196
227
  logger.error('Function LineChart.newLine(). Function _createLine() return null.');
197
228
  return false;
198
229
  }
199
- // Add properties of line used to create legend.
200
- line.objName = lineData.obj_name;
201
- line.parName = lineData.par_name;
202
- line.parId = lineData.par_id;
203
- line.color = this.colors[this.lines.length];
204
230
  // Create legend-item as dom-element.
205
- line.legend = addNewElement(this.legendDiv, 'div', {
206
- class: 'chart-legend-item nowrap',
231
+ line.legend = addNewElement(this._legendDiv, 'div', {
232
+ class: 'chart-legend-item nowrap',
207
233
  id: `chart_${this.chartId}_legend_${lineData.par_id}`,
208
234
  style: `cursor: pointer; max-width:${this.chartDiv.clientWidth - 20}px;`,
209
235
  innerHTML: `<span class="chart-legend-color" style="background-color:${line.color}; color:${line.color};">--</span>${line.objName} / ${line.parName}`
@@ -237,9 +263,11 @@ class LineChart {
237
263
  return true;
238
264
  }
239
265
 
240
- // Update data in chart and redraw this one again. All items in legend stay the same!
241
- // @parameter lines Array of objects of type SingleLine.
242
- // @return true if no problem detected else false.
266
+ /**
267
+ * Update data in chart and redraw this one. All items in legend stay the same!
268
+ * @parameter lines Array of objects of type SingleLine.
269
+ * @return true if no problem detected, else false.
270
+ */
243
271
  updateLines (lines) {
244
272
  if (lines == null) {
245
273
  logger.error('Function LineChart.updateLines(). Parameter lines is null.');
@@ -251,13 +279,13 @@ class LineChart {
251
279
  }
252
280
  // Reset to default some parameters of chart.
253
281
  this.lines = [];
254
- this.minTs = 99999999999999;
255
- this.maxTs = 0;
256
- this.minVal = 999999999999999;
257
- this.maxVal = 0;
258
- this.axeYLabelStep = null;
259
- this.coefTs = null;
260
- this.coefVal = null;
282
+ this._minTs = 99999999999999;
283
+ this._maxTs = 0;
284
+ this._minVal = 999999999999999;
285
+ this._maxVal = 0;
286
+ this._axeYLabelStep = null;
287
+ this._coefTs = null;
288
+ this._coefVal = null;
261
289
  // Process lines. Each new line add to array of lines.
262
290
  for (var i in lines) {
263
291
  var line = this._createLine(lines[i]);
@@ -268,21 +296,15 @@ class LineChart {
268
296
  this.draw();
269
297
  return true;
270
298
  }
271
-
272
- _processSelectLegentItem = function() {
273
- console.info("Prozess legent item");
274
- }
275
-
276
- // Create line from object that represent one line of backend's response. If found one negative
277
- // value return null and output error in logger. If data-string is empty, create line with flag
278
- // isEmpty=true. The function also update some variables of chart minTs, maxTs, minVal, maxVal.
279
- // Example data-string "161111145730/3.48;161111145843/;161111150013/"
280
- // Each element separated by ";" in each element time-stamp separated from value by "/".
281
- // Value may be empty! Always in last item the element without value. It indicate only
282
- // where is end of time-period.
299
+
300
+ /**
301
+ * Create line for chart and update some properties of chart minTs, maxTs, minVal, maxVal.
302
+ * @parameter lineData Object.
303
+ * @return SingleLine Object.
304
+ */
283
305
  _createLine (lineData) {
284
- // Create object empty line.
285
- var line = new SingleLine(lineData);
306
+ // Create line. Add data later.
307
+ var line = new SingleLine(lineData, this.colors[this.lines.length]);
286
308
 
287
309
  // Flag. True if value of all items are NaN. In this case set min/max 0/1
288
310
  var isAllNaN = true;
@@ -305,12 +327,12 @@ class LineChart {
305
327
  var item = ar[i].split('/');
306
328
 
307
329
  if (item[0] == "") {
308
- logger.warn('LineChart._createLine(). Item ' + i + ' time-stamp is empty.');
330
+ logger.warn('LineChart._createLine(). Item ' + i + '. Element for axis-x is empty.');
309
331
  continue;
310
332
  }
311
333
 
312
334
  if (isNaN(item[0])) {
313
- logger.warn('LineChart._createLine(). Item ' + i + ' contains invalid time-stamp "' + item[0] + '".');
335
+ logger.warn('LineChart._createLine(). Item ' + i + '. Element for axis-x is not a number "' + item[0] + '".');
314
336
  continue;
315
337
  }
316
338
 
@@ -326,30 +348,30 @@ class LineChart {
326
348
  // Update mininal value. Positive and negative values process differently.
327
349
  if(elem.val < 0) {
328
350
 
329
- if(Math.floor(elem.val * 1.1) < this.minVal) {
330
- this.minVal = Math.floor(elem.val * 1.1);
351
+ if(Math.floor(elem.val * 1.1) < this._minVal) {
352
+ this._minVal = Math.floor(elem.val * 1.1);
331
353
  }
332
354
 
333
355
  } else {
334
356
 
335
- if(Math.floor(elem.val * 0.9) < this.minVal) {
336
- this.minVal = Math.floor(elem.val * 0.9);
357
+ if(Math.floor(elem.val * 0.9) < this._minVal) {
358
+ this._minVal = Math.floor(elem.val * 0.9);
337
359
  }
338
360
  }
339
361
 
340
362
  // Update maximal value.
341
- if (Math.ceil(elem.val * 1.1) > this.maxVal) {
342
- this.maxVal = Math.ceil(elem.val * 1.1);
363
+ if (Math.ceil(elem.val * 1.1) > this._maxVal) {
364
+ this._maxVal = Math.ceil(elem.val * 1.1);
343
365
  }
344
366
 
345
367
  isAllNaN = false;
346
368
  }
347
369
 
348
370
  // Update min/max time-stamp
349
- if (elem.ts < this.minTs)
350
- this.minTs = elem.ts;
351
- if (elem.ts > this.maxTs)
352
- this.maxTs = elem.ts;
371
+ if (elem.ts < this._minTs)
372
+ this._minTs = elem.ts;
373
+ if (elem.ts > this._maxTs)
374
+ this._maxTs = elem.ts;
353
375
  }
354
376
 
355
377
  // If line is empty skip next operations.
@@ -357,98 +379,109 @@ class LineChart {
357
379
  return line;
358
380
 
359
381
  line.isEmpty = false;
382
+
360
383
  // Adjust min/max values if both 0 or all values NaN
361
- if ((this.maxVal == 0 && this.minVal == 0) || isAllNaN) {
362
- this.minVal = 0;
363
- this.maxVal = 1;
384
+ if ((this._maxVal == 0 && this._minVal == 0) || isAllNaN) {
385
+ this._minVal = 0;
386
+ this._maxVal = 1;
364
387
  }
365
388
 
366
389
  // Calculate padding. This area used for labels. Both are type of object TextMetrics.
367
- var minValText = this.context.measureText(this.minVal.toString());
368
- var maxValText = this.context.measureText(this.maxVal.toString());
390
+ var minValText = this._context.measureText(this._minVal.toString());
391
+ var maxValText = this._context.measureText(this._maxVal.toString());
369
392
  // Set place for label using biggest value.
370
393
  if (minValText.width < maxValText.width) {
371
- this.padding.left = Math.round(maxValText.width) + 23;
394
+ this._padding.left = Math.round(maxValText.width) + 23;
372
395
  } else {
373
- this.padding.left = Math.round(minValText.width) + 23;
396
+ this._padding.left = Math.round(minValText.width) + 23;
374
397
  }
375
398
 
376
399
  return line;
377
400
  }
378
401
 
379
- // Clear all on graphic. Legend stay visible.
402
+ /**
403
+ * Clear all on graphic. Legend stay visible.
404
+ */
380
405
  clear () {
381
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
406
+ this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
382
407
  }
383
408
 
384
- // Draw graphic with all elements.
409
+ /**
410
+ * Draw graphic with all elements.
411
+ */
385
412
  draw () {
386
413
  // Update coefficient for values and time-stamps only if line is not empty.
387
- this.coefTs = (this.canvas.width - this.padding.left) / (this.maxTs - this.minTs);
388
- this.coefVal = (this.canvas.height - this.padding.bottom) / (this.maxVal - this.minVal);
414
+ this._coefTs = (this._canvas.width - this._padding.left) / (this._maxTs - this._minTs);
415
+ this._coefVal = (this._canvas.height - this._padding.bottom) / (this._maxVal - this._minVal);
389
416
  // Clear canvas
390
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
417
+ this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
391
418
  // Return true if all lines in the chart are empty.
392
419
  if (this.isEmpty())
393
420
  return;
394
421
  // Draw axe-x and labels with values
395
- this._drawAxeX();
422
+ if(this.axisXType == "time") {
423
+ this._drawAxeXTime();
424
+ } else {
425
+ this._drawAxeXNum();
426
+ }
396
427
  // Draw axe-y and time-labels
397
428
  this._drawAxeY();
398
429
  // Draw each line separate.
399
430
  for (var i = 0; i < this.lines.length; i++) {
400
- this.drawLine(i);
431
+ this._drawLine(i);
401
432
  }
402
433
  // Refresh data need to show live-data
403
- this.canvas.dataset.minTs = this.minTs.toString();
404
- this.canvas.dataset.coefTs = this.coefTs.toString();
405
- this.canvas.dataset.maxVal = this.maxVal.toString();
406
- this.canvas.dataset.coefVal = this.coefVal.toString();
407
- this.canvas.dataset.paddingLeft = this.padding.left.toString();
408
- this.canvas.dataset.paddingBottom = this.padding.bottom.toString();
434
+ this._canvas.dataset.minTs = this._minTs.toString();
435
+ this._canvas.dataset.coefTs = this._coefTs.toString();
436
+ this._canvas.dataset.maxVal = this._maxVal.toString();
437
+ this._canvas.dataset.coefVal = this._coefVal.toString();
438
+ this._canvas.dataset.paddingLeft = this._padding.left.toString();
439
+ this._canvas.dataset.paddingBottom = this._padding.bottom.toString();
409
440
  }
410
441
 
411
- // Set new size E.g. if size of parent panel changed.
442
+ /**
443
+ * Change size of chart and redraw. May be used if size of parent panel changed but data
444
+ * in lines stay the same.
445
+ */
412
446
  resize(difWidth, difHeight) {
413
- //if(!angular.isNumber(difWidth) || !angular.isNumber(difHeight)) return;
414
- this.canvas.width = this.canvas.width + difWidth;
415
- this.canvas.height = this.canvas.height + difHeight;
416
- this.context.translate(0.5, 0.5);
447
+ this._canvas.width = this._canvas.width + difWidth;
448
+ this._canvas.height = this._canvas.height + difHeight;
449
+ this._context.translate(0.5, 0.5);
450
+ this.draw();
417
451
  }
418
452
 
419
- // Draw axe X and labels.
420
- _drawAxeX () {
421
- // Draw axe X
422
- this.context.strokeStyle = this.colorDefault;
423
- this.context.lineWidth = 1;
424
- this.context.beginPath();
425
- this.context.moveTo(this.padding.left, this.canvas.height - this.padding.bottom);
426
- this.context.lineTo(this.canvas.width, this.canvas.height - this.padding.bottom);
427
- this.context.stroke();
428
- // Calculate suitable step between labels on axe-x.
429
- // Average distance between labels in minutes
430
- var stepTs = Math.round(this.axeXLabelWidth / this.coefTs / 60);
431
- // Variable use by loop over array.
453
+ /**
454
+ * Draw axes-x and labels for time based values.
455
+ */
456
+ _drawAxeXTime () {
457
+ this._drawAxeXBasic();
458
+ // Variable use by loop over differentyl arrays.
432
459
  var arLen;
433
- // Select from available steps best suitable.
434
- arLen = this.stepsX.length - 1;
435
- for (var i = 0; i < arLen; i++) {
436
- if (stepTs > this.stepsX[i] && stepTs < this.stepsX[i + 1]) {
437
- stepTs = this.stepsX[i];
438
- break;
460
+ // Calculate suitable step between labels on axis-x. First get average distance between
461
+ // labels in seconds.
462
+ var stepTs = Math.round(this._axeXLabelWidth / this._coefTs );
463
+ // If step is very small set to minimal step 1 second.
464
+ if(stepTs <= 1) {
465
+ stepTs = 1;
466
+ } else {
467
+ // Select from available steps best suitable.
468
+ arLen = this.stepsX.length - 1;
469
+ for (var i = 0; i < arLen; i++) {
470
+ if (stepTs > this.stepsX[i] && stepTs < this.stepsX[i + 1]) {
471
+ stepTs = this.stepsX[i];
472
+ break;
473
+ }
439
474
  }
440
475
  }
441
- // Convert step in minutes to seconds.
442
- stepTs = stepTs * 60;
443
476
  // Calculate time-stamp of first label
444
477
  // Get time of day of minimal time-stamp
445
- var tsLabel = this._getBeginOfDayFromUnixTs(this.minTs);
478
+ var tsLabel = this._getBeginOfDayFromUnixTs(this._minTs);
446
479
  var isFound = false;
447
480
  // Limit time-stamp of minimal label to current time.
448
481
  // Time-stamp of first label can't be bigger as as current time.
449
482
  var tsNow = new Date().getTime() / 1000;
450
483
  while (tsLabel < tsNow) {
451
- if (tsLabel > this.minTs) {
484
+ if (tsLabel > this._minTs) {
452
485
  isFound = true;
453
486
  break;
454
487
  }
@@ -457,109 +490,162 @@ class LineChart {
457
490
  // Create array of labels width coordinate-X and time-stamp for label.
458
491
  var labels = [];
459
492
  var posX, labelStr;
460
- while (tsLabel < this.maxTs) {
493
+ while (tsLabel < this._maxTs) {
461
494
  posX = this._convertTs2PosX(tsLabel);
462
- labelStr = this._formatTsForLabel(tsLabel, (this.maxTs - this.minTs));
495
+ labelStr = this._formatTsForLabel(tsLabel, (this._maxTs - this._minTs));
463
496
  labels.push({ X: posX, str: labelStr });
464
497
  tsLabel = tsLabel + stepTs;
465
498
  }
466
499
  // Begin draw vertical label-lines and help-lines.
467
- this.context.beginPath();
468
- // Color for label-lines and help-lines.
469
- this.context.strokeStyle = '#e6e6e6';
500
+ this._context.beginPath();
501
+ this._context.strokeStyle = '#e6e6e6';
470
502
  // Distance between label-line and help-line.
471
- var halfStep = Math.round(stepTs * this.coefTs / 2);
503
+ var halfStep = Math.round(stepTs * this._coefTs / 2);
472
504
  // For all lines position Y is the same
473
- var posY = this.canvas.height - this.padding.bottom;
505
+ var posY = this._canvas.height - this._padding.bottom;
474
506
  arLen = labels.length;
475
507
  for (var i = 0; i < arLen; i++) {
476
- this.context.moveTo(labels[i].X, 0);
477
- this.context.lineTo(labels[i].X, posY);
508
+ this._context.moveTo(labels[i].X, 0);
509
+ this._context.lineTo(labels[i].X, posY+ 10);
478
510
  // Help-line between two labels
479
- if (labels[i].X - halfStep > this.padding.left + 2) {
480
- this.context.moveTo(labels[i].X - halfStep, 0);
481
- this.context.lineTo(labels[i].X - halfStep, posY);
511
+ if (labels[i].X - halfStep > this._padding.left + 2) {
512
+ this._context.moveTo(labels[i].X - halfStep, 0);
513
+ this._context.lineTo(labels[i].X - halfStep, posY);
482
514
  }
483
515
  }
484
516
  // Last help-line after last label if not outside canvas
485
517
  if (labels[i - 1] != null) {
486
- if (labels[i - 1].X + halfStep <= this.canvas.width) {
487
- this.context.moveTo(labels[i - 1].X + halfStep, 0);
488
- this.context.lineTo(labels[i - 1].X + halfStep, posY);
518
+ if (labels[i - 1].X + halfStep <= this._canvas.width) {
519
+ this._context.moveTo(labels[i - 1].X + halfStep, 0);
520
+ this._context.lineTo(labels[i - 1].X + halfStep, posY);
489
521
  }
490
522
  }
491
- this.context.stroke();
523
+ this._context.stroke();
492
524
  // Type labels on axe-x
493
- this.context.font = "12px arial";
525
+ this._context.font = "12px arial";
494
526
  arLen = labels.length;
495
527
  for (i = 0; i < arLen; i++) {
496
528
  // Print first part of label Hour:Min
497
- this.context.fillText(labels[i].str.hm, labels[i].X - 17, (posY + 17));
529
+ this._context.fillText(labels[i].str.hm, labels[i].X - 17, (posY + 17));
498
530
  // The second part of label (Day.Mon) print only by first label at label by begin of day.
499
531
  if (i == 0) {
500
- this.context.fillText(labels[i].str.dm, labels[i].X - 17, (posY + 32));
532
+ this._context.fillText(labels[i].str.dm, labels[i].X - 17, (posY + 32));
501
533
  }
502
534
  else {
503
535
  if (labels[i].str.dm != labels[i - 1].str.dm) {
504
- this.context.fillText(labels[i].str.dm, labels[i].X - 17, (posY + 32));
536
+ this._context.fillText(labels[i].str.dm, labels[i].X - 17, (posY + 32));
505
537
  }
506
538
  }
507
539
  }
508
- this.context.closePath();
540
+ this._context.closePath();
541
+ }
542
+
543
+ /**
544
+ * Draw axis-x and labels for numeric values.
545
+ */
546
+ _drawAxeXNum () {
547
+ this._drawAxeXBasic();
548
+ // Variable use by loop over differentyl arrays.
549
+ let arLen;
550
+ // Find optimal step between labels. First get average value, then find optimal value.
551
+ let stepX = Math.round(this._axeXLabelWidth / this._coefTs );
552
+ stepX = this._getNextPreferedNumber(stepX);
553
+ // Calculate value of of first label
554
+ let minXLabelVal = this._getNextPreferedNumber(this._minTs);
555
+
556
+ // Create array of labels width coordinate-X and value for label.
557
+ let labelsX = [];
558
+ while (minXLabelVal < this._maxTs) {
559
+ labelsX.push({ X: this._convertTs2PosX(minXLabelVal), str: minXLabelVal });
560
+ minXLabelVal = minXLabelVal + stepX;
561
+ }
562
+
563
+ // Begin draw vertical label-lines and help-lines.
564
+ this._context.beginPath();
565
+ this._context.strokeStyle = '#e6e6e6';
566
+ // Distance between label-line and help-line.
567
+ let halfStep = Math.round(stepX * this._coefTs / 2);
568
+ // For all lines position Y is the same
569
+ let posY = this._canvas.height - this._padding.bottom;
570
+ arLen = labelsX.length;
571
+ for (i = 0; i < arLen; i++) {
572
+ this._context.moveTo(labelsX[i].X, 0);
573
+ this._context.lineTo(labelsX[i].X, posY+ 10);
574
+ // Help-line between two labels
575
+ if (labelsX[i].X - halfStep > this._padding.left + 2) {
576
+ this._context.moveTo(labelsX[i].X - halfStep, 0);
577
+ this._context.lineTo(labelsX[i].X - halfStep, posY);
578
+ }
579
+ }
580
+ // Last help-line after last label if not outside canvas
581
+ if (labelsX[i - 1] != null) {
582
+ if (labelsX[i - 1].X + halfStep <= this._canvas.width) {
583
+ this._context.moveTo(labelsX[i - 1].X + halfStep, 0);
584
+ this._context.lineTo(labelsX[i - 1].X + halfStep, posY);
585
+ }
586
+ }
587
+ this._context.stroke();
588
+
589
+ // Type labels on axe-x
590
+ this._context.font = "12px arial";
591
+ arLen = labelsX.length;
592
+ for (i = 0; i < arLen; i++) {
593
+
594
+ this._context.fillText(labelsX[i].str, labelsX[i].X, (posY + 20));
595
+ }
596
+ this._context.closePath();
597
+
598
+ // End_ drawAxeNum()
509
599
  }
510
600
 
511
- // Calculate coordinates for labels on axe Y. Distance between labels approximately 40px.
512
- // Draw axe Y, labels and help-lines (the line between two labels). For labels use place
513
- // in chart.padding.left. Draw one help-line at label and second line between labels.
601
+ /**
602
+ * Draw asix-x as line.
603
+ */
604
+ _drawAxeXBasic() {
605
+ this._context.strokeStyle = this.colorDefault;
606
+ this._context.lineWidth = 1 ;
607
+ this._context.beginPath();
608
+ this._context.moveTo(this._padding.left, this._canvas.height - this._padding.bottom);
609
+ this._context.lineTo(this._canvas.width, this._canvas.height - this._padding.bottom);
610
+ this._context.stroke();
611
+ }
612
+
613
+ /**
614
+ * Calculate coordinates for labels on axis Y. Distance between labels approximately 40px.
615
+ * Draw axe Y, labels and help-lines (the line between two labels). For labels use place
616
+ * in chart.padding.left. Draw one help-line at label and second line between labels.
617
+ */
514
618
  _drawAxeY () {
515
619
  // No think to do if coefficients are null. May be the chart contains only one
516
620
  // empty line (without measuring-data). In this case don't need draw axes.
517
- if (this.coefTs == null || this.coefVal == null)
621
+ if (this._coefTs == null || this._coefVal == null)
518
622
  return;
519
623
  // Variable used by efficiently loop over big array.
520
624
  var arLen;
521
625
  // How much labels with default height fit into current axe Y.
522
- var labelsAmount = (this.canvas.height - this.padding.bottom) / this.axeYLabelHeight;
523
- // Find optimal step between labels based. First calculate exact step. In second step
524
- // (in loop) find optimal value of step using basic values in array steps.
525
- this.axeYLabelStep = (this.maxVal - this.minVal) / labelsAmount;
526
- // Find optimal value of step
527
- var c = 1;
528
- var isFound = false;
529
- var minStep, maxStep;
530
- while (c < 100000000000) {
531
- for (var i = 0; i < (this.stepsY.length - 1); i++) {
532
- minStep = this.stepsY[i] * c;
533
- maxStep = this.stepsY[i + 1] * c;
534
- if (minStep <= this.axeYLabelStep && this.axeYLabelStep <= maxStep) {
535
- this.axeYLabelStep = maxStep;
536
- isFound = true;
537
- break;
538
- }
539
- }
540
- if (isFound)
541
- break;
542
- c = c * 10;
543
- }
626
+ var labelsAmount = (this._canvas.height - this._padding.bottom) / this._axeYLabelHeight;
627
+ // Find optimal step between labels. First get average value, then find optimal value.
628
+ this._axeYLabelStep = (this._maxVal - this._minVal) / labelsAmount;
629
+ this._axeYLabelStep = this._getNextPreferedNumber(this._axeYLabelStep);
544
630
  // Find value of first label. It is not chart.minVal! It is next
545
631
  // suitable value. Consider that chart.MinVal may be negative.
546
632
  var labelMinVal = 0;
547
- if (this.minVal < 0) {
548
- while (labelMinVal > this.minVal) {
549
- if (labelMinVal <= this.minVal)
633
+ if (this._minVal < 0) {
634
+ while (labelMinVal > this._minVal) {
635
+ if (labelMinVal <= this._minVal)
550
636
  break;
551
- labelMinVal -= this.axeYLabelStep;
637
+ labelMinVal -= this._axeYLabelStep;
552
638
  }
553
- } else if (this.minVal > 0) {
554
- while (labelMinVal < this.maxVal) {
555
- if (labelMinVal >= this.minVal)
639
+ } else if (this._minVal > 0) {
640
+ while (labelMinVal < this._maxVal) {
641
+ if (labelMinVal >= this._minVal)
556
642
  break;
557
- labelMinVal += this.axeYLabelStep;
643
+ labelMinVal += this._axeYLabelStep;
558
644
  }
559
645
  }
560
646
  // Calculate maximal hight of axe-y. This value used to prevent labels on axe-y
561
- // below axe-x. This may happy some times if this.minVal is negative.
562
- var axeYHight = this.canvas.height - this.padding.bottom;
647
+ // below axe-x. This may happy some times if this._minVal is negative.
648
+ var axeYHight = this._canvas.height - this._padding.bottom;
563
649
 
564
650
  // Create array of labels. Each item in array contains {value:value, Y:position-Y}
565
651
  // It will be used to draw labels and help-lines. The loop limit to 33 labels to
@@ -570,10 +656,10 @@ class LineChart {
570
656
 
571
657
  while (i < 33) {
572
658
 
573
- labelValue = labelMinVal + (this.axeYLabelStep * i);
659
+ labelValue = labelMinVal + (this._axeYLabelStep * i);
574
660
 
575
661
  // If step less as 1 or =2.5, format values of label to two decimal digits
576
- if (this.axeYLabelStep < 1 || this.axeYLabelStep == 2.5)
662
+ if (this._axeYLabelStep < 1 || this._axeYLabelStep == 2.5)
577
663
  labelValue = labelValue.toFixed(2);
578
664
 
579
665
  labelPosY = this._convertVal2PosY(labelValue);
@@ -582,7 +668,7 @@ class LineChart {
582
668
  if (labelPosY < 0)
583
669
  break;
584
670
 
585
- // Some time if this.minVal is negative, label at the bottom placed below axe-x.
671
+ // Some time if this._minVal is negative, label at the bottom placed below axe-x.
586
672
  // To prevent it add label to array only if label's position is smaler as available
587
673
  // hight of chart.
588
674
  if(labelPosY < axeYHight) {
@@ -593,55 +679,57 @@ class LineChart {
593
679
  i++;
594
680
  }
595
681
  // Begin drawing.
596
- this.context.lineWidth = 1;
597
- this.context.beginPath();
682
+ this._context.lineWidth = 1;
683
+ this._context.beginPath();
598
684
  // Draw axe-Y
599
- this.context.strokeStyle = this.colorDefault;
600
- this.context.moveTo(this.padding.left, 0);
601
- this.context.lineTo(this.padding.left, this.canvas.height - this.padding.bottom);
602
- this.context.stroke();
685
+ this._context.strokeStyle = this.colorDefault;
686
+ this._context.moveTo(this._padding.left, 0);
687
+ this._context.lineTo(this._padding.left, this._canvas.height - this._padding.bottom);
688
+ this._context.stroke();
603
689
  // Create horizontal label-lines and help-lines. Begin from bottom.
604
- this.context.beginPath();
690
+ this._context.beginPath();
605
691
  // Color for label-lines and help-lines. Gray 90%
606
- this.context.strokeStyle = '#e6e6e6';
692
+ this._context.strokeStyle = '#e6e6e6';
607
693
  // Position Y for help-lines. (The line between two help-lines)
608
694
  var helpLinePosY;
609
695
  arLen = axeY.length;
610
696
  for (i = 0; i < arLen; i++) {
611
697
  // Draw help-line between labels
612
- helpLinePosY = axeY[i].Y + Math.round(this.coefVal * this.axeYLabelStep / 2);
613
- if (helpLinePosY < this.canvas.height - this.padding.bottom) {
614
- this.context.moveTo(this.padding.left, helpLinePosY);
615
- this.context.lineTo(this.canvas.width, helpLinePosY);
698
+ helpLinePosY = axeY[i].Y + Math.round(this._coefVal * this._axeYLabelStep / 2);
699
+ if (helpLinePosY < this._canvas.height - this._padding.bottom) {
700
+ this._context.moveTo(this._padding.left, helpLinePosY);
701
+ this._context.lineTo(this._canvas.width, helpLinePosY);
616
702
  }
617
703
  // Create help-line where label defined. Skip if help-line on axe X.
618
- if (axeY[i].Y >= this.canvas.height - this.padding.bottom)
704
+ if (axeY[i].Y >= this._canvas.height - this._padding.bottom)
619
705
  continue;
620
- this.context.moveTo(this.padding.left, axeY[i].Y);
621
- this.context.lineTo(this.canvas.width, axeY[i].Y);
706
+ this._context.moveTo(this._padding.left, axeY[i].Y);
707
+ this._context.lineTo(this._canvas.width, axeY[i].Y);
622
708
  }
623
709
  // If possible create help-line after last top label
624
- helpLinePosY = axeY[axeY.length - 1].Y - Math.round(this.coefVal * this.axeYLabelStep / 2);
710
+ helpLinePosY = axeY[axeY.length - 1].Y - Math.round(this._coefVal * this._axeYLabelStep / 2);
625
711
  if (helpLinePosY > 0) {
626
- this.context.moveTo(this.padding.left, helpLinePosY);
627
- this.context.lineTo(this.canvas.width, helpLinePosY);
712
+ this._context.moveTo(this._padding.left, helpLinePosY);
713
+ this._context.lineTo(this._canvas.width, helpLinePosY);
628
714
  }
629
715
  // Draw all created lines
630
- this.context.stroke();
716
+ this._context.stroke();
631
717
  // Draw labels on axe Y
632
- this.context.font = "12px arial";
633
- this.context.strokeStyle = this.colorDefault;
718
+ this._context.font = "12px arial";
719
+ this._context.strokeStyle = this.colorDefault;
634
720
  // Print labels on left site of axe Y and under the line.
635
721
  // The position defined left bottom corner of text-box.
636
722
  arLen = axeY.length;
637
723
  for (i = 0; i < arLen; i++) {
638
- this.context.fillText(axeY[i].value, 0, axeY[i].Y + 10);
724
+ this._context.fillText(axeY[i].value, 0, axeY[i].Y + 10);
639
725
  }
640
726
  }
641
727
 
642
- // Draw one line on existing graphic. Used afte add new line.
643
- // @param lineIndex Number. Index of line in array this.lines[]
644
- drawLine (lineIndex) {
728
+ /**
729
+ * Draw one line on existing graphic. Used afte add new line.
730
+ * @param lineIndex Number. Index of line in array this.lines[]
731
+ */
732
+ _drawLine (lineIndex) {
645
733
  // Create local reference to processed line.
646
734
  var line = this.lines[lineIndex];
647
735
  if (line == null) {
@@ -743,38 +831,84 @@ class LineChart {
743
831
  pos.push(item);
744
832
  }
745
833
  // Begin draw line
746
- this.context.strokeStyle = this.colors[lineIndex];
747
- this.context.lineWidth = 1;
748
- this.context.beginPath();
834
+ this._context.strokeStyle = this.colors[lineIndex];
835
+ this._context.lineWidth = 1;
836
+ this._context.beginPath();
749
837
  // Set first position to first valid measuring value
750
838
  arLen = pos.length;
751
839
  for (i = 0; i < arLen; i++) {
752
840
  if (!isNaN(pos[i].Y)) {
753
- this.context.moveTo(pos[i].X, pos[i].Y);
841
+ this._context.moveTo(pos[i].X, pos[i].Y);
754
842
  i++;
755
843
  break;
756
844
  }
757
845
  }
758
846
  while (i < arLen) {
847
+
848
+ // If valu is valid number containe to draw line.
759
849
  if (!isNaN(pos[i].Y)) {
760
- this.context.lineTo(pos[i].X, pos[i].Y);
850
+ this._context.lineTo(pos[i].X, pos[i].Y);
761
851
  i++;
762
852
  } else {
763
- this.context.stroke();
764
- i++;
765
- while (i < pos.length) {
766
- if (!isNaN(pos[i].Y)) {
853
+ // Case value is no a number. Different processing depend on flag connectIfMissing
854
+ if (this.connectIfMissing == true) {
855
+ // Case connect "begin" and "end" of missing range.
856
+ while (i < pos.length) {
857
+ if (!isNaN(pos[i].Y)) {
858
+ break;
859
+ }
860
+ i++;
861
+ }
862
+ } else {
863
+ // Case don't draw line where data missing
864
+ this._context.stroke();
865
+ this._context.beginPath();
866
+
867
+ while (i < pos.length) {
868
+ if (!isNaN(pos[i].Y)) {
869
+ break;
870
+ }
767
871
  i++;
768
- break;
769
872
  }
770
- i++;
771
873
  }
772
874
  }
773
875
  }
774
- this.context.stroke();
876
+ this._context.stroke();
877
+ }
878
+
879
+ /**
880
+ * Get next prefered number from 1-2-5 serie. Min 1E-09 max 1E18. Used to calculate optimal
881
+ * step on axis Y and X if type "number". See https://en.wikipedia.org/wiki/Preferred_number
882
+ * Or used to get value of fist label on axis-x.
883
+ * @param num {number} If 0 retrun 0.
884
+ * @returns number or null if step outside allowed values.
885
+ */
886
+ _getNextPreferedNumber(num) {
887
+
888
+ if(num == 0)
889
+ return 0;
890
+
891
+ // E1 preferred number series (1-2-5 series)
892
+ let steps = [1E-9,2E-9,5E-9,1E-8];
893
+ let factor = 1;
894
+ let minStep, maxStep;
895
+ while (factor < 1E18+1) {
896
+ for (var i = 0; i < (steps.length - 1); i++) {
897
+ minStep = steps[i] * factor;
898
+ maxStep = steps[i + 1] * factor;
899
+ if (minStep <= num && num <= maxStep) {
900
+ //logger.info(`Counter c=${c}, avg step=${step}, new step=${maxStep}`);
901
+ return maxStep;
902
+ }
903
+ }
904
+ factor = factor * 10;
905
+ }
906
+ return null;
775
907
  }
776
908
 
777
- // Return true if all lines are empty
909
+ /**
910
+ * Return true if all lines are empty
911
+ */
778
912
  isEmpty () {
779
913
  var isEmpty = true;
780
914
  for (var i = 0; i < this.lines.length; i++) {
@@ -785,25 +919,31 @@ class LineChart {
785
919
  return isEmpty;
786
920
  }
787
921
 
788
- // Calculate from time-stamp position X using coefficient for axe X and available width.
922
+ /**
923
+ * Calculate from time-stamp position X using coefficient for axe X and available width.
924
+ */
789
925
  _convertTs2PosX (ts) {
790
- return (this.canvas.width - Math.round((this.maxTs - ts) * this.coefTs));
926
+ return (this._canvas.width - Math.round((this._maxTs - ts) * this._coefTs));
791
927
  }
792
928
 
793
- // Calculate from measuring value position Y using coefficient for axe Y and available height.
794
- // Return rounded integer.
929
+ /**
930
+ * Calculate from measuring value position Y using coefficient for axe Y and available height.
931
+ * Return rounded integer.
932
+ */
795
933
  _convertVal2PosY (val) {
796
- return (Math.round((this.maxVal - val) * this.coefVal));
934
+ return (Math.round((this._maxVal - val) * this._coefVal));
797
935
  }
798
936
 
799
- // Convert unix time-stamp to object that contains two elements. This object used as label on
800
- // Axe-X. Result is dependet on difference. By small difference in first line show hour:min,
801
- // second line day.mon. By difference between 3-180 days in first line show day.mon in second
802
- // line yaer. By difference > 180 days in first line show month in second line yaer. Day and
803
- // month in format DD.MM always two digits. Hours and minutes in format HH:MM always two digits.
804
- // @param ts Number. Unix-time-stamp.
805
- // @param tsdif Number. Difference between first and last timestamp
806
- // @return Object. The object used to create labels on axe-x.
937
+ /**
938
+ * Convert unix time-stamp to object that contains two elements. This object used as label on
939
+ * Axe-X. Result is dependet on difference. By small difference in first line show hour:min,
940
+ * second line day.mon. By difference between 3-180 days in first line show day.mon in second
941
+ * line yaer. By difference > 180 days in first line show month in second line yaer. Day and
942
+ * month in format DD.MM always two digits. Hours and minutes in format HH:MM always two digits.
943
+ * @param ts Number. Unix-time-stamp.
944
+ * @param tsdif Number. Difference between first and last timestamp
945
+ * @return Object. The object used to create labels on axe-x.
946
+ */
807
947
  _formatTsForLabel (ts, tsdif) {
808
948
  let date = new Date(ts * 1000);
809
949
 
@@ -842,9 +982,11 @@ class LineChart {
842
982
  return result;
843
983
  }
844
984
 
845
- // Calculate begin of day in seconds from unix time-stamp in seconds.
846
- // @param ts {number} unix time-stamp in seconds
847
- // @return {Number}
985
+ /**
986
+ * Calculate begin of day in seconds from unix time-stamp in seconds.
987
+ * @param ts {number} unix time-stamp in seconds
988
+ * @return {Number}
989
+ */
848
990
  _getBeginOfDayFromUnixTs (ts) {
849
991
  var result = new Date(ts * 1000);
850
992
  result = new Date(result.getFullYear(), result.getMonth(), result.getDate());
@@ -870,42 +1012,76 @@ class LineChart {
870
1012
 
871
1013
  // Default color used in some elements
872
1014
  colorDefault = '#657585';
873
-
874
- // Array of possible steps on axe-Y.
875
- stepsX = [2, 5, 10, 15, 20, 30, 60, 90, 2*60, 4*60, 5*60, 6*60, 12*60, 24*60, 2*24*60, 3*24*60, 4*24*60,
876
- 5*24*60, 7*24*60, 10*24*60, 15*24*60, 20*24*60, 30*24*60, 45*24*60, 60*24*60, 90*24*60, 120*24*60];
877
-
878
- // Array of minimal steps for axe-X
879
- stepsY = [0.01, 0.02, 0.05, 0.1];
1015
+
1016
+ // Array of possible steps on axis-X in seconds
1017
+ stepsX = [1, 2, 5, 10, 20, 30, 1*60, 2*60, 5*60, 10*60, 15*60, 20*60, 30*60,
1018
+ 60*60, 2*60*60, 4*60*60, 6*60*60, 12*60*60, 24*60*60, 2*24*60*60, 3*24*60*60,
1019
+ 4*24*60*60, 5*24*60*60, 7*24*60*60, 10*24*60*60, 15*24*60*60, 20*24*60*60,
1020
+ 30*24*60*60, 45*24*60*60, 60*24*60*60, 90*24*60*60, 120*24*60*60];
880
1021
 
881
1022
  // End class LineChart
882
1023
  }
883
1024
 
884
- // The object represent line that used in charts.
885
- // @constructor:
886
- // @param lineData Object. The object represent data of one line in chart.
887
- // obj_name - String. The string used as label for th line. object_name + parameter_name.
888
- // par_id - String. Optional. May be used to request data from database.
889
- // par_name - Srtring. The string used as label for th line. object_name + parameter_name.
890
- // values - String. Semicolon separated pairs "Unix-Timestamp/Value;Unix-Timestamp/Value;.."
891
- // The values may be empty! The last element should be withoud value!!! It Indicate
892
- // end of timespane of this line. Example :
893
- // "161111145820/3.36;161111145880/3.42;161111145940/3.50;161111146000/3.58;161111146060/3.48,;161111146120/3.08;161111146120/"
1025
+ /**
1026
+ * The object represent a line in chart.
1027
+ * @class
1028
+ * @param lineData {LineData}
1029
+ * @param color {string} color in HTML format, name or hex.
1030
+ */
894
1031
  class SingleLine {
895
- constructor(lineData) {
896
- this.parId = lineData.par_id; // This Id may be used to request data from database.
897
- this.parName = lineData.par_name; // Name of parameter representing this line. E.g "CPU Total"
898
- this.objName = lineData.obj_name; // Name of object where belong this parameter. E.g. "Proxy server"
1032
+ constructor(lineData, color) {
1033
+ this.parId = lineData.par_id;
1034
+ this.parName = lineData.par_name != null ? lineData.par_name : "";
1035
+ this.objName = lineData.obj_name != null ? lineData.obj_name : "";
899
1036
  this.real = []; // Empty array for pairs ts/val. "ts" is unix time-stamp, "val" is measuring value.
900
- // Fill with data later, because need update min/max parsmeter of chart.
1037
+
1038
+ // Set connectIfMissing. Default false.
1039
+ if(lineData.connectIfmissing == null) {
1040
+ this.connectIfmissing = false;
1041
+ } else {
1042
+ this.connectIfmissing = lineData.connectIfmissing; // By false don't draw line if data missing
1043
+ }
1044
+
901
1045
  this.isEmpty = true; // Flag indicate that line contains no data.
902
1046
  this.hidden = false; // Property uses to hide line in chart by click on legend item.
903
- this.color = "black" // Default color for line
1047
+
1048
+ if(color == null) {
1049
+ this.color = "black"; // Default color for line
1050
+ } else {
1051
+ this.color = color;
1052
+ }
1053
+
1054
+ this.legend = null; // HTML-Element to show one elemen in chart's legend. Init later.
904
1055
  }
905
1056
  }
906
1057
 
907
- var allCharts = [];
908
-
909
- // Version
910
- LineChart.prototype.Version = "1.2.0";
911
-
1058
+ /**
1059
+ * The object represent data to create one line in chart.
1060
+ * @class
1061
+ * @param obj_name {string} is visible in legen of line.
1062
+ * @param par_name {string}, tring is visible in legen of line.
1063
+ * @param par_id {number} internal id of line. Optional. May be used to request data from backend.
1064
+ * @param values {string}, elements separates by ";". Each element containing time-stamp and
1065
+ * meassuring value separated by "/". eg. "161111145730/3.48;161111145843/3.50;161111150013/"
1066
+ * Value may be in range Number.MIN_VALUE till Number.MAX_VALUE or may be missing. The last element
1067
+ * must be without value. It indicating where is end of time-period for this line.
1068
+ * @param connectIfMissing {boolean} if false don't draw line where data are missing, else connect
1069
+ * begin and end of range where data are mising. Default false.
1070
+ * @example
1071
+ * line = {
1072
+ * obj_name:"Proxy Server",
1073
+ * par_name:"CpuTotal",
1074
+ * par_id:"2452",
1075
+ * values: "161111145730/3.48;161111145843/3.50;161111150013/",
1076
+ * connectIfMissing: true
1077
+ * }
1078
+ */
1079
+ class LineData {
1080
+ constructor(obj_name, par_name, par_id, values, connectIfMissing) {
1081
+ this.obj_name = obj_name;
1082
+ this.par_name = par_name;
1083
+ this.par_id = par_id;
1084
+ this.values = values;
1085
+ this.connectIfMissing = connectIfMissing ;
1086
+ }
1087
+ }