gpt4 book ai didi

NVD3.js - 如何向堆积图中的每个条形添加条形文本?

转载 作者:行者123 更新时间:2023-12-02 09:29:30 25 4
gpt4 key购买 nike

我在 NVD3/AngularJS 中创建了一个多条形图。我想在每个矩形条内显示文本及其值,如下面的 JSON 所示。

如何在每个条形图中显示文本值?

NVD3 图表定义

multiBarChart: {
options: function(){
return {
chart: {
type: 'multiBarChart',
stacked: true,
x: function(d){return d.x;},
y: function(d){return d.y;},
text: function(d){return d.x;},
showLabels: true,
showLegend: false,
transitionDuration: 500,
forceX: ["Team", "Meeting", "Phase", "Source"],
xAxis: {
axisLabel: 'Category',
axisLabelDistance: -8
},
yAxis: {
axisLabel: 'Number Of Action Items',
}
}
}
},
data: categoryChartData
}

JSON 数据(categoryChartData)

  [ 
{"values" : [
{
"y" :10,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Team1"
},

{"values" : [
{
"y" :5,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Team2"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 7,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Meeting1"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 3,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Meeting2"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :9,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Phase1"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :5,
"x" : "Phase"
}, {
"y" : 0,
"x" : "Source"
}
],
"key" : "Phase1"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 2,
"x" : "Source"
}
],
"key" : "Internal"
},
{"values" : [
{
"y" :0,
"x" : "Team"
}, {
"y" : 0,
"x" : "Meeting"
},
{
"y" :0,
"x" : "Phase"
}, {
"y" : 1,
"x" : "Source"
}
],
"key" : "Customer"
}


];

Category Stacked Chart

最佳答案

由于动画堆叠条形图的一些复杂性,Angular-nvd3 本身不会对多条形图执行此操作,但它会对离散条形图执行此操作,如 How to display values in Stacked Multi-bar chart - nvd3 Graphs探索过。然而,在对该问题的回答的更新中,@Topicus 链接到 a gist they wrote这可以实现您正在寻找的目标。

我根据您的情况调整了要点;你可以在 this Plunker 中看到结果。如果标签显示有点不稳定,您可以稍微调整一下格式。关键是动画完成后需要附加标签,因此我设置的超时等于(也可以略大于)transitionDuration 图表属性的值。我还删除了所有零值,这样它们就不会掩盖非零值。

$scope.options = {
chart: {
type: 'multiBarChart',
height: 500,
transitionDuration: 500,
...
}
};

$scope.data...

$timeout(function () {
d3.selectAll('.nv-multibar .nv-group').each(function(group){
var g = d3.select(this);

// Remove previous labels if there is any
g.selectAll('text').remove();
g.selectAll('.nv-bar').each(function(bar) {
var b = d3.select(this);
var barWidth = b.attr('width');
var barHeight = b.attr('height');

g.append('text')
// Transforms shift the origin point then the x and y of the bar
// is altered by this transform. In order to align the labels
// we need to apply this transform to those.
.attr('transform', b.attr('transform'))
.text(function() {
// No decimals format and eliminate zero values
if (bar.y === 0) {
return;
}
return parseFloat(bar.y).toFixed(0);
})
.attr('y', function() {
// Center label vertically
var height = this.getBBox().height;
return parseFloat(b.attr('y')) + 15; // 15 is the label's margin from the top of bar
})
.attr('x', function() {
// Center label horizontally
var width = this.getBBox().width;
return parseFloat(b.attr('x')) + (parseFloat(barWidth) / 2) - (width / 2);
})
.style("stroke","black")
.attr('class', 'bar-values');
});
});
}, 500);

关于NVD3.js - 如何向堆积图中的每个条形添加条形文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com