Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantin committed Jan 28, 2015
1 parent 686bf74 commit 7a5fd9f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
98 changes: 96 additions & 2 deletions build/development/plugins/tauCharts.trendline.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,98 @@
}

return {equation: [lastvalue], points: results, string: "" + lastvalue};
},

loess: function(data) {
//adapted from the LoessInterpolator in org.apache.commons.math
function loess_pairs(pairs, bandwidth) {
var xval = pairs.map(function(pair){return pair[0]});
var yval = pairs.map(function(pair){return pair[1]});
var res = loess(xval, yval, bandwidth);
return xval.map(function (x, i) {
return [x, res[i]];
});
}

function loess(xval, yval, bandwidth) {
function tricube(x) {
var tmp = 1 - x * x * x;
return tmp * tmp * tmp;
}

var res = [];

var left = 0;
var right = Math.floor(bandwidth * xval.length) - 1;

for (var i = 0; i < xval.length; i++) {

var x = xval[i];

if (i > 0) {
if (right < xval.length - 1 &&
xval[right+1] - xval[i] < xval[i] - xval[left]) {
left++;
right++;
}
}

var edge;
if (xval[i] - xval[left] > xval[right] - xval[i])
edge = left;
else
edge = right;

var denom = Math.abs(1.0 / (xval[edge] - x));

var sumWeights = 0;
var sumX = 0, sumXSquared = 0, sumY = 0, sumXY = 0;

var k = left;
while(k <= right)
{
var xk = xval[k];
var yk = yval[k];
var dist;
if (k < i) {
dist = (x - xk);
} else {
dist = (xk - x);
}
var w = tricube(dist * denom);
var xkw = xk * w;
sumWeights += w;
sumX += xkw;
sumXSquared += xk * xkw;
sumY += yk * w;
sumXY += yk * xkw;
k++;
}

var meanX = sumX / sumWeights;
var meanY = sumY / sumWeights;
var meanXY = sumXY / sumWeights;
var meanXSquared = sumXSquared / sumWeights;

var beta;
if (meanXSquared == meanX * meanX)
beta = 0;
else
beta = (meanXY - meanX * meanY) / (meanXSquared - meanX * meanX);

var alpha = meanY - beta * meanX;

res[i] = beta * x + alpha;
}

return res;
}

return {
equation: 'loess',
points: loess_pairs(data, 0.5),
string: 'loess'
};
}
};

Expand Down Expand Up @@ -243,6 +335,7 @@
trendColor].join(' ');

var line = d3.svg.line()
.interpolate('basis')
.x(function (d) {
return xScale(d[0]);
})
Expand Down Expand Up @@ -314,7 +407,8 @@
{
type: 'linear',
showPanel: true,
showTrend: true
showTrend: true,
models: ['linear', 'exponential', 'logarithmic']
});

return {
Expand Down Expand Up @@ -451,7 +545,7 @@
title: 'Trend line',
error: this._error,
showTrend: (settings.showTrend && this._isApplicable) ? 'checked' : '',
models: ['linear', 'exponential', 'logarithmic'].map(function (x) {
models: settings.models.map(function (x) {
var selected = (settings.type === x) ? 'selected' : '';
return '<option ' + selected + ' value="' + x + '">' + x + '</option>';
})
Expand Down
Loading

0 comments on commit 7a5fd9f

Please sign in to comment.