【问题标题】:d3 pie chart not displaying all labelsd3饼图不显示所有标签
【发布时间】:2015-11-01 13:41:11
【问题描述】:

我正在尝试一个简单的饼图,其中切片内有标签。我可以显示标签,但不是全部。例如在示例代码中,我有 Rick 5%、Paul 4% 和 Steve 3% 没有显示,因为切片的尺寸很小。我该如何克服这个问题?

<html>
<head>    
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Testing Pie Chart</title>
    <!--<script type="text/javascript" src="d3/d3.v2.js"></script>-->
    <script src="../js/d3.min.js" type="text/javascript"></script>
    <style type="text/css">

        #pieChart {    
            position:absolute;
            top:10px;
            left:10px;
            width:400px;
            height: 400px; 
        }

        #lineChart {    
            position:absolute;
            top:10px;
            left:410px;
            height: 150px;
        }

        #barChart {
            position:absolute;
            top:160px;
            left:410px;
            height: 250px;
        }

        .slice {
            font-size: 8pt;
            font-family: Verdana;
            fill: white; //svg specific - instead of color
            font-weight: normal ;   
        } 

        /*for line chart*/
        .axis path, .axis line {
            fill: none;
            stroke: black;
            shape-rendering: crispEdges; //The shape-rendering property is an SVG attribute, used here to make sure our axis and its tick mark lines are pixel-perfect. 
        }

        .line {
            fill: none;
            /*stroke: steelblue;*/
            stroke-width: 3px;
        }

        .dot {
            /*fill: white;*/
            /*stroke: steelblue;*/
            stroke-width: 1.5px;
        }


        .axis text {
            font-family: Verdana;
            font-size: 11px;
        }

        .title {
            font-family: Verdana;
            font-size: 15px;    

        }

        .xAxis {
            font-family: verdana;
            font-size: 11px;
            fill: black;
        }  

        .yAxis {
            font-family: verdana;
            font-size: 11px;
            fill: white;
        }


        table {
            border-collapse:collapse;
            border: 0px;    
            font-family: Verdana;   
            color: #5C5558;
            font-size: 12px;
            text-align: right;          
        }

        td {
            padding-left: 10px;     
        }

        #lineChartTitle1 {
            font-family: Verdana;
            font-size  : 14px;
            fill       : lightgrey;
            font-weight: bold;
            text-anchor: middle;
        }

        #lineChartTitle2 {
            font-family: Verdana;
            font-size  : 72px;
            fill       : grey;
            text-anchor: middle;
            font-weight: bold;
            /*font-style: italic;*/
        }

    </style>

</head>
<body>

        var formatAsPercentage = d3.format("%"),
                formatAsPercentage1Dec = d3.format(".1%"),
                formatAsInteger = d3.format(","),
                fsec = d3.time.format("%S s"),
                fmin = d3.time.format("%M m"),
                fhou = d3.time.format("%H h"),
                fwee = d3.time.format("%a"),
                fdat = d3.time.format("%d d"),
                fmon = d3.time.format("%b")
                ;


        function dsPieChart() {

            var dataset = [
                {category: "Tom", measure: 0.30},
                {category: "John", measure: 0.30},
                {category: "Martin", measure: 0.30},
                {category: "Sam", measure: 0.30},
                {category: "Peter", measure: 0.25},
                {category: "Johannes", measure: 0.15},
                {category: "Rick", measure: 0.05},
                {category: "Lenny", measure: 0.18},
                {category: "Paul", measure: 0.04},
                {category: "Steve", measure: 0.03}
            ]
                    ;

            var width = 400,
                    height = 400,
                    outerRadius = Math.min(width, height) / 2,
                    innerRadius = outerRadius * .999,
                    // for animation
                    innerRadiusFinal = outerRadius * .5,
                    innerRadiusFinal3 = outerRadius * .45,
                    color = d3.scale.category20()    //builtin range of colors
                    ;

            var vis = d3.select("#pieChart")
                    .append("svg:svg")              
                    .data([dataset])                  
                    .attr("width", width)          
                    .attr("height", height)
                    .append("svg:g")                
                    .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")")   
                    ;

            var arc = d3.svg.arc()             
                    .outerRadius(outerRadius).innerRadius(innerRadius);

            // for animation
            var arcFinal = d3.svg.arc().innerRadius(innerRadiusFinal).outerRadius(outerRadius);
            var arcFinal3 = d3.svg.arc().innerRadius(innerRadiusFinal3).outerRadius(outerRadius);

            var pie = d3.layout.pie()           
                    .value(function (d) {
                        return d.measure;
                    });   

            var arcs = vis.selectAll("g.slice")     
                    .data(pie)                          
                    .enter()                            
                    .append("svg:g")                
                    .attr("class", "slice")    
                    .on("mouseover", mouseover)
                    .on("mouseout", mouseout)
                    .on("click", up)
                    ;

            arcs.append("svg:path")
                    .attr("fill", function (d, i) {
                        return color(i);
                    }) 
                    .attr("d", arc)     
                    .append("svg:title") 
                    .text(function (d) {
                        return d.data.category + ": " + formatAsPercentage(d.data.measure);
                    });

            d3.selectAll("g.slice").selectAll("path").transition()
                    .duration(750)
                    .delay(10)
                    .attr("d", arcFinal)
                    ;

            arcs.filter(function (d) {
                return d.endAngle - d.startAngle > .2;
            })
                    .append("svg:text")
                    .attr("dy", ".35em")
                    .attr("text-anchor", "middle")
                    .attr("transform", function (d) {
                        return "translate(" + arcFinal.centroid(d) + ")rotate(" + angle(d) + ")";
                    })

                    .text(function (d) {
                        return d.data.category;
                    })
                    ;

            function angle(d) {
                var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
                return a > 90 ? a - 180 : a;
            }


            // Pie chart title          
            vis.append("svg:text")
                    .attr("dy", ".35em")
                    .attr("text-anchor", "middle")
                    .text("Revenue Share 2012")
                    .attr("class", "title")
                    ;


            function mouseover() {
                d3.select(this).select("path").transition()
                        .duration(750)
                        //.attr("stroke","red")
                        //.attr("stroke-width", 1.5)
                        .attr("d", arcFinal3)
                        ;
            }

            function mouseout() {
                d3.select(this).select("path").transition()
                        .duration(750)
                        //.attr("stroke","blue")
                        //.attr("stroke-width", 1.5)
                        .attr("d", arcFinal)
                        ;
            }

            function up(d, i) {

                updateBarChart(d.data.category, color(i));
                updateLineChart(d.data.category, color(i));

            }
        }

        dsPieChart();


    </script>
</body>

【问题讨论】:

标签: javascript d3.js


【解决方案1】:

这行代码决定了哪些切片将标签文本附加到它们:

arcs.filter(function (d) {
    return d.endAngle - d.startAngle > .2;
})
.append("svg:text")...

所以总弧度小于0.2弧度的切片会被过滤掉,标签文字不会被添加。

您可以只减少过滤器值,以在更薄的切片上显示标签(例如,将本例中的 .2 更改为 .05)以获得您想要的效果:

【讨论】:

    【解决方案2】:

    您的非常小的数据没有被显示,因为它被此代码过滤掉了。如果你去掉这段代码,那么那些小数据也会显示出来。

        arcs.filter(function (d) {
            return d.endAngle - d.startAngle > .2;
        })
    

    但是标签在小数据中显示不清晰。在饼图之外显示标签会使标签显示得更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2017-10-24
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多