chartjs-plugin-trendline 2.0.3 → 2.0.5

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.
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see chartjs-plugin-trendline.min.js.LICENSE.txt */
2
- (()=>{var t={460:(t,i)=>{var e={id:"chartjs-plugin-trendline",afterDatasetsDraw:function(t){var i,e;for(var a in t.scales)if("x"==a[0]?e=t.scales[a]:i=t.scales[a],e&&i)break;var r=t.ctx;t.data.datasets.forEach((function(i,a){var n=i.alwaysShowTrendline||t.isDatasetVisible(a);if(i.trendlineLinear&&n&&i.data.length>1){var o=t.getDatasetMeta(a);!function(t,i,e,a,r){var n=e.borderColor||"rgba(169,169,169, .6)",o=e.trendlineLinear.colorMin||n,h=e.trendlineLinear.colorMax||n,l=e.trendlineLinear.width||e.borderWidth,u=e.trendlineLinear.lineStyle||"solid",d=e.trendlineLinear.fillColor;l=void 0!==l?l:3;var m=new s,x=e.data.findIndex((t=>null!=t)),c=e.data.length-1,f=t.data[x].x,X=t.data[c].x,v="object"==typeof e.data[x];e.data.forEach((function(t,i){if(null!=t)if(["time","timeseries"].includes(a.options.type)){var e=null!=t.x?t.x:t.t;m.add(new Date(e).getTime(),t.y)}else v?isNaN(t.x)||isNaN(t.y)?isNaN(t.x)?isNaN(t.y)||m.add(i,t.y):m.add(i,t.x):m.add(t.x,t.y):m.add(i,t)}));var p,y,g=a.getPixelForValue(m.minx),w=r.getPixelForValue(m.f(m.minx));if(e.trendlineLinear.projection&&m.scale()<0){var Y=m.fo();Y<m.minx&&(Y=m.maxx),p=a.getPixelForValue(Y),y=r.getPixelForValue(m.f(Y))}else p=a.getPixelForValue(m.maxx),y=r.getPixelForValue(m.f(m.maxx));v||(g=f,p=X);var P=t.controller.chart.chartArea.bottom,C=t.controller.chart.width;if(w>P){var L=w-P,b=w-y;w=P,g+=C*(L/b)}else y>P&&(L=y-P,b=y-w,y=P,p=C-(p-(C-C*(L/b))));i.lineWidth=l,"dotted"===u&&i.setLineDash([2,3]),i.beginPath(),i.moveTo(g,w),i.lineTo(p,y);var F=i.createLinearGradient(g,w,p,y);y<w?(F.addColorStop(0,h),F.addColorStop(1,o)):(F.addColorStop(0,o),F.addColorStop(1,h)),i.strokeStyle=F,i.stroke(),i.closePath(),!d||(i.fillStyle=d,i.beginPath(),i.moveTo(g,w),i.lineTo(p,y),i.lineTo(p,P),i.lineTo(g,P),i.closePath(),i.fill())}(o,r,i,e,t.scales[o.yAxisID])}})),r.setLineDash([])}};function s(){this.count=0,this.sumX=0,this.sumX2=0,this.sumXY=0,this.sumY=0,this.minx=1e100,this.maxx=-1e100,this.maxy=-1e100}s.prototype={add:function(t,i){t=parseFloat(t),i=parseFloat(i),this.count++,this.sumX+=t,this.sumX2+=t*t,this.sumXY+=t*i,this.sumY+=i,t<this.minx&&(this.minx=t),t>this.maxx&&(this.maxx=t),i>this.maxy&&(this.maxy=i)},f:function(t){t=parseFloat(t);var i=this.count*this.sumX2-this.sumX*this.sumX;return(this.sumX2*this.sumY-this.sumX*this.sumXY)/i+t*((this.count*this.sumXY-this.sumX*this.sumY)/i)},fo:function(){var t=this.count*this.sumX2-this.sumX*this.sumX;return-(this.sumX2*this.sumY-this.sumX*this.sumXY)/t/((this.count*this.sumXY-this.sumX*this.sumY)/t)},scale:function(){var t=this.count*this.sumX2-this.sumX*this.sumX;return(this.count*this.sumXY-this.sumX*this.sumY)/t}},"undefined"!=typeof window&&window.Chart&&(window.Chart.hasOwnProperty("register")?window.Chart.register(e):window.Chart.plugins.register(e));try{t.exports=e}catch(t){}}},i={};!function e(s){var a=i[s];if(void 0!==a)return a.exports;var r=i[s]={exports:{}};return t[s](r,r.exports,e),r.exports}(460)})();
2
+ (()=>{var t={460:(t,i)=>{var e={id:"chartjs-plugin-trendline",afterDatasetsDraw:function(t){var i,e;for(var a in t.scales)if("x"==a[0]?e=t.scales[a]:i=t.scales[a],e&&i)break;var r=t.ctx;t.data.datasets.forEach((function(i,a){var n=i.alwaysShowTrendline||t.isDatasetVisible(a);if(i.trendlineLinear&&n&&i.data.length>1){var o=t.getDatasetMeta(a);!function(t,i,e,a,r){var n=e.borderColor||"rgba(169,169,169, .6)",o=e.trendlineLinear.colorMin||n,h=e.trendlineLinear.colorMax||n,l=e.trendlineLinear.width||e.borderWidth,u=e.trendlineLinear.lineStyle||"solid",d=e.trendlineLinear.fillColor;l=void 0!==l?l:3;var m=new s,x=e.data.findIndex((t=>null!=t)),c=e.data.length-1,f=t.data[x].x,X=t.data[c].x,v="object"==typeof e.data[x];e.data.forEach((function(t,i){if(null!=t)if(["time","timeseries"].includes(a.options.type)){var e=null!=t.x?t.x:t.t;void 0!==e?m.add(new Date(e).getTime(),t.y):m.add(i,t)}else v?isNaN(t.x)||isNaN(t.y)?isNaN(t.x)?isNaN(t.y)||m.add(i,t.y):m.add(i,t.x):m.add(t.x,t.y):m.add(i,t)}));var p,y,g=a.getPixelForValue(m.minx),w=r.getPixelForValue(m.f(m.minx));if(e.trendlineLinear.projection&&m.scale()<0){var Y=m.fo();Y<m.minx&&(Y=m.maxx),p=a.getPixelForValue(Y),y=r.getPixelForValue(m.f(Y))}else p=a.getPixelForValue(m.maxx),y=r.getPixelForValue(m.f(m.maxx));v||(g=f,p=X);var L=t.controller.chart.chartArea.bottom,P=t.controller.chart.width;if(w>L){var C=w-L,b=w-y;w=L,g+=P*(C/b)}else y>L&&(C=y-L,b=y-w,y=L,p=P-(p-(P-P*(C/b))));i.lineWidth=l,"dotted"===u?i.setLineDash([2,3]):i.setLineDash([]),i.beginPath(),i.moveTo(g,w),i.lineTo(p,y);var D=i.createLinearGradient(g,w,p,y);y<w?(D.addColorStop(0,h),D.addColorStop(1,o)):(D.addColorStop(0,o),D.addColorStop(1,h)),i.strokeStyle=D,i.stroke(),i.closePath(),!d||(i.fillStyle=d,i.beginPath(),i.moveTo(g,w),i.lineTo(p,y),i.lineTo(p,L),i.lineTo(g,L),i.closePath(),i.fill())}(o,r,i,e,t.scales[o.yAxisID])}})),r.setLineDash([])}};function s(){this.count=0,this.sumX=0,this.sumX2=0,this.sumXY=0,this.sumY=0,this.minx=1e100,this.maxx=-1e100,this.maxy=-1e100}s.prototype={add:function(t,i){t=parseFloat(t),i=parseFloat(i),this.count++,this.sumX+=t,this.sumX2+=t*t,this.sumXY+=t*i,this.sumY+=i,t<this.minx&&(this.minx=t),t>this.maxx&&(this.maxx=t),i>this.maxy&&(this.maxy=i)},f:function(t){t=parseFloat(t);var i=this.count*this.sumX2-this.sumX*this.sumX;return(this.sumX2*this.sumY-this.sumX*this.sumXY)/i+t*((this.count*this.sumXY-this.sumX*this.sumY)/i)},fo:function(){var t=this.count*this.sumX2-this.sumX*this.sumX;return-(this.sumX2*this.sumY-this.sumX*this.sumXY)/t/((this.count*this.sumXY-this.sumX*this.sumY)/t)},scale:function(){var t=this.count*this.sumX2-this.sumX*this.sumX;return(this.count*this.sumXY-this.sumX*this.sumY)/t}},"undefined"!=typeof window&&window.Chart&&(window.Chart.hasOwnProperty("register")?window.Chart.register(e):window.Chart.plugins.register(e));try{t.exports=e}catch(t){}}},i={};!function e(s){var a=i[s];if(void 0!==a)return a.exports;var r=i[s]={exports:{}};return t[s](r,r.exports,e),r.exports}(460)})();
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * chartjs-plugin-trendline.js
3
- * Version: 2.0.3
3
+ * Version: 2.0.5
4
4
  *
