X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=inline;f=gallery%2Flinear-regression.js;h=cb6b9d95cac57919ae21d06fce9e00c67edc7e72;hb=c715be428d83d80c0a26fd60dc5bc36fb4c908d9;hp=77f69c51ae02d6077963c6a06150aeaadc610d1a;hpb=e14dc3feda3e6d429bb953dab83c593d851d90af;p=dygraphs.git
diff --git a/gallery/linear-regression.js b/gallery/linear-regression.js
index 77f69c5..cb6b9d9 100644
--- a/gallery/linear-regression.js
+++ b/gallery/linear-regression.js
@@ -1,26 +1,27 @@
-// Use this as a template for new Gallery entries.
+/*global Gallery,Dygraph,data */
Gallery.register(
'linear-regression',
{
name: 'Linear Regressions',
title: 'Linear Regression Demo',
setup: function(parent) {
- parent.innerHTML =
- "
Click the buttons to generate linear regressions over either data "+
- "series. If you zoom in and then click the regression button, the regression "+
- "will only be run over visible points. Zoom back out to see what the local "+
- "regression looks like over the full data.
"+
- "" +
- "
" +
- " " +
- " " +
- "" +
- "
";
+ parent.innerHTML = [
+ "
Click the buttons to generate linear regressions over either data ",
+ "series. If you zoom in and then click the regression button, the regression ",
+ "will only be run over visible points. Zoom back out to see what the local ",
+ "regression looks like over the full data.
",
+ "",
+ "
",
+ " ",
+ " ",
+ "",
+ "
"].join("\n");
},
run: function() {
- document.getElementById("ry1").onclick = function() { regression(1) };
- document.getElementById("ry2").onclick = function() { regression(2) };
- document.getElementById("clear").onclick = function() { clearLines() };
+ var g, regression, clearLines; // defined below
+ document.getElementById("ry1").onclick = function() { regression(1); };
+ document.getElementById("ry2").onclick = function() { regression(2); };
+ document.getElementById("clear").onclick = function() { clearLines(); };
var data = [];
for (var i = 0; i < 120; i++) {
@@ -29,22 +30,11 @@ Gallery.register(
30.0 - i / 5.0 - 10.0 * Math.sin(i / 3.0 + 1.0)]);
}
- g = new Dygraph(
- document.getElementById("demodiv"),
- data,
- {
- labels: ['X', 'Y1', 'Y2'],
- underlayCallback: drawLines,
- drawPoints: true,
- strokeWidth: 0.0
- }
- );
-
// coefficients of regression for each series.
// if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1
// only. The regression line is y = 1 + 2 * x.
var coeffs = [ null, null, null ];
- function regression(series) {
+ regression = function(series) {
// Only run the regression over visible points.
var range = g.xAxisRange();
@@ -54,7 +44,7 @@ Gallery.register(
if (x < range[0] || x > range[1]) continue;
var y = g.getValue(i, series);
- if (y == null) continue;
+ if (y === null || y === undefined) continue;
if (y.length == 2) {
// using fractions
y = y[0] / y[1];
@@ -76,12 +66,12 @@ Gallery.register(
}
g.updateOptions({}); // forces a redraw.
- }
+ };
- function clearLines() {
+ clearLines = function() {
for (var i = 0; i < coeffs.length; i++) coeffs[i] = null;
g.updateOptions({});
- }
+ };
function drawLines(ctx, area, layout) {
if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
@@ -100,11 +90,11 @@ Gallery.register(
var p1 = g.toDomCoords(x1, y1);
var p2 = g.toDomCoords(x2, y2);
- var c = new RGBColor(g.getColors()[i - 1]);
+ var c = Dygraph.toRGB_(g.getColors()[i - 1]);
c.r = Math.floor(255 - 0.5 * (255 - c.r));
c.g = Math.floor(255 - 0.5 * (255 - c.g));
c.b = Math.floor(255 - 0.5 * (255 - c.b));
- var color = c.toHex();
+ var color = 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = 1.0;
@@ -116,5 +106,17 @@ Gallery.register(
ctx.restore();
}
}
+
+ g = new Dygraph(
+ document.getElementById("demodiv"),
+ data,
+ {
+ labels: ['X', 'Y1', 'Y2'],
+ underlayCallback: drawLines,
+ drawPoints: true,
+ drawAxesAtZero: true,
+ strokeWidth: 0.0
+ }
+ );
}
});