javascript - Stacked Bar Chart with D3 -
i want create stacked bar chart d3. have info in csv file:
type sum color regular 29756.85897 greenish regular 9756.85897 bluish
and want each row appear above other in y axis.
for illustration in photo, bluish area should start in y=9756 until y=39512.
what should change?
this relevant html code:
the whole code:
<!doctype html> <html> <head> <meta charset="utf-8"> <style> body { font: 10px sans-serif; margin:auto; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispedges; } .bar1 { fill: #00ff66; } .bar1:hover { fill: black ; } .x.axis path { display: none; } .d3-tip { line-height: 1; font-weight: bold; padding: 12px; background: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 2px; } /* creates little triangle extender tooltip */ .d3-tip:after { box-sizing: border-box; display: inline; font-size: 10px; width: 100%; line-height: 1; color: rgba(0, 0, 0, 0.8); content: "\25bc"; position: absolute; text-align: center; } /* style northward tooltips differently */ .d3-tip.n:after { margin: -1px 0 0 0; top: 100%; left: 0; } </style> <body> <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> <script> var margin = {top: 80, right: 90, bottom: 30, left: 90}, width = 1000 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var formatpercent = d3.format(".0%"); //יצירת x //יאכלס את סוגי הרכב השונים var x = d3.scale.ordinal() .rangeroundbands([0, width], .1); //יצירת ציר y //יציג בר עבור מחיר הרכב המוצע לדילרים var y = d3.scale.linear() .range([height, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom"); //יצירת ציר הy //והצמדתו לצד שמאל var yaxis = d3.svg.axis() .scale(y) .orient("left").ticks(4) var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { homecoming "<strong></strong>"+d.type+"<br><strong></strong> <span style='color:#00ff66'>" + d.sum + "</span>"; }) 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 + ")"); svg.call(tip); //קליטת הטבלה והגדרת הטווחים על הצירים d3.csv("targil2.csv", type, function(error, data) { x.domain(data.map(function(d) { homecoming d.type; })); y.domain([0, d3.max(data, function(d) { homecoming d.sum*2; })]); var stack = d3.layout.stack(); .x(function(d) { homecoming d.type }) // tell d3 utilize type x value .y(function(d) { homecoming d.sum }); // tell d3 utilize sum y value var stackdata = stack(data); //הוספה של 2 הצירים svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis); svg.append("g") .attr("class", "y axis axisleft") .attr("transform", "translate(0,0)") .call(yaxis) .append("text") .attr("y", 6) .attr("dy", "-2em") .style("text-anchor", "end") .style("text-anchor", "end") .text("price"); //הוספת בר הנתונים svg.selectall(".bar") .data(data) .enter().append("rect") .attr("x", function(d) { homecoming x(d.type); }) .attr("width", x.rangeband()) .attr("y", function(d) { homecoming d.y0 }) .attr("height", function(d) { homecoming (height - y(d.sum)); }) .style("fill", function(d){ if(d["color"] == "green"){ homecoming "green";} else homecoming "#0066ff";}) .on('mouseover', tip.show) .on('mouseout', tip.hide) }); function type(d) { d.sum = +d.sum; homecoming d; } </script> </body> </html> i tried utilize stack function told me, , changed attribute of "y" , it's not work me now. think did wrong.
here go.
<!doctype html> <html> <head> <meta charset="utf-8"> <style> body { font: 10px sans-serif; margin:auto; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispedges; } .bar1 { fill: #00ff66; } .bar1:hover { fill: black ; } .x.axis path { display: none; } .d3-tip { line-height: 1; font-weight: bold; padding: 12px; background: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 2px; } /* creates little triangle extender tooltip */ .d3-tip:after { box-sizing: border-box; display: inline; font-size: 10px; width: 100%; line-height: 1; color: rgba(0, 0, 0, 0.8); content: "\25bc"; position: absolute; text-align: center; } /* style northward tooltips differently */ .d3-tip.n:after { margin: -1px 0 0 0; top: 100%; left: 0; } </style> <body> <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> <script> var margin = {top: 80, right: 90, bottom: 30, left: 90}, width = 1000 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var formatpercent = d3.format(".0%"); //יצירת x //יאכלס את סוגי הרכב השונים var x = d3.scale.ordinal() .rangeroundbands([0, width], .1); //יצירת ציר y //יציג בר עבור מחיר הרכב המוצע לדילרים var y = d3.scale.linear() .range([height, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom"); //יצירת ציר הy //והצמדתו לצד שמאל var yaxis = d3.svg.axis() .scale(y) .orient("left").ticks(4) var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { homecoming "<strong></strong>"+d.type+"<br><strong></strong> <span style='color:#00ff66'>" + d.sum + "</span>"; }) 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 + ")"); svg.call(tip); //קליטת הטבלה והגדרת הטווחים על הצירים d3.csv("targil2.csv", type, function(error, data) { window.dataset = data; data.sort(function(x,y){ var = x.sum; var b = y.sum; homecoming > b ? -1 : < b ? 1 : 0 }) x.domain(data.map(function(d) { homecoming d.type; })); y.domain([0, d3.max(data, function(d) { homecoming d.sum*2; })]); var stack = d3.layout.stack() .x(function(d) { homecoming d.type }) // tell d3 utilize type x value .y(function(d) { homecoming d.sum }); // tell d3 utilize sum y value // var stackdata = stack(data); //הוספה של 2 הצירים svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis); svg.append("g") .attr("class", "y axis axisleft") .attr("transform", "translate(0,0)") .call(yaxis) .append("text") .attr("y", 6) .attr("dy", "-2em") .style("text-anchor", "end") .style("text-anchor", "end") .text("price"); var stacksofar = 0; //הוספת בר הנתונים svg.selectall(".bar") .data(data) .enter().append("rect") .attr("x", function(d) { homecoming x(d.type); }) .attr("width", x.rangeband()) .attr("y", function(d){ d3.select(this) .attr("height", function(d2){ var thisheight = height - y(d.sum); stacksofar += thisheight homecoming thisheight }); homecoming (height - stacksofar) }) .style("fill", function(d){ if(d["color"] == "green"){ homecoming "green";} else homecoming "#0066ff";}) .on('mouseover', tip.show) .on('mouseout', tip.hide) }); function type(d) { d.sum = +d.sum; homecoming d; } </script> </body> </html> javascript d3.js
No comments:
Post a Comment