javascript - d3.js bar chart not showing right value -
so have created d3 bar chart, can't seem figure out how make values of bars show correctly. displays bars @ max value 30.
i have pinpointed error line :
d.amenities = amenitynames.map(function (name) { console.log( +d["sum"]); return {name: name, value: +d["sum"]}; });
it seems assigning value wrong not sure how correct this.
how correct this? there same of json. appreciate.
var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x0 = d3.scale.ordinal() .rangeroundbands([0, width], .1); var x1 = d3.scale.ordinal(); var y = d3.scale.linear() .range([height, 0]); //todo generate color rang based on amenities var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00", "#7b6844","#f45000","#f9fc30"]); var xaxis = d3.svg.axis() .scale(x0) .orient("bottom"); var yaxis = d3.svg.axis() .scale(y) .orient("left") .tickformat(d3.format(".2s")); 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.json("studentinteractionapi", function (error, data) { var amenityname = function () { var bool = true; var unique = []; (var = 0; < data.length - 1; i++) { (var x = 0; x < unique.length - 1; x++) { if (unique[x] == data[i]['amenity']) { bool = false; break; } bool = true; } if (bool == true) { unique.push(data[i]['amenity']); } } return unique; }; var amenitynames = amenityname(data); console.log(amenitynames); data.foreach(function (d) { d.amenities = amenitynames.map(function (name) { console.log( +d["sum"]); return {name: name, value: +d["sum"]}; }); }); x0.domain(data.map(function (d) { return d.userid; })); x1.domain(amenitynames).rangeroundbands([0, x0.rangeband()]); y.domain([0, d3.max(data, function (d) { return d3.max(d.amenities, function (d) { return d.value; }); })]); 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("population"); var state = svg.selectall(".state") .data(data) .enter().append("g") .attr("class", "g") .attr("transform", function (d) { return "translate(" + x0(d.userid) + ",0)"; }); state.selectall("rect") .data(function (d) { console.log(d.amenities); return d.amenities; }) .enter().append("rect") .attr("width", x1.rangeband()) .attr("x", function (d) { return x1(d.name); }) .attr("y", function (d) { return y(d.value); }) .attr("height", function (d) { return height - y(d.value); }) .style("fill", function (d) { return color(d.name); }); }); json sample [{"userid":1,"amenity":"time","sum":28}, {"userid":1,"amenity":"kiii","sum":33}, {"userid":1,"amenity":"food","sum":5}, {"userid":1,"amenity":"taste","sum":32}, {"userid":1,"amenity":"hmmmm","sum":10}, {"userid":1,"amenity":"max","sum":19.5}, {"userid":1,"amenity":"youtube","sum":10}, {"userid":1,"amenity":"f","sum":2.5}, {"userid":1,"amenity":"smith","sum":2.5},]
Comments
Post a Comment