adaptive-linechart 1.2.1 → 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/CHANGELOG.md +7 -1
- package/Developer How-To.md +17 -0
- package/README.md +47 -40
- package/dist/linechart-all.min.js +1 -2
- package/dist/linechart.css +1 -1
- package/example/example.csv +5 -5
- package/example/example.js +42 -56
- package/example/test.html +18 -0
- package/package.json +1 -1
- package/src/chart-util.js +47 -31
- package/src/linechart.css +1 -1
- package/src/linechart.js +487 -311
package/src/linechart.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
41
|
+
// Create title for chart if parameter title is not null.
|
|
34
42
|
if (title != null) {
|
|
35
|
-
|
|
43
|
+
let titleElement = addNewElement(this.chartDiv, 'label', {
|
|
36
44
|
class: 'chart-title nowrap',
|
|
37
|
-
style: 'width:'
|
|
45
|
+
style: 'width:'+ (width - 20)+ 'px; text-align:center; display:block; margin-left:10px; margin-top:10px',
|
|
38
46
|
});
|
|
39
|
-
|
|
40
|
-
}
|
|
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.
|
|
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.
|
|
94
|
+
this._canvas.dataset.axisXType = this.axisXType;
|
|
95
|
+
this._canvas.dataset.chartId = chartId.toString();
|
|
76
96
|
// Save reference to context.
|
|
77
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
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-
|
|
105
|
-
var
|
|
106
|
-
if (isNaN(
|
|
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 (
|
|
111
|
-
|
|
130
|
+
if (curValY < 100) {
|
|
131
|
+
curValY = curValY.toFixed(2);
|
|
112
132
|
}
|
|
113
|
-
else if (
|
|
114
|
-
|
|
133
|
+
else if (curValY < 200) {
|
|
134
|
+
curValY = curValY.toFixed(1);
|
|
115
135
|
}
|
|
116
136
|
else {
|
|
117
|
-
|
|
137
|
+
curValY = Math.round(curValY);
|
|
118
138
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if
|
|
122
|
-
|
|
123
|
-
|
|
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.
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
//
|
|
151
|
-
this.
|
|
152
|
-
//
|
|
153
|
-
this.
|
|
154
|
-
//
|
|
155
|
-
this.
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
//
|
|
161
|
-
this.
|
|
162
|
-
//
|
|
163
|
-
|
|
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.
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
//
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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.
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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.
|
|
255
|
-
this.
|
|
256
|
-
this.
|
|
257
|
-
this.
|
|
258
|
-
this.
|
|
259
|
-
this.
|
|
260
|
-
this.
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
|
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 + '
|
|
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 + '
|
|
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.
|
|
330
|
-
this.
|
|
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.
|
|
336
|
-
this.
|
|
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.
|
|
342
|
-
this.
|
|
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.
|
|
350
|
-
this.
|
|
351
|
-
if (elem.ts > this.
|
|
352
|
-
this.
|
|
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.
|
|
362
|
-
this.
|
|
363
|
-
this.
|
|
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.
|
|
368
|
-
var maxValText = this.
|
|
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.
|
|
394
|
+
this._padding.left = Math.round(maxValText.width) + 23;
|
|
372
395
|
} else {
|
|
373
|
-
this.
|
|
396
|
+
this._padding.left = Math.round(minValText.width) + 23;
|
|
374
397
|
}
|
|
375
398
|
|
|
376
399
|
return line;
|
|
377
400
|
}
|
|
378
401
|
|
|
379
|
-
|
|
402
|
+
/**
|
|
403
|
+
* Clear all on graphic. Legend stay visible.
|
|
404
|
+
*/
|
|
380
405
|
clear () {
|
|
381
|
-
this.
|
|
406
|
+
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
|
382
407
|
}
|
|
383
408
|
|
|
384
|
-
|
|
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.
|
|
388
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
431
|
+
this._drawLine(i);
|
|
401
432
|
}
|
|
402
433
|
// Refresh data need to show live-data
|
|
403
|
-
this.
|
|
404
|
-
this.
|
|
405
|
-
this.
|
|
406
|
-
this.
|
|
407
|
-
this.
|
|
408
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
414
|
-
this.
|
|
415
|
-
this.
|
|
416
|
-
this.
|
|
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
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
this.
|
|
424
|
-
|
|
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
|
-
//
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
493
|
+
while (tsLabel < this._maxTs) {
|
|
461
494
|
posX = this._convertTs2PosX(tsLabel);
|
|
462
|
-
labelStr = this._formatTsForLabel(tsLabel, (this.
|
|
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.
|
|
468
|
-
|
|
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.
|
|
503
|
+
var halfStep = Math.round(stepTs * this._coefTs / 2);
|
|
472
504
|
// For all lines position Y is the same
|
|
473
|
-
var posY = this.
|
|
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.
|
|
477
|
-
this.
|
|
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.
|
|
480
|
-
this.
|
|
481
|
-
this.
|
|
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.
|
|
487
|
-
this.
|
|
488
|
-
this.
|
|
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.
|
|
523
|
+
this._context.stroke();
|
|
492
524
|
// Type labels on axe-x
|
|
493
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
536
|
+
this._context.fillText(labels[i].str.dm, labels[i].X - 17, (posY + 32));
|
|
505
537
|
}
|
|
506
538
|
}
|
|
507
539
|
}
|
|
508
|
-
this.
|
|
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
|
-
|
|
512
|
-
|
|
513
|
-
|
|
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.
|
|
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.
|
|
523
|
-
// Find optimal step between labels
|
|
524
|
-
|
|
525
|
-
this.
|
|
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.
|
|
548
|
-
while (labelMinVal > this.
|
|
549
|
-
if (labelMinVal <= this.
|
|
633
|
+
if (this._minVal < 0) {
|
|
634
|
+
while (labelMinVal > this._minVal) {
|
|
635
|
+
if (labelMinVal <= this._minVal)
|
|
550
636
|
break;
|
|
551
|
-
labelMinVal -= this.
|
|
637
|
+
labelMinVal -= this._axeYLabelStep;
|
|
552
638
|
}
|
|
553
|
-
} else if (this.
|
|
554
|
-
while (labelMinVal < this.
|
|
555
|
-
if (labelMinVal >= this.
|
|
639
|
+
} else if (this._minVal > 0) {
|
|
640
|
+
while (labelMinVal < this._maxVal) {
|
|
641
|
+
if (labelMinVal >= this._minVal)
|
|
556
642
|
break;
|
|
557
|
-
labelMinVal += this.
|
|
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.
|
|
562
|
-
var axeYHight = this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
597
|
-
this.
|
|
682
|
+
this._context.lineWidth = 1;
|
|
683
|
+
this._context.beginPath();
|
|
598
684
|
// Draw axe-Y
|
|
599
|
-
this.
|
|
600
|
-
this.
|
|
601
|
-
this.
|
|
602
|
-
this.
|
|
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.
|
|
690
|
+
this._context.beginPath();
|
|
605
691
|
// Color for label-lines and help-lines. Gray 90%
|
|
606
|
-
this.
|
|
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.
|
|
613
|
-
if (helpLinePosY < this.
|
|
614
|
-
this.
|
|
615
|
-
this.
|
|
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.
|
|
704
|
+
if (axeY[i].Y >= this._canvas.height - this._padding.bottom)
|
|
619
705
|
continue;
|
|
620
|
-
this.
|
|
621
|
-
this.
|
|
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.
|
|
710
|
+
helpLinePosY = axeY[axeY.length - 1].Y - Math.round(this._coefVal * this._axeYLabelStep / 2);
|
|
625
711
|
if (helpLinePosY > 0) {
|
|
626
|
-
this.
|
|
627
|
-
this.
|
|
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.
|
|
716
|
+
this._context.stroke();
|
|
631
717
|
// Draw labels on axe Y
|
|
632
|
-
this.
|
|
633
|
-
this.
|
|
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.
|
|
724
|
+
this._context.fillText(axeY[i].value, 0, axeY[i].Y + 10);
|
|
639
725
|
}
|
|
640
726
|
}
|
|
641
727
|
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
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.
|
|
747
|
-
this.
|
|
748
|
-
this.
|
|
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.
|
|
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.
|
|
850
|
+
this._context.lineTo(pos[i].X, pos[i].Y);
|
|
761
851
|
i++;
|
|
762
852
|
} else {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
922
|
+
/**
|
|
923
|
+
* Calculate from time-stamp position X using coefficient for axe X and available width.
|
|
924
|
+
*/
|
|
789
925
|
_convertTs2PosX (ts) {
|
|
790
|
-
return (this.
|
|
926
|
+
return (this._canvas.width - Math.round((this._maxTs - ts) * this._coefTs));
|
|
791
927
|
}
|
|
792
928
|
|
|
793
|
-
|
|
794
|
-
|
|
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.
|
|
934
|
+
return (Math.round((this._maxVal - val) * this._coefVal));
|
|
797
935
|
}
|
|
798
936
|
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
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
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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
|
|
875
|
-
stepsX = [2, 5, 10,
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
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
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
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;
|
|
897
|
-
this.parName = lineData.par_name
|
|
898
|
-
this.objName = lineData.obj_name
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
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
|
+
}
|