5
5
  * Copyright 2023 Marcus Alsterfjord
6
6
  * Released under the MIT license
@@ -0,0 +1,72 @@
1
+ <html>
2
+
3
+ <head>
4
+ <title>Time scale chart.js</title>
5
+ </head>
6
+ <style>
7
+ .chartBox {
8
+ width: 700px
9
+ }
10
+ </style>
11
+
12
+ <body>
13
+ <div class="chartBox">
14
+ <canvas id="myChart"></canvas>
15
+ </div>
16
+
17
+ <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
18
+
19
+ <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
20
+
21
+ <script src="./../src/chartjs-plugin-trendline.js"></script>
22
+
23
+ <script>
24
+ const data = {
25
+ "labels": [
26
+ "2023-08-10 20:00:00",
27
+ "2023-08-10 21:30:00",
28
+ "2023-08-10 22:30:30",
29
+ "2023-08-11 07:00:00",
30
+ "2023-08-11 07:15:00",
31
+ "2023-08-11 08:00:00"
32
+ ],
33
+ "datasets": [
34
+ {
35
+ "type": "line",
36
+ "label": "Blood Sugar",
37
+ "data": {
38
+ "2023-08-10 21:30:00": "6.20",
39
+ "2023-08-10 22:30:30": "6.60",
40
+ "2023-08-11 07:00:00": "5.90",
41
+ "2023-08-11 08:00:00": "4.60",
42
+ "2023-08-11 11:17:00": "5.10",
43
+ "2023-08-11 12:15:00": "4.90"
44
+ },
45
+ "backgroundColor": "#A9CCE3",
46
+ "pointStyle": "circle",
47
+ "pointRadius": 6,
48
+ "pointHoverRadius": 12,
49
+ "borderColor": "#5DADE2",
50
+ "trendlineLinear": {
51
+ "colorMin": "red",
52
+ "colorMax": "green",
53
+ "lineStyle": "dotted",
54
+ "width": 3,
55
+ "projection": true
56
+ }
57
+ }
58
+ ],
59
+ };
60
+ const config = {
61
+ type: 'line',
62
+ data
63
+ };
64
+ console.log("Loaded");
65
+ const myChart = new Chart(
66
+ document.getElementById('myChart'),
67
+ config,
68
+ );
69
+ </script>
70
+ </body>
71
+
72
+ </html>
@@ -0,0 +1,111 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
+ <title>LineChart Example</title>
8
+ <script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.0/dist/chart.umd.js"></script>
9
+ <script src="./../src/chartjs-plugin-trendline.js"></script>
10
+ <script>
11
+ let data = {
12
+ "labels": [
13
+ 202303,
14
+ 202304,
15
+ 202305,
16
+ 202306,
17
+ 202307,
18
+ 202308,
19
+ 202309
20
+ ],
21
+ "datasets": [
22
+ {
23
+ "label": "Dataset 1",
24
+ "data": [
25
+ 10,
26
+ 30,
27
+ 50,
28
+ 20,
29
+ 25,
30
+ 44,
31
+ -10
32
+ ],
33
+ "borderColor": "#4dc9f6",
34
+ "backgroundColor": "#4dc9f6",
35
+ "trendlineLinear": {
36
+ "colorMin": "#4dc9f6",
37
+ "colorMax": "#4dc9f6",
38
+ "lineStyle": "dotted",
39
+ "width": 2,
40
+ "projection": false
41
+ }
42
+ },
43
+ {
44
+ "label": "Dataset 2",
45
+ "data": [
46
+ 100,
47
+ 33,
48
+ 22,
49
+ 19,
50
+ 11,
51
+ 49,
52
+ 200
53
+ ],
54
+ "borderColor": "#f67019",
55
+ "backgroundColor": "#f67019",
56
+ "trendlineLinear": {
57
+ "colorMin": "#f67019",
58
+ "colorMax": "#f67019",
59
+ "lineStyle": "solid",
60
+ "width": 2,
61
+ "projection": false
62
+ }
63
+ }
64
+ ]
65
+ };
66
+
67
+ var lineOptions = {
68
+ plugins: {
69
+ legend: {
70
+ display: true,
71
+ labels: {
72
+ fontSize: 10,
73
+ fontColor: "#000",
74
+ },
75
+ },
76
+ },
77
+ scales: {
78
+ x:
79
+ {
80
+ gridLines: {
81
+ display: true,
82
+ },
83
+ display: true,
84
+ color: "rgba(255, 255, 255, 0.1)",
85
+ },
86
+ y:
87
+ {
88
+ display: false,
89
+ },
90
+ },
91
+ responsive: true,
92
+ maintainAspectRatio: true,
93
+ };
94
+ document.addEventListener("DOMContentLoaded", function(event) {
95
+ new Chart(document.getElementById("line-chart"), {
96
+ type: 'line',
97
+ data: data,
98
+ options:lineOptions
99
+ });
100
+ });
101
+ </script>
102
+ </head>
103
+ <body>
104
+ <h1>Line Chart</h1>
105
+
106
+ <div style="width: 800px;">
107
+ <canvas id="line-chart"></canvas>
108
+ </div>
109
+
110
+ </body>
111
+ </html>
@@ -0,0 +1,67 @@
1
+ <html>
2
+
3
+ <head>
4
+ <title>Time scale chart.js</title>
5
+ </head>
6
+ <style>
7
+ .chartBox {
8
+ width: 700px
9
+ }
10
+ </style>
11
+
12
+ <body>
13
+ <div class="chartBox">
14
+ <canvas id="myChart"></canvas>
15
+ </div>
16
+
17
+ <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
18
+
19
+ <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
20
+
21
+ <script src="./../src/chartjs-plugin-trendline.js"></script>
22
+
23
+ <script>
24
+ const data = {
25
+ labels: [
26
+ "2023-01-01T08:00:00.000Z",
27
+ "2023-01-02T08:00:00.000Z",
28
+ "2023-01-03T08:00:00.000Z",
29
+ "2023-01-04T08:00:00.000Z",
30
+ ],
31
+ datasets: [{
32
+ label: "Default",
33
+ data: [65, 59, 80, 81],
34
+ trendlineLinear: {
35
+ colorMin: "red",
36
+ colorMax: "green",
37
+ lineStyle: "dotted",
38
+ width: 2,
39
+ projection: false,
40
+ },
41
+ }],
42
+ };
43
+ const config = {
44
+ type: 'line',
45
+ data,
46
+ options: {
47
+ scales: {
48
+ x: {
49
+ type: 'time',
50
+ time: {
51
+ unit: 'day',
52
+ },
53
+ },
54
+ y: {
55
+ beginAtZero: true
56
+ }
57
+ }
58
+ }
59
+ };
60
+ const myChart = new Chart(
61
+ document.getElementById('myChart'),
62
+ config,
63
+ );
64
+ </script>
65
+ </body>
66
+
67
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chartjs-plugin-trendline",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Trendline for Chart.js",
5
5
  "main": "src/chartjs-plugin-trendline.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/Makanz/chartjs-plugin-trendline#readme",
