|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
body { |
|
font: 10px sans-serif; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.x.axis path { |
|
display: none; |
|
} |
|
|
|
.line { |
|
fill: none; |
|
stroke: steelblue; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
.reg { |
|
fill: none; |
|
stroke: orange; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="https://d3js.org/d3.v4.js"></script> |
|
<script src="https://unpkg.com/[email protected]/dist/simple-statistics.min.js"> |
|
</script> |
|
|
|
<script> |
|
|
|
var margin = {top: 20, right: 20, bottom: 30, left: 50}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var parseDate = d3.timeParse("%d-%b-%y"); |
|
|
|
var x = d3.scaleTime() |
|
.range([0, width]); |
|
|
|
var y = d3.scaleLinear() |
|
.range([height, 0]); |
|
|
|
var xAxis = d3.axisBottom(x);//.tickFormat(function(d){ return d.x;}); |
|
var yAxis = d3.axisLeft(y); |
|
|
|
var line = d3.line() |
|
.x(function(d) { return x(d.date); }) |
|
.y(function(d) { return y(d.close); }); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
d3.tsv("data.tsv", function(error, data) { |
|
data.forEach(function(d) { |
|
d.date = parseDate(d.date); |
|
d.close = +d.close; |
|
}); |
|
|
|
x.domain(d3.extent(data, function(d) { return d.date; })); |
|
y.domain(d3.extent(data, function(d) { return d.close; })); |
|
|
|
// Derive a linear regression |
|
var regression = ss.linearRegression(data.map(function(d) { |
|
return [+d.date, d.close]; |
|
})); |
|
|
|
var lin = ss.linearRegressionLine(regression); |
|
|
|
// Create a line based on the beginning and endpoints of the range |
|
var lindata = x.domain().map(function(x) { |
|
return { |
|
date: new Date(x), |
|
close: lin(+x) |
|
}; |
|
}); |
|
|
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis); |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 6) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text("Price ($)"); |
|
|
|
svg.append("path") |
|
.datum(data) |
|
.attr("class", "line") |
|
.attr("d", line); |
|
|
|
svg.selectAll(".dot") |
|
.data(data) |
|
.enter().append("circle") |
|
.attr("class", "dot") |
|
.attr("opacity", 0.9) |
|
.attr("r", 2) |
|
.attr("cx", function(d) { return x(d.date); }) |
|
.attr("cy", function(d) { return y(d.close); }) |
|
.style("fill", "red"). |
|
style("stroke"," lightgrey" ).style("stroke-width","1px"); |
|
|
|
svg.append("path") |
|
.datum(lindata) |
|
.attr("class", "reg") |
|
.attr("d", line); |
|
}); |
|
|
|
</script> |