我一直试图让数据标签显示在我构建的d3图表的条形图上。没有快乐。我试过svg.selectAll(" text")...但没有错误,也没有标签。
我创建了一个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); });
};
答案 0 :(得分:0)
我不确定你明白了什么,抱歉。
您可以通过在数据上创建新选项来在栏上添加文字:
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);
这里有一个codepen:http://codepen.io/anon/pen/EjKwjL
希望它有所帮助,
此致