chartjs-plugin-trendline 0.2.2 → 1.0.3

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/.prettierrc ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "trailingComma": "es5",
3
+ "tabWidth": 4,
4
+ "semi": true,
5
+ "singleQuote": true
6
+ }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # chartjs-plugin-trendline
2
2
 
3
- This plugin draws an linear trendline in your Chart. Made for Chart.js > 2.0
3
+ This plugin draws an linear trendline in your Chart. Made for Chart.js 3.X
4
4
 
5
5
  ## Installation
6
6
 
@@ -9,7 +9,7 @@ This plugin draws an linear trendline in your Chart. Made for Chart.js > 2.0
9
9
  Load ChartJS first, then the plugin which will automatically register itself with ChartJS
10
10
 
11
11
  ```html
12
- <script src="https://cdn.jsdelivr.net/npm/chart.js/dist/Chart.min.js"></script>
12
+ <script src="https://cdn.jsdelivr.net/npm/chart.js@3.8.0/dist/chart.min.js"></script>
13
13
  <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline"></script>
14
14
  ```
15
15
 
@@ -35,7 +35,8 @@ To configure the trendline plugin you simply add a new config options to your da
35
35
  trendlineLinear: {
36
36
  style: "rgba(255,105,180, .8)",
37
37
  lineStyle: "dotted|solid",
38
- width: 2
38
+ width: 2,
39
+ projection: true|false (optional)
39
40
  }
40
41
  }
