Tom-Alexander/regression-js

How to use regression.js in a browser

alitaker opened this issue · 3 comments

Hi, the readme states that regression.js works in a browser too, but I can't seem to use it when importing with:
import {regression} from 'regression';
Either I get
ReferenceError: regression is not defined
when using it
or
TypeError: global is undefined
or
TypeError: a is undefined
when using the minified version.

Can anybody provide a strip down HTML example without using node.js or package managers?

Thank you

Hi, I found this example and that worked for me https://jsfiddle.net/nagubal/cpxwLf2t/
the relevant part is this one:

const getRegression = (data, degre) => {
degre = degre || 2;
let dataRegression = [];
data.forEach((element, index) => dataRegression.push([index + 1, element]));

let resultRegression = [];
regression('polynomial', dataRegression, degre).points.forEach((element) =>
resultRegression.push(Math.ceil(element[1] * 100) / 100)
);
return resultRegression;
};

Apparently the module usage has been changed but the documentation was not updated yet

@Miguel446 that example is using regression.js version 1.3.0 from here:
https://cdnjs.cloudflare.com/ajax/libs/regression/1.3.0/regression.min.js

I found an exampe with 1.4.0 at the following link:
https://bl.ocks.org/daluu/5bb59ef3f3fed3de227535da367649ba
It's working for me and now I'm using that version.

Anyway, I'd like to be able to use current and future versions, so if anyone has a clue on using the latest version in an HTML, please post it here.

After googling a bit I found that there should be a specific build for browsers or a workaround config, as for this example of Vue.js:
https://vuejsdevelopers.com/2019/02/04/vue-es-module-browser-build/

Great article about modules and also the latest regression.js cdn version is 1.4.0 and the npm version is 2.0.1, so unfortunately the developer stopped supporting web browsers some time ago

But if you really need the latest version, I found this article about how to use a npm module on html, I’m not sure if it will work but it’s worth giving it a shot

https://medium.com/jeremy-keeshin/hello-world-for-javascript-with-npm-modules-in-the-browser-6020f82d1072