javascript - Label is not coming in proper position in D3 Bar graphs -
i have following problems bar chart.how set decimal label value in proper position in bar graph.label should not overlap in nearest bar.but in case it's overlapping.please suggest how correct below code
var data = [ { request: 1, avgrequest: 4123.18 }, { request: 2, avgrequest: 5221.16 }, { request: 3, avgrequest: 32.42 }, { request: 4, avgrequest: 22.13 }, { request: 5, avgrequest: 413.21 }, { request: 6, avgrequest: 112.19 } ]; var margin = { top: 40, right: 40, bottom: 35, left: 85 }, width = 450 - margin.left - margin.right, height = 250 - margin.top - margin.bottom; var formatpercent = d3.format(".0%"); var x = d3.scale.ordinal() .rangeroundbands([0, width], .1); 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 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 + ")"); data.foreach(function (d) { d.request = d.request; d.avgrequest = +d.avgrequest; }); x.domain(data.map(function (d) { return d.request; })); y.domain([0, d3.max(data, function (d) { return d.avgrequest; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis); // xaxis label svg.append("text") .attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom + 5) + ")") .style("text-anchor", "middle") .text("numbers of request"); //yaxis label svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 0 - margin.left) .attr("x", 0 - (height / 2)) .attr("dy", "1em") .style("text-anchor", "middle") .text("avg request); // title svg.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top / 2)) .attr("text-anchor", "middle") .style("font-size", "16px") .style("text-decoration", "underline") .text("avg"); svg.append("g") .attr("class", "y axis") .call(yaxis); svg.selectall(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function (d) { return x(d.request); }) .attr("width", x.rangeband()) .attr("y", function (d) { return y(d.avgrequest); }) .attr("height", function (d) { return height - y(d.avgrequest); }); var text = svg.selectall("text1") .data(data) .enter() .append("text") .attr("class", function (d) { return "label " + d.request; }) .attr("x", function (d, i) { return x(i) + x.rangeband() / 5; }) .attr("y", function (d, i) { return y(d.avgrequest) + 25; }) .text(function (d) { return d.avgrequest; }) .attr("font-size", "15px") .style("stroke", "black");
i added 2 new variables separate dimensions of complete chart , plot area:
var margin = { top: 40, right: 40, bottom: 35, left: 85 }, width = 450, height = 250; plotareawidth = width - margin.left - margin.right, plotareaheight = height - margin.top - margin.bottom; and changed other code accordingly.
and changes text labels:
var text = svg.selectall("text1") .data(data) .enter() .append("text") .attr("class", function (d) { return "label " + d.request; }) .attr("x", function (d, i) { //return x(i) + x.rangeband() / 2; return x(d.request); }) .attr("y", function (d, i) { //return y(d.avgrequest) + 25; return y(d.avgrequest) - 5; }) .text(function (d) { return d.avgrequest; }) .attr("font-size", "15px") .style("stroke", "black"); changed example at jsbin.
Comments
Post a Comment