|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.axis--x path { |
|
display: none; |
|
} |
|
|
|
.line { |
|
fill: none; |
|
stroke: steelblue; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
</style> |
|
|
|
<svg width="960" height="500"></svg> |
|
|
|
<script src="//d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
var svg = d3.select("svg"), |
|
margin = {top: 20, right: 80, bottom: 30, left: 50}, |
|
width = svg.attr("width") - margin.left - margin.right, |
|
height = svg.attr("height") - margin.top - margin.bottom, |
|
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
|
|
var parseTime = d3.timeParse("%Y%m%d"); |
|
|
|
var x = d3.scaleTime().range([0, width]), |
|
y = d3.scaleLinear().range([height, 0]), |
|
z = d3.scaleOrdinal(d3.schemeCategory10); |
|
|
|
var line = d3.line() |
|
.curve(d3.curveBasis) |
|
.x(function(d) { return x(d.date); }) |
|
.y(function(d) { return y(d.return); }); |
|
|
|
|
|
|
|
function type(d, _, columns) { |
|
d.Date = parseTime(+d.Date); |
|
// i = 1 to skip date, c = columns[i] for +d[c] |
|
for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c]; |
|
return d; |
|
} |
|
|
|
|
|
d3.csv("data.csv", type, function(error, data) { |
|
if (error) throw error; |
|
|
|
// console.log(data.columns); |
|
// console.log(data); |
|
// console.log(data[0].Date); |
|
|
|
var returns = data.columns.slice(data.columns.length-1).map(function(id) { |
|
return { |
|
id: "hkws", |
|
values: data.map(function(d) { |
|
return {date: d.Date, return: d.return}; |
|
}) |
|
}; |
|
}); |
|
console.log(returns[0].values); |
|
|
|
|
|
|
|
x.domain(d3.extent(returns[0].values, function(d) { return d.date; })); |
|
|
|
|
|
|
|
|
|
y.domain( |
|
d3.extent(returns[0].values, function(d) { return d.return;}) |
|
); |
|
|
|
|
|
// z.domain(rr.map(function(c) { return c.id; })); |
|
|
|
|
|
g.append("g") |
|
.attr("class", "axis axis--x") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x)); |
|
|
|
|
|
g.append("g") |
|
.attr("class", "axis axis--y") |
|
.call(d3.axisLeft(y)) |
|
.append("text") |
|
.attr("x",20) |
|
.attr("y", 6) |
|
.attr("dy", "0.71em") |
|
.attr("text-anchor", "start") |
|
.attr("fill", "#000") |
|
.text("return, 1-base"); |
|
|
|
|
|
|
|
|
|
|
|
console.log("what inside returns", returns); |
|
var a= g.append("path").datum(returns[0].values) |
|
.attr("class", "line") |
|
.attr("d", function(d) { return line(d); }) |
|
.style("stroke", "red" ); |
|
console.log("what inside a", a); |
|
|
|
|
|
var text1 = g.append("text") |
|
.datum(returns[0].values) |
|
.attr("transform", function(d){return "translate(" + x(d[returns[0].values.length - 1].date) + "," + y(d[returns[0].values.length - 1].return) + ")";}) |
|
.attr("x", 3) |
|
.attr("dy", "0.35em") |
|
.style("font", "10px sans-serif") |
|
.text(function(d) { return returns[0].id; }); |
|
|
|
console.log("text1 has", text1); |
|
}); |
|
|
|
|
|
</script> |
|
|