41
42
  ```
@@ -1 +1,2 @@
1
- var pluginTrendlineLinear={id:"trendlineLinear",afterDraw:function(a){var t,s;for(var e in a.scales)if("x"==e[0]?s=a.scales[e]:t=a.scales[e],s&&t)break;var n=a.chart.ctx;a.data.datasets.forEach(function(t,e){if(t.trendlineLinear&&a.isDatasetVisible(e)&&0!=t.data.length){var i=a.getDatasetMeta(e);addFitter(i,n,t,s,a.scales[i.yAxisID])}}),n.setLineDash([])}};function addFitter(t,e,i,a,s){var n=i.trendlineLinear.style||i.borderColor,r=i.trendlineLinear.width||i.borderWidth,o=i.trendlineLinear.lineStyle||"solid";n=void 0!==n?n:"rgba(169,169,169, .6)",r=void 0!==r?r:3;var l=new LineFitter,d=i.data.length-1,h=t.data[0]._model.x,u=t.data[d]._model.x,m=!1;i.data&&"object"==typeof i.data[0]&&(m=!0),i.data.forEach(function(t,e){if(null!=t)if("time"===a.options.type){var i=null!=t.x?t.x:t.t;l.add(new Date(i).getTime(),t.y)}else m?l.add(t.x,t.y):l.add(e,t)});var x=a.getPixelForValue(l.minx),c=a.getPixelForValue(l.maxx),f=s.getPixelForValue(l.f(l.minx)),g=s.getPixelForValue(l.f(l.maxx));m||(x=h,c=u);var p=t.controller.chart.chartArea.bottom,v=t.controller.chart.width;if(p<f){var X=f-p,L=f-g;f=p,x+=v*(X/L)}else if(p<g){X=g-p,L=g-f;g=p,c=v-(c-(v-v*(X/L)))}e.lineWidth=r,"dotted"===o&&e.setLineDash([2,3]),e.beginPath(),e.moveTo(x,f),e.lineTo(c,g),e.strokeStyle=n,e.stroke()}function LineFitter(){this.count=0,this.sumX=0,this.sumX2=0,this.sumXY=0,this.sumY=0,this.minx=1e100,this.maxx=-1e100}LineFitter.prototype={add:function(t,e){t=parseFloat(t),e=parseFloat(e),this.count++,this.sumX+=t,this.sumX2+=t*t,this.sumXY+=t*e,this.sumY+=e,t<this.minx&&(this.minx=t),t>this.maxx&&(this.maxx=t)},f:function(t){t=parseFloat(t);var e=this.count*this.sumX2-this.sumX*this.sumX;return(this.sumX2*this.sumY-this.sumX*this.sumXY)/e+t*((this.count*this.sumXY-this.sumX*this.sumY)/e)}},"undefined"!=typeof window&&window.Chart&&window.Chart.plugins.register(pluginTrendlineLinear);try{module.exports=exports=pluginTrendlineLinear}catch(t){}
1
+ /*! For license information please see chartjs-plugin-trendline.min.js.LICENSE.txt */
2
+ (()=>{var t={460:(t,s)=>{var i={id:"chartjs-plugin-trendline",afterDatasetsDraw:function(t){var s,i;for(var a in t.scales)if("x"==a[0]?i=t.scales[a]:s=t.scales[a],i&&s)break;var r=t.ctx;t.data.datasets.forEach((function(s,a){if(s.trendlineLinear&&t.isDatasetVisible(a)&&0!=s.data.length){var n=t.getDatasetMeta(a);!function(t,s,i,a,r){var n=i.trendlineLinear.style||i.borderColor,h=i.trendlineLinear.width||i.borderWidth,o=i.trendlineLinear.lineStyle||"solid";n=void 0!==n?n:"rgba(169,169,169, .6)",h=void 0!==h?h:3;var u=new e,l=i.data.findIndex((t=>null!=t)),m=i.data.length-1,d=t.data[l].x,x=t.data[m].x,c="object"==typeof i.data[l];i.data.forEach((function(t,s){if(null!=t)if("time"===a.options.type){var i=null!=t.x?t.x:t.t;u.add(new Date(i).getTime(),t.y)}else c?u.add(t.x,t.y):u.add(s,t)}));var f,X,v=a.getPixelForValue(u.minx),p=r.getPixelForValue(u.f(u.minx));if(i.trendlineLinear.projection&&u.scale()<0){var g=u.fo();g<u.minx&&(g=u.maxx),f=a.getPixelForValue(g),X=r.getPixelForValue(u.f(g))}else f=a.getPixelForValue(u.maxx),X=r.getPixelForValue(u.f(u.maxx));c||(v=d,f=x);var w=t.controller.chart.chartArea.bottom,y=t.controller.chart.width;if(p>w){var Y=p-w,F=p-X;p=w,v+=y*(Y/F)}else X>w&&(Y=X-w,F=X-p,X=w,f=y-(f-(y-y*(Y/F))));s.lineWidth=h,"dotted"===o&&s.setLineDash([2,3]),s.beginPath(),s.moveTo(v,p),s.lineTo(f,X),s.strokeStyle=n,s.stroke()}(n,r,s,i,t.scales[n.yAxisID])}})),r.setLineDash([])}};function e(){this.count=0,this.sumX=0,this.sumX2=0,this.sumXY=0,this.sumY=0,this.minx=1e100,this.maxx=-1e100,this.maxy=-1e100}e.prototype={add:function(t,s){t=parseFloat(t),s=parseFloat(s),this.count++,this.sumX+=t,this.sumX2+=t*t,this.sumXY+=t*s,this.sumY+=s,t<this.minx&&(this.minx=t),t>this.maxx&&(this.maxx=t),s>this.maxy&&(this.maxy=s)},f:function(t){t=parseFloat(t);var s=this.count*this.sumX2-this.sumX*this.sumX;return(this.sumX2*this.sumY-this.sumX*this.sumXY)/s+t*((this.count*this.sumXY-this.sumX*this.sumY)/s)},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(i):window.Chart.plugins.register(i));try{t.exports=i}catch(t){}}},s={};!function i(e){var a=s[e];if(void 0!==a)return a.exports;var r=s[e]={exports:{}};return t[e](r,r.exports,i),r.exports}(460)})();
@@ -0,0 +1,10 @@
1
+ /*!
2
+ * chartjs-plugin-trendline.js
3
+ * Version: 1.0.3
4
+ *
5
+ * Copyright 2022 Marcus Alsterfjord
6
+ * Released under the MIT license
7
+ * https://github.com/Makanz/chartjs-plugin-trendline/blob/master/README.md
8
+ *
9
+ * Mod by: vesal: accept also xy-data so works with scatter
10
+ */
@@ -5,8 +5,8 @@
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
7
  <title>BarChart Example</title>
8
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
9
- <script src="../src/chartjs-plugin-trendline.js"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/chart.js@3.8.0/dist/chart.min.js"></script>
9
+ <script src="./../src/chartjs-plugin-trendline.js"></script>
10
10
  <script>
11
11
  document.addEventListener("DOMContentLoaded", function(event) {
12
12
  // Bar chart
@@ -5,8 +5,8 @@
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
7
  <title>LineChart Example</title>
8
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
9
- <script src="../src/chartjs-plugin-trendline.js"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/chart.js@3.8.0/dist/chart.min.js"></script>
9
+ <script src="./../src/chartjs-plugin-trendline.js"></script>
10
10
  <script>
11
11
  document.addEventListener("DOMContentLoaded", function(event) {
12
12
  new Chart(document.getElementById("line-chart"), {
@@ -0,0 +1,55 @@
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>XYlineChart Projection Example</title>
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.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
+ datasets: [{
16
+ data: [{x: 0, y: 30}, {x: 5, y:25}, {x: 10, y: 23}],
17
+ label: "Count",
18
+ borderColor: "#3e95cd",
19
+ fill: false,
20
+ trendlineLinear: {
21
+ style: "#3e95cd",
22
+ lineStyle: "line",
23
+ width: 1,
24
+ projection: true
25
+ }
26
+ }, {
27
+ data: [{x: 60, y: 0}],
28
+ borderColor: "black",
29
+ label: "Goal",
30
+ fill: false
31
+ }]
32
+ },
33
+ options: {
34
+ title: {
35
+ display: true,
36
+ text: 'Cigarettes per days',
37
+ },
38
+ maintainAspectRatio: false,
39
+ responsive: true,
40
+ scales: {
41
+ xAxes: [{ type: 'linear', position: 'bottom', scaleLabel: { labelString: 'days', display: true}}],
42
+ yAxes: [{ type: 'linear', position: 'left', scaleLabel: { labelString: 'cigarretts/day', display: true}, display: true }],
43
+ }
44
+ }
45
+ });
46
+ });
47
+ </script>
48
+ </head>
49
+ <body>
50
+ <h1>X/Y Chart with trendline projection on x axis</h1>
51
+ <canvas id="line-chart" width="600" height="450"></canvas>
52
+
53
+ <p>Using example code from <a href="http://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/" target="_blank">tobiasahlin.com.</a></p>
54
+ </body>
55
+ </html>
package/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "chartjs-plugin-trendline",
3
- "version": "0.2.2",
3
+ "version": "1.0.3",
4
4
  "description": "Trendline for Chart.js",
5
5
  "main": "src/chartjs-plugin-trendline.js",
6
6
  "scripts": {
7
- "build": "gulp",
8
- "bump-minor": "gulp bump-minor",
9
- "bump-patch": "gulp bump-patch"
7
+ "build": "webpack --mode=production"
10
8
  },
11
9
  "repository": {
12
10
  "type": "git",
@@ -19,11 +17,7 @@
19
17
  },
20
18
  "homepage": "https://github.com/Makanz/chartjs-plugin-trendline#readme",
21
19
  "devDependencies": {
22
- "del": "^6.0.0",
23
- "gulp": "^4.0.2",
24
- "gulp-bump": "^3.2.0",
25
- "gulp-rename": "^2.0.0",
26
- "gulp-uglify": "^3.0.2",
27
- "pump": "^3.0.0"
20
+ "webpack": "^5.73.0",
21
+ "webpack-cli": "^4.10.0"
28
22
  }
29
23
  }
@@ -1,101 +1,128 @@
1
1
  /*!
2
2
  * chartjs-plugin-trendline.js
3
- * Version: 0.2.2
3
+ * Version: 1.0.3
4
4
  *
5
- * Copyright 2020 Marcus Alsterfjord
5
+ * Copyright 2022 Marcus Alsterfjord
6
6
  * Released under the MIT license
7
7
  * https://github.com/Makanz/chartjs-plugin-trendline/blob/master/README.md
8
8
  *
9
9
  * Mod by: vesal: accept also xy-data so works with scatter
10
10
  */
11
11
  var pluginTrendlineLinear = {
12
- id: "trendlineLinear",
13
- afterDraw: function(chartInstance) {
12
+ id: 'chartjs-plugin-trendline',
13
+ afterDatasetsDraw: function (chartInstance) {
14
14
  var yScale;
15
15
  var xScale;
16
16
  for (var axis in chartInstance.scales) {
17
- if ( axis[0] == 'x')
18
- xScale = chartInstance.scales[axis];
19
- else
20
- yScale = chartInstance.scales[axis];
21
- if ( xScale && yScale ) break;
17
+ if (axis[0] == 'x') xScale = chartInstance.scales[axis];
18
+ else yScale = chartInstance.scales[axis];
19
+ if (xScale && yScale) break;
22
20
  }
23
- var ctx = chartInstance.chart.ctx;
24
-
25
- chartInstance.data.datasets.forEach(function(dataset, index) {
26
- if (dataset.trendlineLinear && chartInstance.isDatasetVisible(index) && dataset.data.length != 0) {
21
+ var ctx = chartInstance.ctx;
22
+
23
+ chartInstance.data.datasets.forEach(function (dataset, index) {
24
+ if (
25
+ dataset.trendlineLinear &&
26
+ chartInstance.isDatasetVisible(index) &&
27
+ dataset.data.length != 0
28
+ ) {
27
29
  var datasetMeta = chartInstance.getDatasetMeta(index);
28
- addFitter(datasetMeta, ctx, dataset, xScale, chartInstance.scales[datasetMeta.yAxisID]);
30
+ addFitter(
31
+ datasetMeta,
32
+ ctx,
33
+ dataset,
34
+ xScale,
35
+ chartInstance.scales[datasetMeta.yAxisID]
36
+ );
29
37
  }
30
38
  });
31
39
 
32
40
  ctx.setLineDash([]);
33
- }
41
+ },
34
42
  };
35
43
 
36
44
  function addFitter(datasetMeta, ctx, dataset, xScale, yScale) {
37
45
  var style = dataset.trendlineLinear.style || dataset.borderColor;
38
46
  var lineWidth = dataset.trendlineLinear.width || dataset.borderWidth;
39
- var lineStyle = dataset.trendlineLinear.lineStyle || "solid";
47
+ var lineStyle = dataset.trendlineLinear.lineStyle || 'solid';
40
48
 
41
- style = (style !== undefined) ? style : "rgba(169,169,169, .6)";
42
- lineWidth = (lineWidth !== undefined) ? lineWidth : 3;
49
+ style = style !== undefined ? style : 'rgba(169,169,169, .6)';
50
+ lineWidth = lineWidth !== undefined ? lineWidth : 3;
43
51
 
44
52
  var fitter = new LineFitter();
53
+ var firstIndex = dataset.data.findIndex((d) => {
54
+ return d !== undefined && d !== null;
55
+ });
45
56
  var lastIndex = dataset.data.length - 1;
46
- var startPos = datasetMeta.data[0]._model.x;
47
- var endPos = datasetMeta.data[lastIndex]._model.x;
57
+ var startPos = datasetMeta.data[firstIndex].x;
58
+ var endPos = datasetMeta.data[lastIndex].x;
59
+ var xy = typeof dataset.data[firstIndex] === 'object';
48
60
 
49
- var xy = false;
50
- if ( dataset.data && typeof dataset.data[0] === 'object') xy = true;
61
+ dataset.data.forEach(function (data, index) {
62
+ if (data == null) return;
51
63
 
52
- dataset.data.forEach(function(data, index) {
53
-
54
- if(data == null)
55
- return;
56
-
57
- if (xScale.options.type === "time") {
64
+ if (xScale.options.type === 'time') {
58
65
  var x = data.x != null ? data.x : data.t;
59
66
  fitter.add(new Date(x).getTime(), data.y);
60
- }
61
- else if (xy) {
67
+ } else if (xy) {
62
68
  fitter.add(data.x, data.y);
63
- }
64
- else {
69
+ } else {
65
70
  fitter.add(index, data);
66
71
  }
67
-
68
72
  });
69
73
 
70
74
  var x1 = xScale.getPixelForValue(fitter.minx);
71
- var x2 = xScale.getPixelForValue(fitter.maxx);
72
75
  var y1 = yScale.getPixelForValue(fitter.f(fitter.minx));
73
- var y2 = yScale.getPixelForValue(fitter.f(fitter.maxx));
74
- if ( !xy ) { x1 = startPos; x2 = endPos; }
75
-
76
+
77
+ var x2;
78
+ var y2;
79
+
80
+ // Project only on x axes, do not project if trendline will never hit x axes
81
+ if (dataset.trendlineLinear.projection && fitter.scale() < 0) {
82
+ // X
83
+ var x2value = fitter.fo();
84
+ if (x2value < fitter.minx) x2value = fitter.maxx;
85
+ x2 = xScale.getPixelForValue(x2value);
86
+
87
+ // Y
88
+ y2 = yScale.getPixelForValue(fitter.f(x2value));
89
+ } else {
90
+ x2 = xScale.getPixelForValue(fitter.maxx);
91
+ y2 = yScale.getPixelForValue(fitter.f(fitter.maxx));
92
+ }
93
+
94
+ if (!xy) {
95
+ x1 = startPos;
96
+ x2 = endPos;
97
+ }
98
+
76
99
  var drawBottom = datasetMeta.controller.chart.chartArea.bottom;
77
100
  var chartWidth = datasetMeta.controller.chart.width;
78
101
 
79
- if(y1 > drawBottom) { // Left side is below zero
102
+ if (y1 > drawBottom) {
103
+ // Left side is below zero
80
104
  var diff = y1 - drawBottom;
81
105
  var lineHeight = y1 - y2;
82
106
  var overlapPercentage = diff / lineHeight;
83
107
  var addition = chartWidth * overlapPercentage;
84
108
 
85
109
  y1 = drawBottom;
86
- x1 = (x1 + addition);
87
- } else if(y2 > drawBottom) { // right side is below zero
110
+ x1 = x1 + addition;
111
+ } else if (y2 > drawBottom) {
112
+ // right side is below zero
88
113
  var diff = y2 - drawBottom;
89
114
  var lineHeight = y2 - y1;
90
115
  var overlapPercentage = diff / lineHeight;
91
- var subtraction = chartWidth - (chartWidth * overlapPercentage);
116
+ var subtraction = chartWidth - chartWidth * overlapPercentage;
92
117
 
93
118
  y2 = drawBottom;
94
119
  x2 = chartWidth - (x2 - subtraction);
95
120
  }
96
121
 
97
122
  ctx.lineWidth = lineWidth;
98
- if (lineStyle === "dotted") { ctx.setLineDash([2, 3]); }
123
+ if (lineStyle === 'dotted') {
124
+ ctx.setLineDash([2, 3]);
125
+ }
99
126
  ctx.beginPath();
100
127
  ctx.moveTo(x1, y1);
101
128
  ctx.lineTo(x2, y2);
@@ -111,34 +138,56 @@ function LineFitter() {
111
138
  this.sumY = 0;
112
139
  this.minx = 1e100;
113
140
  this.maxx = -1e100;
141
+ this.maxy = -1e100;
114
142
  }
115
143
 
116
144
  LineFitter.prototype = {
117
- 'add': function (x, y) {
145
+ add: function (x, y) {
118
146
  x = parseFloat(x);
119
- y = parseFloat(y);
120
-
147
+ y = parseFloat(y);
148
+
121
149
  this.count++;
122
150
  this.sumX += x;
123
151
  this.sumX2 += x * x;
124
152
  this.sumXY += x * y;
125
153
  this.sumY += y;
126
- if ( x < this.minx ) this.minx = x;
127
- if ( x > this.maxx ) this.maxx = x;
154
+ if (x < this.minx) this.minx = x;
155
+ if (x > this.maxx) this.maxx = x;
156
+ if (y > this.maxy) this.maxy = y;
128
157
  },
129
- 'f': function (x) {
158
+ f: function (x) {
130
159
  x = parseFloat(x);
131
-
160
+
132
161
  var det = this.count * this.sumX2 - this.sumX * this.sumX;
133
162
  var offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
134
163
  var scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
135
164
  return offset + x * scale;
136
- }
165
+ },
166
+ fo: function () {
167
+ var det = this.count * this.sumX2 - this.sumX * this.sumX;
168
+ var offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
169
+ var scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
170
+
171
+ // Get x when y = 0
172
+ var xo = -offset / scale;
173
+ return xo;
174
+ },
175
+ scale: function () {
176
+ var det = this.count * this.sumX2 - this.sumX * this.sumX;
177
+ var scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
178
+
179
+ return scale;
180
+ },
137
181
  };
138
182
 
139
183
  // If we're in the browser and have access to the global Chart obj, register plugin automatically
140
- if (typeof window !== "undefined" && window.Chart)
141
- window.Chart.plugins.register(pluginTrendlineLinear);
184
+ if (typeof window !== 'undefined' && window.Chart) {
185
+ if (window.Chart.hasOwnProperty('register')) {
186
+ window.Chart.register(pluginTrendlineLinear);
187
+ } else {
188
+ window.Chart.plugins.register(pluginTrendlineLinear);
189
+ }
190
+ }
142
191
 
143
192
  // Otherwise, try to export the plugin
144
193
  try {
@@ -0,0 +1,9 @@
1
+ const path = require('path');
2
+
3
+ module.exports = {
4
+ entry: './src/chartjs-plugin-trendline.js',
5
+ output: {
6
+ filename: 'chartjs-plugin-trendline.min.js',
7
+ path: path.resolve(__dirname, 'dist'),
8
+ },
9
+ };
@@ -1,75 +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>ScatterChart Example</title>
8
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.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: 'scatter',
14
- data: {
15
- datasets: [{
16
- data: [{x: 1500, y: 86},{x: 1600, y: 114},{x: 1700, y: 106},{x: 1750, y: 106},{x: 1800, y: 107},{x: 1850, y: 111},{x: 1900, y: 133},{x: 1950, y: 221},{x: 1999, y: 783},{x: 2050, y: 2478}],
17
- label: "Africa",
18
- borderColor: "#3e95cd",
19
- fill: false,
20
- trendlineLinear: {
21
- style: "#3e95cd",
22
- lineStyle: "line",
23
- width: 1
24
- }
25
- }, {
26
- data: [{x: 1500, y: 282},{x: 1600, y: 350},{x: 1700, y: 411},{x: 1750, y: 502},{x: 1800, y: 635},{x: 1850, y: 809},{x: 1900, y: 947},{x: 1950, y: 1402},{x: 1999, y: 3700},{x: 2050, y: 5267}],
27
- label: "Asia",
28
- borderColor: "#8e5ea2",
29
- fill: false,
30
- trendlineLinear: {
31
- style: "#8e5ea2",
32
- lineStyle: "line",
33
- width: 1
34
- }
35
- }, {
36
- data: [{x: 1500, y: 168},{x: 1600, y: 170},{x: 1700, y: 178},{x: 1750, y: 190},{x: 1800, y: 203},{x: 1850, y: 276},{x: 1900, y: 408},{x: 1950, y: 547},{x: 1999, y: 675},{x: 2050, y: 734}],
37
- label: "Europe",
38
- borderColor: "#3cba9f",
39
- fill: false
40
- }, {
41
- data: [{x: 1500, y: 40},{x: 1600, y: 20},{x: 1700, y: 10},{x: 1750, y: 16},{x: 1800, y: 24},{x: 1850, y: 38},{x: 1900, y: 74},{x: 1950, y: 167},{x: 1999, y: 508},{x: 2050, y: 784}],
42
- label: "Latin America",
43
- borderColor: "#e8c3b9",
44
- fill: false
45
- }, {
46
- data: [{x: 1500, y: 6},{x: 1600, y: 3},{x: 1700, y: 2},{x: 1750, y: 2},{x: 1800, y: 7},{x: 1850, y: 26},{x: 1900, y: 82},{x: 1950, y: 172},{x: 1999, y: 312},{x: 2050, y: 433}],
47
- label: "North America",
48
- borderColor: "#c45850",
49
- fill: false
50
- }
51
- ]
52
- },
53
- options: {
54
- title: {
55
- display: true,
56
- text: 'World population per region (in millions)',
57
- },
58
- maintainAspectRatio: false,
59
- responsive: true,
60
- scales: {
61
- xAxes: [{ type: 'linear', position: 'bottom', scaleLabel: { labelString: 'year', display: true} }],
62
- yAxes: [{ type: 'linear', position: 'left', scaleLabel: { labelString: 'population/1M', display: true}, display: true }],
63
- }
64
- }
65
- });
66
- });
67
- </script>
68
- </head>
69
- <body>
70
- <h1>Line Chart</h1>
71
- <canvas id="line-chart" width="600" height="450"></canvas>
72
-
73
- <p>Using example code from <a href="http://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/" target="_blank">tobiasahlin.com.</a></p>
74
- </body>
75
- </html>
@@ -1,75 +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>XYlineChart Example</title>
8
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.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
- datasets: [{
16
- data: [{x: 1500, y: 86},{x: 1600, y: 114},{x: 1700, y: 106},{x: 1750, y: 106},{x: 1800, y: 107},{x: 1850, y: 111},{x: 1900, y: 133},{x: 1950, y: 221},{x: 1999, y: 783},{x: 2050, y: 2478}],
17
- label: "Africa",
18
- borderColor: "#3e95cd",
19
- fill: false,
20
- trendlineLinear: {
21
- style: "#3e95cd",
22
- lineStyle: "line",
23
- width: 1
24
- }
25
- }, {
26
- data: [{x: 1500, y: 282},{x: 1600, y: 350},{x: 1700, y: 411},{x: 1750, y: 502},{x: 1800, y: 635},{x: 1850, y: 809},{x: 1900, y: 947},{x: 1950, y: 1402},{x: 1999, y: 3700},{x: 2050, y: 5267}],
27
- label: "Asia",
28
- borderColor: "#8e5ea2",
29
- fill: false,
30
- trendlineLinear: {
31
- style: "#8e5ea2",
32
- lineStyle: "line",
33
- width: 1
34
- }
35
- }, {
36
- data: [{x: 1500, y: 168},{x: 1600, y: 170},{x: 1700, y: 178},{x: 1750, y: 190},{x: 1800, y: 203},{x: 1850, y: 276},{x: 1900, y: 408},{x: 1950, y: 547},{x: 1999, y: 675},{x: 2050, y: 734}],
37
- label: "Europe",
38
- borderColor: "#3cba9f",
39
- fill: false
40
- }, {
41
- data: [{x: 1500, y: 40},{x: 1600, y: 20},{x: 1700, y: 10},{x: 1750, y: 16},{x: 1800, y: 24},{x: 1850, y: 38},{x: 1900, y: 74},{x: 1950, y: 167},{x: 1999, y: 508},{x: 2050, y: 784}],
42
- label: "Latin America",
43
- borderColor: "#e8c3b9",
44
- fill: false
45
- }, {
46
- data: [{x: 1500, y: 6},{x: 1600, y: 3},{x: 1700, y: 2},{x: 1750, y: 2},{x: 1800, y: 7},{x: 1850, y: 26},{x: 1900, y: 82},{x: 1950, y: 172},{x: 1999, y: 312},{x: 2050, y: 433}],
47
- label: "North America",
48
- borderColor: "#c45850",
49
- fill: false
50
- }
51
- ]
52
- },
53
- options: {
54
- title: {
55
- display: true,
56
- text: 'World population per region (in millions)',
57
- },
58
- maintainAspectRatio: false,
59
- responsive: true,
60
- scales: {
61
- xAxes: [{ type: 'linear', position: 'bottom', scaleLabel: { labelString: 'year', display: true} }],
62
- yAxes: [{ type: 'linear', position: 'left', scaleLabel: { labelString: 'population/1M', display: true}, display: true }],
63
- }
64
- }
65
- });
66
- });
67
- </script>
68
- </head>
69
- <body>
70
- <h1>Scatter Chart</h1>
71
- <canvas id="line-chart" width="600" height="450"></canvas>
72
-
73
- <p>Using example code from <a href="http://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/" target="_blank">tobiasahlin.com.</a></p>
74
- </body>
75
- </html>
package/gulpfile.js DELETED
@@ -1,37 +0,0 @@
1
- var gulp = require('gulp');
2
- var uglify = require('gulp-uglify');
3
- var pump = require('pump');
4
- var rename = require('gulp-rename');
5
- var del = require('del');
6
- var bump = require('gulp-bump');
7
-
8
- gulp.task('clean', function () {
9
- return del([
10
- 'dist/',
11
- ]);
12
- });
13
-
14
- gulp.task('compress', function (cb) {
15
- pump([
16
- gulp.src('src/*.js'),
17
- uglify(),
18
- rename('chartjs-plugin-trendline.min.js'),
19
- gulp.dest('dist/')
20
- ],
21
- cb
22
- );
23
- });
24
-
25
- gulp.task('bump-minor', function(){
26
- return gulp.src(['./package.json', './src/chartjs-plugin-trendline.js'], {base: './'})
27
- .pipe(bump({type:'minor'}))
28
- .pipe(gulp.dest('./'));
29
- });
30
-
31
- gulp.task('bump-patch', function(){
32
- return gulp.src(['./package.json', './src/chartjs-plugin-trendline.js'], {base: './'})
33
- .pipe(bump({type:'patch'}))
34
- .pipe(gulp.dest('./'));
35
- });
36
-
37
- gulp.task('default', gulp.series('clean', 'compress'));