|
! function () { |
|
function t(a, b) { |
|
return a.RMSE > b.RMSE ? -1 : a.RMSE < b.RMSE ? 1 : 0 |
|
} |
|
|
|
function u(a, b) { |
|
p.append("path").datum(a).attr("class", b).attr("d", s) |
|
} |
|
var g, k, l, m, n, a = [], |
|
b = [], |
|
f = "tonnes", |
|
h = { |
|
top: 20, |
|
right: 20, |
|
bottom: 30, |
|
left: 50 |
|
}, i = 960 - h.left - h.right, |
|
j = 500 - h.top - h.bottom, |
|
o = [], |
|
p = d3.select("body").append("svg").attr("width", i + h.left + h.right).attr("height", j + h.top + h.bottom).append("g").attr("transform", "translate(" + h.left + "," + h.top + ")"), |
|
q = p.append("g").attr("class", "x axis").attr("transform", "translate(0," + j + ")"), |
|
r = p.append("g").attr("class", "y axis"), |
|
s = d3.svg.line().defined(function (a) { |
|
return null != a |
|
}).x(function (a, b) { |
|
return k(new Date(g + b, 1, 1)) |
|
}).y(function (a) { |
|
return l(a) |
|
}); |
|
d3.csv("brazil_soybean_production.csv", function (c) { |
|
c.forEach(function (c) { |
|
a.push(+c.year), b.push(+c.value) |
|
}), g = d3.min(a), k = d3.time.scale().domain([new Date(g, 1, 1), new Date(d3.max(a), 1, 1)]).range([0, i]), l = d3.scale.linear().range([j, 0]), m = d3.svg.axis().scale(k).orient("bottom"), q.call(m), n = d3.svg.axis().scale(l).orient("left").tickFormat(abbrNum); |
|
var d = function (a) { |
|
return a |
|
}, e = function (a) { |
|
return a |
|
}; |
|
["linear", "logarithmic", "exponential"].forEach(function (c) { |
|
switch (c) { |
|
case "linear": |
|
e = function (a) { |
|
return a |
|
}, d = function (a) { |
|
return a |
|
}; |
|
break; |
|
case "logarithmic": |
|
e = function (a) { |
|
return Math.log(a) |
|
}, d = function (a) { |
|
return a |
|
}; |
|
break; |
|
case "exponential": |
|
e = function (a) { |
|
return a |
|
}, d = function (a) { |
|
return Math.log(a) |
|
} |
|
} |
|
var f = a.length; |
|
d3.range(f - 5).forEach(function (g) { |
|
var h = a[g], |
|
i = a.slice(g, f).map(function (a) { |
|
return e(a) |
|
}), |
|
j = b.slice(g, f).map(function (a) { |
|
return d(a) |
|
}), |
|
k = linearRegression(j, i), |
|
l = k.slope, |
|
m = k.intercept, |
|
n = 0, |
|
p = i.length, |
|
q = a.map(function (a, d) { |
|
if (a >= h) { |
|
var f; |
|
return f = "exponential" == c ? Math.exp(m) * Math.exp(a * l) : m + e(a) * l, n += Math.pow(b[d] - f, 2), 0 > f ? null : f |
|
} |
|
return null |
|
}); |
|
o.push({ |
|
type: c, |
|
values: q, |
|
RMSE: Math.pow(n / p, .5), |
|
minYear: h |
|
}) |
|
}) |
|
}); |
|
var h = d3.extent(d3.merge(o.map(function (a) { |
|
return a.values.filter(function (a) { |
|
return null != a |
|
}) |
|
}))); |
|
l.domain(h), u(b, "line"), r.call(n).append("text").attr("transform", "rotate(-90)").attr("y", 6).attr("dy", ".71em").style("text-anchor", "end").text(f); |
|
var v = o.sort(t); |
|
p.append("g").attr("class", "lineGroup").selectAll("path").data(v).enter().append("path").attr("class", function (a) { |
|
return a.type |
|
}).style("visibility", function () { |
|
return "hidden" |
|
}).style("opacity", 1).attr("d", function (a) { |
|
return s(a.values) |
|
}).transition().delay(function (a, b) { |
|
return 10 * b |
|
}).style("visibility", function () { |
|
return "visible" |
|
}).transition().delay(function (a, b) { |
|
return 100 * b |
|
}).style("opacity", function (a, b) { |
|
return b < v.length - 1 ? 0 : 1 |
|
}) |
|
}) |
|
}(); |