Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The name as it appears in the legend and tooltip. Use the following replacements
Default: `Equation: %eq`

##### `decimalPlaces` (Int)
Set the number of decimal places for r and r<sup>2</sup> (linear only). Default: `2`
Set the number of decimal places for r and r<sup>2</sup> (linear and power only). Default: `2`

##### `lineType` (String)
??. Default: `spline`
Expand Down
12 changes: 8 additions & 4 deletions highcharts-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
var regressionType = s.regressionSettings.type || "linear";
var regression;
var extraSerie = s.regressionSettings.regressionSeriesOptions;

// Set default values
extraSerie.data = [];
extraSerie.isRegressionLine = true;
Expand Down Expand Up @@ -81,7 +81,7 @@
regression = _polynomial(mergedData, order, extrapolate);
} else if (regressionType == "power") {
var extrapolate = s.regressionSettings.extrapolate || 0;
regression = _power(mergedData, extrapolate);
regression = _power(mergedData, s.regressionSettings.decimalPlaces, extrapolate);
} else if (regressionType == "logarithmic") {
var extrapolate = s.regressionSettings.extrapolate || 0;
regression = _logarithmic(mergedData, extrapolate);
Expand Down Expand Up @@ -337,7 +337,7 @@
/**
* Code extracted from https://github.com/Tom-Alexander/regression-js/
*/
function _power(data, extrapolate) {
function _power(data, decimalPlaces, extrapolate) {
var sum = [0, 0, 0, 0], n = 0, results = [];

for (len = data.length; n < len; n++) {
Expand Down Expand Up @@ -381,7 +381,7 @@
return 0;
});

var string = 'y = ' + Math.round(A * 100) / 100 + 'x^' + Math.round(B * 100) / 100;
var string = 'y = ' + _round(A, decimalPlaces) + 'x^' + _round(B, decimalPlaces);

return {equation: [A, B], points: results, string: string};
}
Expand Down Expand Up @@ -654,6 +654,10 @@
}

function _round(number, decimalPlaces) {
// Smaller than 0.xxxx (decimalPlaces), use scientific notation
if (number < Math.pow(10, -decimalPlaces)) {
return number.toExponential(decimalPlaces);
}
var decimalFactor = Math.pow(10, decimalPlaces);
return Math.round(number * decimalFactor) / decimalFactor;
}
Expand Down