D3 test
var margin = {top: 8, right: 10, bottom: 2, left: 10}, width = 150 - margin.left - margin.right, height = 150 - margin.top - margin.bottom;
//var parseDate = d3.time.format("%x").parse;
// Parse the date / time //var parseDate = d3.time.format("%d-%b-%y").parse; var parseDate = d3.time.format("%m/%-d/%Y").parse; //01/22/2014 //var parseDate = d3.time.format("%x").parse; bisectDate = d3.bisector(function(d) { return d.date }).left; // ** //console.log(bisectDate); var x = d3.time.scale() .range([0, width]);
var y = d3.scale.linear() .range([height, 0]);
var valueline = d3.svg.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.price); });
var area = d3.svg.area() .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(d.price); });
var line = d3.svg.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.price); });
d3.csv("https://cityam.go-vip.net/special/d3/tesco.csv", type, function(error, data) {
// Nest data by symbol. var symbols = d3.nest() .key(function(d) { return d.symbol; }) .entries(data);
// Compute the maximum price per symbol, needed for the y-domain. symbols.forEach(function(s) { s.maxPrice = d3.max(s.values, function(d) { return d.price; }); });
// Compute the minimum and maximum date across symbols. // We assume values are sorted by date. x.domain([ d3.min(symbols, function(s) { return s.values[0].date; }), d3.max(symbols, function(s) { return s.values[s.values.length - 1].date; }) ]);
// Add an SVG element for each symbol, with the desired dimensions and margin. var svg = d3.select("#D3TEST").selectAll("svg") .data(symbols) .enter().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 + ")");
// Add the area path elements. Note: the y-domain is set per element. svg.append("path") .attr("class", "area") .attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); });
// Add the line path elements. Note: the y-domain is set per element. svg.append("path") .attr("class", "line") .attr("d", function(d) { y.domain([0, d.maxPrice]); return line(d.values); });
// Add a small label for the symbol name. svg.append("text") .attr("x", width - 6) .attr("y", height - 6) .style("text-anchor", "end") .text(function(d) { return d.key; });
var focus = svg.append("g") // ********** .style("display", "none"); // **********
// append the circle at the intersection // ********** focus.append("circle") // ********** .attr("class", "y") // ********** .style("fill", "none") // ********** .style("stroke", "blue") // ********** .attr("r", 4); // **********
// append the rectangle to capture mouse // ********** svg.append("rect") // ********** .attr("width", width) // ********** .attr("height", height) // ********** .style("fill", "none") // ********** .style("pointer-events", "all") // ********** .on("mouseover", function() { focus.style("display", null); }) .on("mouseout", function() { focus.style("display", "none"); }) .on("mousemove", mousemove); // **********
function mousemove() { // ********** var x0 = x.invert(d3.mouse(this)[0]), // ********** i = bisectDate(data, x0, 1), // ********** d0 = data[i - 1], // ********** d1 = data[i], // ********** d = x0 - d0.date > d1.date - x0 ? d1 : d0; // **********
focus.select("circle.y") // ********** .attr("transform", // ********** "translate(" + x(d.date) + "," + // ********** y(d.price) + ")"); // ********** } // **********
});
function type(d) { d.price = +d.price; d.date = parseDate(d.date); return d; }