adaptive-linechart 1.3.0 → 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.
@@ -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.24.0/moment.min.js"></script>
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.0",
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
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Create simple logger. Used to log posible error by processing data of chart.
7
7
  */
8
- if(typeof logger != "notdefined") {
8
+ if(typeof logger == "undefined") {
9
9
  var logger = {
10
10
  info: function (message) { console.info(message);},
11
11
  warn: function (message) {console.warn(message);},
@@ -118,6 +118,25 @@ function isString(variable) {
118
118
  return typeof variable === 'string' || variable instanceof String;
119
119
  }
120
120
 
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
+
121
140
  /**
122
141
  * Function return object that contains pieces of data-time as strings. Year long, month 01..12,
123
142
  * day of month 01..31, hour 01..23, min 01..59, sec 01..59, msec 001...999
@@ -148,6 +167,18 @@ Date.prototype.separate = function() {
148
167
  return dt;
149
168
  }
150
169
 
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
+
151
182
  /**
152
183
  * Convert CSV with header to array of time-series. Assume first value is data-time as string.
153
184
  * Name of serie take from header. CSV may contains many series (columns). Values may be empty.