javascript - d3 堆积条形图值未显示在图表上

标签 javascript charts d3.js

所以我创建了一个 D3 堆积条形图,但我似乎不知道如何使条形的值显示在图表上。我可以在每个栏上显示文本“Sample”,但我不知道如何检索数据。我尝试使用这个... .text(function(d) {return d.total; }) 但没有运气。

在这里jfiddle... http://jsfiddle.net/rasweat/D3ErQ/1/

<script src="http://d3js.org/d3.v3.js"></script>
                        <script type="text/javascript">
                            var margin = {top: 60, right: 20, bottom: 100, left: 100},
                                width = 600 - margin.left - margin.right,
                                height = 400 - margin.top - margin.bottom;

                            var x = d3.scale.ordinal()
                                .rangeRoundBands([0, width-100], .1); //width-100 to make room for the legend.

                            var y = d3.scale.linear()
                                .rangeRound([height, 0]);

                            var color = d3.scale.ordinal()
                                //.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
                                .range(["#1f77b4", "#ff7f0e","d62728"]); //blue, orange, red
                                //color code for Progress Report
                                //.range(["#00FFFF","#00FF00","#990099","#FF0000","#FFFF00"]);

                            var xAxis = d3.svg.axis()
                                .scale(x)
                                .orient("bottom");

                            var yAxis = d3.svg.axis()
                                .scale(y)
                                .orient("left")
                                .tickFormat(d3.format(".2s"));

                            var svg = d3.select("#area_progress_report").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 + ")");

                                // Get the data
                            //var data = [{"Commodity":"Base","num_complete_print":"3","num_incomplete_print":15},{"Commodity":"Blade","num_complete_print":"1","num_incomplete_print":53},{"Commodity":"DTE","num_complete_print":"1","num_incomplete_print":17},{"Commodity":"HUB","num_complete_print":"0","num_incomplete_print":"18"},{"Commodity":"MH","num_complete_print":"0","num_incomplete_print":"18"},{"Commodity":"Mid","num_complete_print":"0","num_incomplete_print":18},{"Commodity":"Top","num_complete_print":"0","num_incomplete_print":18}];
                            var data = <?php echo json_encode($dataset_progress001); ?>;                                
                            //alert(data);
                            //d3.csv("data.csv", function(error, data) {
                              color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Commodity"; }));

                              data.forEach(function(d) {
                                var y0 = 0;
                                d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
                                d.total = d.ages[d.ages.length - 1].y1;
                              });

                              //use this to sort the bars from largest to smallest
                              //data.sort(function(a, b) { return b.total - a.total; });

                              x.domain(data.map(function(d) { return d.Commodity; }));
                              y.domain([0, d3.max(data, function(d) { return d.total; })]);

                              svg.append("g")
                                  .attr("class", "x axis")
                                  .attr("transform", "translate(0," + height + ")")
                                  .call(xAxis)
                                  .selectAll("text")  //added this line through rotate to change orientation of x axis
                                    .style("text-anchor", "end")
                                    .attr("dx", "-.8em")
                                    .attr("dy", "-1em")
                                    .attr("transform", function(d) {
                                        return "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("Population");

                                //grid lines  y.ticks controls the number of lines
                                svg.selectAll("line.horizontalGrid").data(y.ticks(10)).enter()
                                    .append("line")
                                        .attr(
                                        {
                                            "class":"horizontalGrid",
                                            "x1" : 0,
                                            "x2" : width-60,
                                            "y1" : function(d){ return y(d);},
                                            "y2" : function(d){ return y(d);},
                                            "fill" : "none",
                                            "shape-rendering" : "crispEdges",
                                            "stroke" : "grey",
                                            "stroke-width" : "1px"
                                        });

                              var state = svg.selectAll(".state")
                                  .data(data)
                                .enter().append("g")
                                  .attr("class", "g")
                                  .attr("transform", function(d) { return "translate(" + x(d.Commodity) + ",0)"; });

                              state.selectAll("rect")
                                  .data(function(d) { return d.ages; })
                                .enter().append("rect")
                                  .attr("width", x.rangeBand())
                                  .attr("y", function(d) { return y(d.y1); })
                                  .attr("height", function(d) { return y(d.y0) - y(d.y1); })
                                  .style("fill", function(d) { return color(d.name); });

                              var legend = svg.selectAll(".legend")
                                  .data(color.domain().slice().reverse())
                                .enter().append("g")
                                  .attr("class", "legend")
                                  .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

                              legend.append("rect")
                                  .attr("x", width - 18)
                                  .attr("width", 18)
                                  .attr("height", 18)
                                  .style("fill", color);

                              legend.append("text")
                                  .attr("x", width - 24)
                                  .attr("y", 9)
                                  .attr("dy", ".35em")
                                  .style("text-anchor", "end")
                                  .text(function(d) { return d; });

                            //Added y label 10/28
                            svg.append("text")
                                .attr("class", "y label")
                                .attr("text-anchor", "end")
                                .attr("y", -60)
                                .attr("x",-70)
                                .attr("dy", ".75em")
                                .attr("transform", "rotate(-90)")
                                .text("Number Of Components");

                            //Add Title
                            svg.append("text")
                                .attr("x", (width/2) )//(width / 2))             
                                .attr("y", -20) //0 - (margin.top / 2))
                                .attr("text-anchor", "middle")  
                                .style("font-size", "20px") 
                                .style("text-decoration", "underline")  
                                .text("Inspection Progress Report");

                            state.selectAll("text")
                              .data(function(d) { return d.ages; })
                              .enter()
                              .append("text")
                              .attr("x", x.rangeBand()/2)
                              .attr("y", function(d, i) { return y(d.y1) + (y(d.y0) - y(d.y1))/2; })
                              .style("text-anchor", "middle")
                              //.text(function(d) {return d.total; })
                              .text("sample")
                        </script>

最佳答案

您必须更改此行

.data(function(d) { return d.ages; })

实际提供data()您的数据。如果您有一个包含所有名为 ages 的年龄的数组,那么它将是:

.data(ages)

function(d) 中的 d 参数没有引用任何内容,因为 data() 实际上是您输入数据的方法。

关于javascript - d3 堆积条形图值未显示在图表上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019454/

相关文章:

svg - 获取箭头指向D3中节点的外部边缘

javascript - 如何从 extjs 4.2 网格获取行元素

javascript - Raty jQuery 插件无法正常工作 Rails 5

angular - ng2-charts & chart.js - 更改背景颜色

datetime - 日期时间轴作为字符串工作,而不是 Zing Chart 中正确的连续日期/时间轴

javascript - 如何使用 d3.js 沿 GeoJSON 路径为对象设置动画?

javascript - 使用 jQuery 选择器选择 JavaScript 数组元素项

javascript - 如何通过 javascript 将 css 类附加到元素?

javascript - 如何反转 Sencha ExtJS 图表中的轴?

javascript - D3.js - DOM 文本不正确退出