d3.js - Data labels on the bars -
i have been trying data labels appear on bars of d3 chart have built. no joy. have tried svg.selectall("text")... no error , no labels.
i have created codepen example
function arbbymonthdashboard2(targetdiv, monthtoreport) { var color_hash = { 1 : ["january", "green"], 2 : ["february", "orange"], 3 : ["march", "aquamarine"], 4 : ["april", "blue"], 5 : ["may", "yellow"], 6 : ["june", "silver"], 7 : ["july", "antiquewhite"], 8 : ["august", "cyan"], 9 : ["september", "blueviolet"], 10 : ["october", "black"], 11 : ["november", "cadetblue"], 12 : ["december", "red"] } var chartdata = { "data": [ { monthnum: 4, month: '4-2014', arbs: 4 }, { monthnum: 5, month: '5-2014', arbs: 4 }, { monthnum: 6, month: '6-2014', arbs: 4 }, { monthnum: 7, month: '7-2014', arbs: 5 }, { monthnum: 8, month: '8-2014', arbs: 4 }, { monthnum: 9, month: '9-2014', arbs: 8 }, { monthnum: 10, month: '10-2014', arbs: 12 }, { monthnum: 11, month: '11-2014', arbs: 6 }, { monthnum: 12, month: '12-2014', arbs: 16 }, { monthnum: 1, month: '1-2015', arbs: 6 }, { monthnum: 2, month: '2-2015', arbs: 10 }, { monthnum: 3, month: '3-2015', arbs: 10 }, { monthnum: 4, month: '4-2015', arbs: 13 }, { monthnum: 5, month: '5-2015', arbs: 13 } ]}; var width = 400; var height = 900; var margin = {top: 20, right: 20, bottom: 70, left: 40}, width = 600 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parsedate = d3.time.format("%m-%y").parse; var x = d3.scale.ordinal().rangeroundbands([0, width], .05); var y = d3.scale.linear().range([height, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom") .tickformat(d3.time.format("%m-%y")); var yaxis = d3.svg.axis() .scale(y) .orient("left") .ticks(10); var svg = d3.select("#divchart").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 + ")"); chartdata.data.foreach(function(d) { d.month = parsedate(d.month); d.arbs = +d.arbs; } ); x.domain(chartdata.data.map(function(d) { return d.month; })); y.domain([0, d3.max(chartdata.data, function(d) { return d.arbs;})]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis) .selectall("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", "-.55em") .attr("transform", "rotate(-90)" ); 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("total arbs"); svg.selectall("bar") .data(chartdata.data) .enter().append("rect") .style("fill", function(d) { var color = color_hash[d.monthnum][1]; return color; }) .attr("class", "bar") .attr("x", function(d) { return x(d.month); }) .attr("width", x.rangeband()) .attr("y", function(d) { return y(d.arbs); }) .attr("height", function(d) { return height - y(d.arbs); }); };
i'm not sure understand ask, sorry.
you can add text on bar creating new selection on data :
svg.selectall("text.legend") .data(chartdata.data) .enter().append("text") .attr("class", "legend") .attr("x", function(d) { return x(d.month) + x.rangeband() / 2 ; }) .text(function(d){return d.arbs}) .attr('y', height - 10);
you have codepen here: http://codepen.io/anon/pen/ejkwjl
hope helps,
regards
Comments
Post a Comment