d3 v4如何向条形图添加数据标签

时间:2016-10-30 16:01:21

标签: javascript html asp.net d3.js d3v4

我正在研究d3图表,我似乎无法将标签定位到条形图中的相应条形图上。我正在使用d3 v4。

http://orm-chimera-prod.s3.amazonaws.com/1230000000345/images/idvw_0902.png

这是理想的结果,y定位在我的例子中是理想的,但是x位置不会移动而且数据标签都粘在左边

<!-- Wrap these elements in the necessary html and bootstrap classes to achieve the desired layout as depicted in the Assignment instructions -->
<div class="flex col-md-2" id="table">
    <h2>Annual Sightings Table</h2>
    <div class="flex-container" data-ng-app="UFOSightingsApp">
        <div class="row">
            <div data-ng-controller="UFOSightingsController">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Year</th>
                            <th>Sighting Count</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr data-ng-repeat="s in sightings">
                            <td>{{s.sightingYear}}</td>
                            <td>{{s.sightingCount}}</td>
                        </tr>
                    </tbody>

                </table>
            </div>
        </div>
    </div>

</div>



<div class="flex col-md-8 col-md-offset-2" id="graph">
    <h2>Annual Sightings Graph</h2>
    <svg width="100%" height="600px" style="border: 1px solid #000">
    </svg>
</div>

<script src="Scripts/d3/d3.js"></script>

<script>
    var app = angular.module("UFOSightingsApp", []);

    app.controller("UFOSightingsController", function ($scope, $http) {

        $http.get("/api/UFOSightings").success(function (data) {
            $scope.sightings = data;
        });


    });



    var svg = d3.select("svg");

    var svgWidth = svg.style("width");
    var svgHeight = svg.style("height");
    var width = svgWidth.substring(0, svgWidth.length - 2) - 0;
    var height = svgHeight.substring(0, svgHeight.length - 2) - 65;

    var xScale = d3.scaleBand()
        .range([0, width])
        .padding(0.1);


    var yScale = d3.scaleLinear()
        .range([height, 0]);

    var xAxis = d3.axisBottom()
        .scale(xScale);


    var yAxis = d3.axisLeft()
        .scale(yScale);




    d3.json("SightingData.json", function (error, data) {

        if (error) throw error;

        data.forEach(function (d) {
            d.sightingCount = +d.sightingCount;
            d.sightingYear = d.sightingYear;
        });

        xScale.domain(data.map(function (d) { return d.sightingYear; }));
        yScale.domain([0, d3.max(data, function (d) { return d.sightingCount; })]);

        //draw the bars
        svg.selectAll("rect")
            .data(data)
            .enter()
            .append("rect")
            .attr("class", "bar")
            .attr("x", function (d) { return xScale(d.sightingYear); })
            .attr("width", xScale.bandwidth() - 2)
            .attr("y", function (d) { return yScale(d.sightingCount); })
            .attr("height", function (d) { return height - yScale(d.sightingCount); });

        //label the bars
        svg.selectAll("text")
            .data(data)
            .enter()
            .append("text")
            .text(function (d) { return d.sightingCount; })
            .attr("x", function (d) { return xScale(d.sightingYear) + xScale.range() / 2; })
            .attr("y", function (d) { return yScale(d.sightingCount) + 12; })
            .style("fill", "white");





        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);


        svg.append("g")
            .attr("class", "y axis")
            .call(yAxis);
        //.append("text")
        //.attr("transform", "rotate(-90)")
        //.attr("y", -36)
        //.attr("dy", ".71em")
        //.style("text-anchor", "end")
        //.text("Salary");

    });





</script>

我需要为我工作的部分适用于许多在线示例,其中“//标记条形图”位于

1 个答案:

答案 0 :(得分:1)

您正在使用scaleBand。因此,代替range(),它应该是bandwidth()

 svg.selectAll("text")
        .data(data)
        .enter()
        .append("text")
        .text(function (d) { return d.sightingCount; })
        .attr("x", function (d) { return xScale(d.sightingYear) + xScale.bandwidth() / 2; })
        .attr("y", function (d) { return yScale(d.sightingCount) + 12; })
        .style("fill", "white");