19
19
  "devDependencies": {
20
- "webpack": "^5.83.1",
21
- "webpack-cli": "^5.1.1"
20
+ "webpack": "^5.88.2",
21
+ "webpack-cli": "^5.1.4"
22
22
  }
23
23
  }
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * chartjs-plugin-trendline.js
3
- * Version: 2.0.3
3
+ * Version: 2.0.5
4
4
  *
5
5
  * Copyright 2023 Marcus Alsterfjord
6
6
  * Released under the MIT license
@@ -25,6 +25,7 @@ var pluginTrendlineLinear = {
25
25
  dataset.alwaysShowTrendline ||
26
26
  chartInstance.isDatasetVisible(index);
27
27
 
28
+ // TODO: Add support for objects
28
29
  if (
29
30
  dataset.trendlineLinear &&
30
31
  showTrendline &&
@@ -65,11 +66,16 @@ function addFitter(datasetMeta, ctx, dataset, xScale, yScale) {
65
66
  var xy = typeof dataset.data[firstIndex] === 'object';
66
67
 
67
68
  dataset.data.forEach(function (data, index) {
69
+ console.log('forEach');
68
70
  if (data == null) return;
69
71
 
70
72
  if (['time', 'timeseries'].includes(xScale.options.type)) {
71
73
  var x = data.x != null ? data.x : data.t;
72
- fitter.add(new Date(x).getTime(), data.y);
74
+ if (x !== undefined) {
75
+ fitter.add(new Date(x).getTime(), data.y);
76
+ } else {
77
+ fitter.add(index, data);
78
+ }
73
79
  } else if (xy) {
74
80
  if (!isNaN(data.x) && !isNaN(data.y)) {
75
81
  fitter.add(data.x, data.y);
@@ -132,9 +138,13 @@ function addFitter(datasetMeta, ctx, dataset, xScale, yScale) {
132
138
  }
133
139
 
134
140
  ctx.lineWidth = lineWidth;
141
+
135
142
  if (lineStyle === 'dotted') {
136
- ctx.setLineDash([2, 3]);
143
+ ctx.setLineDash([2, 3]); // Dotted
144
+ } else {
145
+ ctx.setLineDash([]); // Solid
137
146
  }
147
+
138
148
  ctx.beginPath();
139
149
  ctx.moveTo(x1, y1);
140
150
  ctx.lineTo(x2, y2);
@@ -1,73 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
- <title>LineChart Example</title>
8
- <script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.0/dist/chart.umd.js"></script>
9
- <script src="./../src/chartjs-plugin-trendline.js"></script>
10
- <script>
11
- document.addEventListener("DOMContentLoaded", function(event) {
12
- new Chart(document.getElementById("line-chart"), {
13
- type: 'line',
14
- data: {
15
- labels: [1500,1600,1700,1750,1800,1850,1900,1950,1999,2050],
16
- datasets: [{
17
- data: [86,114,106,106,107,111,133,221,783,2478],
18
- label: "Africa",
19
- borderColor: "#3e95cd",
20
- fill: false,
21
- trendlineLinear: {
22
- colorMin: "#3e95cd",
23
- lineStyle: "line",
24
- width: 1
25
- }
26
- }, {
27
- data: [282,350,411,502,635,809,947,1402,3700,5267],
28
- label: "Asia",
29
- borderColor: "#8e5ea2",
30
- fill: false,
31
- trendlineLinear: {
32
- colorMin: "red",
33
- colorMax: "green",
34
- lineStyle: "line",
35
- width: 1
36
- }
37
- }, {
38
- data: [168,170,178,190,203,276,408,547,675,734],
39
- label: "Europe",
40
- borderColor: "#3cba9f",
41
- fill: false
42
- }, {
43
- data: [40,20,10,16,24,38,74,167,508,784],
44
- label: "Latin America",
45
- borderColor: "#e8c3b9",
46
- fill: false
47
- }, {
48
- data: [6,3,2,2,7,26,82,172,312,433],
49
- label: "North America",
50
- borderColor: "#c45850",
51
- fill: false
52
- }
53
- ]
54
- },
55
- options: {
56
- title: {
57
- display: true,
58
- text: 'World population per region (in millions)'
59
- }
60
- }
61
- });
62
- });
63
- </script>
64
- </head>
65
- <body>
66
- <h1>Line Chart</h1>
67
-
68
- <div style="width: 800px;">
69
- <canvas id="line-chart"></canvas>
70
- </div>
71
-
72
- </body>
73
- </html>