adaptive-linechart 1.2.2 → 1.4.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 +24 -0
- package/DEVELOPER.md +37 -0
- package/README.md +210 -101
- package/build.js +36 -0
- package/dist/linechart-all.min.js +1 -2
- package/dist/linechart.css +1 -1
- package/example/example2.csv +745 -0
- package/example/index.html +85 -2
- package/package.json +7 -1
- package/src/chart-util.js +79 -32
- package/src/linechart.css +1 -1
- package/src/linechart.js +668 -363
- package/tools/updoc.js +35 -0
- package/example/example.js +0 -84
- /package/example/{example.csv → example1.csv} +0 -0
package/example/index.html
CHANGED
|
@@ -6,12 +6,95 @@
|
|
|
6
6
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
|
7
7
|
<title>Example LineCharts</title>
|
|
8
8
|
<link type="text/css" rel="stylesheet" href="../dist/linechart.css" >
|
|
9
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.
|
|
9
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.31.0/moment.min.js"></script>
|
|
10
10
|
<script src="../dist/linechart-all.min.js"></script>
|
|
11
|
-
<script src="example.js"></script>
|
|
12
11
|
</head>
|
|
13
12
|
|
|
14
13
|
<body>
|
|
15
14
|
<div id="charts" style="width: 99%;"> </div>
|
|
15
|
+
<script>
|
|
16
|
+
var isMobil = false;
|
|
17
|
+
var charts = [];
|
|
18
|
+
|
|
19
|
+
(function () {
|
|
20
|
+
let parent = document.getElementById('charts');
|
|
21
|
+
|
|
22
|
+
let lchart = new LineChart(parent, null, null,
|
|
23
|
+
'Example with numerical values on axis-x', null, 'number');
|
|
24
|
+
charts.push(lchart);
|
|
25
|
+
|
|
26
|
+
let lineData1 = { obj_name: 'Line one', values: '' };
|
|
27
|
+
for (let i1 = 0; i1 < 91; i1++) {
|
|
28
|
+
lineData1.values += i1 + '/' + (i1 * Math.cos(i1)) + ';';
|
|
29
|
+
}
|
|
30
|
+
lineData1.values = lineData1.values.substring(0, lineData1.values.length - 1);
|
|
31
|
+
lchart.addLine(lineData1);
|
|
32
|
+
|
|
33
|
+
let lineData2 = { obj_name: 'Line two', values: '' };
|
|
34
|
+
for (let i2 = 0; i2 < 90; i2++) {
|
|
35
|
+
lineData2.values += i2 + '/' + (i2 * Math.cos(i2) * 2) + ';';
|
|
36
|
+
}
|
|
37
|
+
lineData2.values = lineData2.values.substring(0, lineData2.values.length - 1);
|
|
38
|
+
lchart.addLine(lineData2);
|
|
39
|
+
|
|
40
|
+
lchart.draw();
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
fetch("example1.csv")
|
|
44
|
+
.then(function(response) {
|
|
45
|
+
if (!response.ok) throw new Error('HTTP '+ response.status);
|
|
46
|
+
return response.text();
|
|
47
|
+
})
|
|
48
|
+
.then(function(csv) {
|
|
49
|
+
let arScvData = scvToTimeSeries(csv);
|
|
50
|
+
lchart = new LineChart(document.getElementById('charts'),null, null,
|
|
51
|
+
"Example data from CSV and data-time on axis-x.", null, "time");
|
|
52
|
+
charts.push(lchart);
|
|
53
|
+
for(let i=0; i < arScvData.length - 1; i++) {
|
|
54
|
+
let line = {
|
|
55
|
+
obj_name: 'Stock preis',
|
|
56
|
+
par_name: arScvData[i].name,
|
|
57
|
+
values: arScvData[i].data
|
|
58
|
+
};
|
|
59
|
+
lchart.addLine(line);
|
|
60
|
+
}
|
|
61
|
+
lchart.draw();
|
|
62
|
+
}).catch(function(error){
|
|
63
|
+
document.body.insertAdjacentHTML('beforeend',
|
|
64
|
+
'<p style="color:red">Error: ' + error.message + '</p>');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
fetch('example2.csv')
|
|
68
|
+
.then(function(response) {
|
|
69
|
+
if (!response.ok) throw new Error('HTTP '+ response.status);
|
|
70
|
+
return response.text();
|
|
71
|
+
})
|
|
72
|
+
.then(function (csv) {
|
|
73
|
+
let values = scvToTimeSeries(csv);
|
|
74
|
+
let parent = document.getElementById('charts');
|
|
75
|
+
|
|
76
|
+
let line = {
|
|
77
|
+
obj_name: 'Stock preis',
|
|
78
|
+
par_name: values[0].name,
|
|
79
|
+
values: values[0].data
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let connected = new LineChart(parent, null, null,
|
|
83
|
+
'Example IfDataMissing="connect": line bridges the missing values.', null, 'time', null);
|
|
84
|
+
connected.addLine(line);
|
|
85
|
+
connected.draw();
|
|
86
|
+
charts.push(connected);
|
|
87
|
+
|
|
88
|
+
let noline = new LineChart(parent, null, null,
|
|
89
|
+
'Example IfDataMissing="noline": line is interrupted at missing values.', null, 'time', 'noline');
|
|
90
|
+
noline.addLine(line);
|
|
91
|
+
noline.draw();
|
|
92
|
+
charts.push(noline);
|
|
93
|
+
})
|
|
94
|
+
.catch(function (error) {
|
|
95
|
+
document.body.insertAdjacentHTML('beforeend',
|
|
96
|
+
'<p style="color:red">Error: ' + error.message + '</p>');
|
|
97
|
+
});
|
|
98
|
+
</script>
|
|
16
99
|
</body>
|
|
17
100
|
</html>
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptive-linechart",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Independent library to draw line-chart in browser.",
|
|
5
5
|
"scripts": {
|
|
6
|
+
"build": "node build.js",
|
|
7
|
+
"updoc": "node tools/updoc.js",
|
|
6
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
9
|
},
|
|
8
10
|
"browser": "dist/linechart-all.min.js",
|
|
@@ -17,5 +19,9 @@
|
|
|
17
19
|
"repository": {
|
|
18
20
|
"type": "git",
|
|
19
21
|
"url": "git+https://gitlab.com/softdorado/linechart.git"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"terser": "^5.31.0",
|
|
25
|
+
"jsdoc-to-markdown": "^8.0.0"
|
|
20
26
|
}
|
|
21
27
|
}
|
package/src/chart-util.js
CHANGED
|
@@ -2,25 +2,30 @@
|
|
|
2
2
|
// Collection of small utilities
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Create simple logger. Used to log posible error by processing data of chart.
|
|
7
|
+
*/
|
|
8
|
+
if(typeof logger == "undefined") {
|
|
7
9
|
var logger = {
|
|
8
10
|
info: function (message) { console.info(message);},
|
|
9
11
|
warn: function (message) {console.warn(message);},
|
|
10
12
|
error: function (message) {console.error(message);},
|
|
11
|
-
debug: function (message) {console.debug(
|
|
13
|
+
debug: function (message) {console.debug(message);}
|
|
12
14
|
};
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Add new element with predefined attributes and return reference to new element.
|
|
19
|
+
* @param reference to parent element. If null create new element in <body>.
|
|
20
|
+
* @param tag String. Tag of new element. Default <div>
|
|
21
|
+
* @param attrSet Object. Contains sub-elements. Each is one attribute of DOM element.
|
|
22
|
+
* id - String. ID of new element. If not defined or is not unique create random.
|
|
23
|
+
* innerText - String. If defined add as innerText.
|
|
24
|
+
* innerHTML - String- If defined add as innerHTML.
|
|
25
|
+
* @return reference to DOM element.
|
|
26
|
+
* @example
|
|
27
|
+
* var n = addNewElement(x, y, {id:'chart_legend_item', style: 'display:none; padding:10px;'});
|
|
28
|
+
*/
|
|
24
29
|
function addNewElement(parent, tag, attrSet) {
|
|
25
30
|
if (attrSet == null) attrSet = {};
|
|
26
31
|
|
|
@@ -61,14 +66,18 @@ function addNewElement(parent, tag, attrSet) {
|
|
|
61
66
|
return document.getElementById(attrSet.id);
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Counter that used in function addNewElement() to generate unique id
|
|
71
|
+
*/
|
|
65
72
|
var newElementCounter = 0;
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Calculate average value from array and return float with defined precision if at least
|
|
76
|
+
* one item in array is a number else return NaN.
|
|
77
|
+
* @param arData - Array. Array of numbers.
|
|
78
|
+
* @param precision - Integer. Default 2 digits.
|
|
79
|
+
* @return float or NaN.
|
|
80
|
+
*/
|
|
72
81
|
function getAvgValue (arData, precision) {
|
|
73
82
|
|
|
74
83
|
if(arData == null || arData.length == 0) return NaN;
|
|
@@ -92,18 +101,46 @@ function getAvgValue (arData, precision) {
|
|
|
92
101
|
}
|
|
93
102
|
}
|
|
94
103
|
|
|
95
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Get random integer with 16 digits.
|
|
106
|
+
* @returns number
|
|
107
|
+
*/
|
|
96
108
|
function getRandomInt () {
|
|
97
109
|
return Math.random() * 100000000000000000;
|
|
98
|
-
}
|
|
110
|
+
}
|
|
99
111
|
|
|
100
|
-
|
|
112
|
+
/**
|
|
113
|
+
* If variable is string return true, else false.
|
|
114
|
+
* @param variable {any}
|
|
115
|
+
* @returns boolean
|
|
116
|
+
*/
|
|
101
117
|
function isString(variable) {
|
|
102
118
|
return typeof variable === 'string' || variable instanceof String;
|
|
103
119
|
}
|
|
104
120
|
|
|
105
|
-
|
|
106
|
-
|
|
121
|
+
/**
|
|
122
|
+
* Round a value to a given number of decimal places. Removes floating point
|
|
123
|
+
* noise, e.g. 1.4 + 0.2 = 1.5999999999999999 -> 1.6.
|
|
124
|
+
* @param value {number} number to round. NaN/null are returned unchanged.
|
|
125
|
+
* @param decimals {number} amount of decimal places. Default 0.
|
|
126
|
+
* @returns {number}
|
|
127
|
+
*/
|
|
128
|
+
function roundTo(value, decimals) {
|
|
129
|
+
|
|
130
|
+
if (value == null || isNaN(value))
|
|
131
|
+
return value;
|
|
132
|
+
|
|
133
|
+
if (decimals == null || decimals < 0)
|
|
134
|
+
decimals = 0;
|
|
135
|
+
|
|
136
|
+
var factor = Math.pow(10, decimals);
|
|
137
|
+
return Math.round(value * factor) / factor;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Function return object that contains pieces of data-time as strings. Year long, month 01..12,
|
|
142
|
+
* day of month 01..31, hour 01..23, min 01..59, sec 01..59, msec 001...999
|
|
143
|
+
*/
|
|
107
144
|
Date.prototype.separate = function() {
|
|
108
145
|
|
|
109
146
|
var dt = {};
|
|
@@ -128,12 +165,26 @@ Date.prototype.separate = function() {
|
|
|
128
165
|
}
|
|
129
166
|
|
|
130
167
|
return dt;
|
|
131
|
-
}
|
|
168
|
+
}
|
|
132
169
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Return the date-time in local time as ISO-like string "YYYY-MM-DD HH:MM:SS".
|
|
172
|
+
* Standard Date has no comparable function (toISOString() uses UTC), so this
|
|
173
|
+
* extension adds the local variant. Used e.g. for log messages.
|
|
174
|
+
*/
|
|
175
|
+
Date.prototype.formatISOLocal = function() {
|
|
176
|
+
|
|
177
|
+
var dt = this.separate();
|
|
178
|
+
|
|
179
|
+
return dt.year + '-' + dt.month + '-' + dt.mday + ' ' + dt.hour + ':' + dt.min + ':' + dt.sec;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Convert CSV with header to array of time-series. Assume first value is data-time as string.
|
|
184
|
+
* Name of serie take from header. CSV may contains many series (columns). Values may be empty.
|
|
185
|
+
* Each serie is object {name: "Name of serie", data: "Unix-Timestamp/Value;Unix-Timestam/Value"}
|
|
186
|
+
* This function need package Moment.js https://momentjs.com
|
|
187
|
+
*/
|
|
137
188
|
function scvToTimeSeries(csv) {
|
|
138
189
|
|
|
139
190
|
let arResult = [];
|
|
@@ -179,7 +230,3 @@ function scvToTimeSeries(csv) {
|
|
|
179
230
|
|
|
180
231
|
return arResult;
|
|
181
232
|
}
|
|
182
|
-
|
|
183
|
-
//
|
|
184
|
-
// Extentions existing types
|
|
185
|
-
//
|