function renderDifferenceChart (dataset,dom_element_to_append_to){ var margin = {top: 50, right: 80, bottom: 50, left: 80}; var width = 700 - margin.left - margin.right; var height = width*3/5; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.area() .interpolate("linear") .x(function(d) { return x(d.date); }) .y(function(d) { return y(d["Series 1"]); }); var area = d3.svg.area() .interpolate("area") .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d["Series 1"]); }); var svg = d3.select(dom_element_to_append_to).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 + ")"); dataset.forEach(function(d) { d["Series 1"]= +d["Series 1"]; d["Series 2"] = +d["Series 2"]; }); x.domain(d3.extent(dataset, function(d) { return d.date; })); y.domain([ d3.min(dataset, function(d) { return Math.min(d["Series 1"], d["Series 2"]) - 10; }), d3.max(dataset, function(d) { return Math.max(d["Series 1"], d["Series 2"]) + 10; }) ]); svg.datum(dataset); svg.append("clipPath") .attr("id", "clip-below") .append("path") .attr("d", area.y0(height)); svg.append("clipPath") .attr("id", "clip-above") .append("path") .attr("d", area.y0(0)); svg.append("path") .attr("class", "area above") .attr("clip-path", "url(#clip-above)") .attr("d", area.y0(function(d) { return y(d["Series 2"]); })); svg.append("path") .attr("class", "area below") .attr("clip-path", "url(#clip-below)") .attr("d", area); svg.append("path") .attr("class", "line") .attr("d", line); 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("Cashflow (INR)"); var focus = svg.append("g") .attr("class", "focus") .style("display", "none"); focus.append("circle") .attr("r", 4.5); focus.append("text") .attr("x", 9) .attr("dy", ".35em"); svg.append("rect") .attr("class", "overlay") .attr("width", width) .attr("height", height) .on("mouseover", function() { focus.style("display", null); }) .on("mouseout", function() { focus.style("display", "none"); }) .on("mousemove", mousemove); var bisectDate = d3.bisector(function(d) { return d.date; }).left; function mousemove() { var x0 = x.invert(d3.mouse(this)[0]), i = bisectDate(dataset, x0, 1), d0 = dataset[i - 1], d1 = dataset[i], d = x0 - d0.date > d1.date - x0 ? d1 : d0; var value = 0; if(d['Series 1']==0){ value = d['Series 2']; }else if(d['Series 2']==0){ value = d['Series 1']; }else if(d['Series 2'] > d['Series 1']){ value = d['Series 2']; }else{ value = d['Series 1']; } focus.attr("transform", "translate(" + x(d.date) + "," + y(value) + ")"); focus.select("text").text("-ve ₹" + d["Series 2"] + "; +ve ₹" + d["Series 1"]); } }