gpt4 book ai didi

javascript - 如何使用 dc.js 和 d3.js 显示值堆叠条形图?

转载 作者:行者123 更新时间:2023-11-30 11:50:41 25 4
gpt4 key购买 nike

Based on this question ,我能够使用带有标签的 dc.js 和 d3.js 创建条形图。但是,我将条形图变成了堆叠条形图(基于 this documentation ),并且我很难显示每个堆叠的标签。

例如下图中的每个堆栈都应该显示数字,但它显示的是 [object Object]

enter image description here

请参阅下面的代码和此处的 codepen

Javascript

var data = [{Category: "A", ID: "1A"}, {Category: "A", ID: "1A"}, {Category: "A", ID: "1A"}, {Category: "A", ID: "2B"}, {Category: "A", ID: "2B"}, {Category: "B", ID: "1A"}, {Category: "B", ID: "1A"}, {Category: "B", ID: "1A"}, {Category: "B", ID: "2B"}, {Category: "B", ID: "3C"}, {Category: "B", ID: "3C"}, {Category: "B", ID: "3C"}, {Category: "B", ID: "4D"}, {Category: "C", ID: "1A"}, {Category: "C", ID: "2B"}, {Category: "C", ID: "3C"}, {Category: "C", ID: "4D"}, {Category: "C", ID: "4D"}, {Category: "C", ID: "5E"}];

var ndx = crossfilter(data);

var XDimension = ndx.dimension(function (d) {return d.Category;});



var YDimension = XDimension.group().reduce(
function reduceAdd(p, d) {
p[d.ID] = (p[d.ID]|| 0) + 1;
return p;
},
function reduceRemove(p, d) {
p[d.ID] = (p[d.ID]|| 0) -1;
return p;
},
function reduceInitial() {
return {};})


dc.barChart("#Chart")
.width(480).height(300)
.dimension(XDimension)
.group(YDimension,"1A",function(d) {return d.value["1A"];})
.stack(YDimension,"2B",function(d) {return d.value["2B"];})
.stack(YDimension,"3C",function(d) {return d.value["3C"];})
.stack(YDimension,"4D",function(d) {return d.value["4D"];})
.stack(YDimension,"5E",function(d) {return d.value["5E"];})
.transitionDuration(500)
.xUnits(dc.units.ordinal)
.x(d3.scale.ordinal().domain(XDimension))
.renderlet(function (chart) {

//Check if labels exist
var gLabels = chart.select(".labels");
if (gLabels.empty()){
gLabels = chart.select(".chart-body").append('g').classed('labels', true);
}

var gLabelsData = gLabels.selectAll("text").data(chart.selectAll(".bar")[0]);

gLabelsData.exit().remove(); //Remove unused elements

gLabelsData.enter().append("text") //Add new elements

gLabelsData
.attr('text-anchor', 'middle')
.attr('fill', 'white')
.text(function(d){
text_object = d3.select(d).data()[0].data.value
return text_object
})
.attr('x', function(d){
return +d.getAttribute('x') + (d.getAttribute('width')/2);
})
.attr('y', function(d){ return +d.getAttribute('y') + 15; })
.attr('style', function(d){
if (+d.getAttribute('height') < 18) return "display:none";
});

})


dc.renderAll();

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dc/1.7.0/dc.css" media="screen">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<script src="https://rawgithub.com/NickQiZhu/dc.js/master/web/js/crossfilter.js"></script>
<script src="https://cdnjs.site44.com/dc3.js"></script>
<script src="https://rawgithub.com/NickQiZhu/dc.js/master/web/js/crossfilter.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<body>
<div id='Chart'></div>
</body>

最佳答案

通常我在这些情况下所做的是在有问题的行上放置一个断点,然后四处查看对象是否包含我在其他地方想要的数据。

看起来你可以在这里使用 .datum().y:

    text_object =  d3.select(d).datum().y

http://codepen.io/gordonwoodhull/pen/EgNvNr?editors=1010

enter image description here

这仍然是一种奇怪的代码(使用 DOM 元素作为数据来连接其他一些元素是很老套的)但希望这会让你感动。

关于javascript - 如何使用 dc.js 和 d3.js 显示值堆叠条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39581885/

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