条形图和标签未正确排序

时间:2014-03-06 01:12:51

标签: svg d3.js

我是D3.js的新手,我正在尝试显示带有复选框的条形图进行排序,当运行代码时,我没有选中复选框,但条形图会自行排序并与标签。我无法识别出问题所在?

<script>
var margin = {
    top: 20,
    right: 20,
    bottom: 200,
    left: 40
},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

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

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


var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(10);

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


d3.csv("n.csv", type, function (error, data) {

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


    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)
        .selectAll("text")
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", "-.55em")
        .attr("transform", "rotate(-90)");

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

    svg.selectAll(".bar")
        .data(data)
        .enter()
        .append("rect")
        .attr("class", "bar")
        .attr("x", function (d) {
            return x(d.Agency_Name);
        })
        .attr("width", x.rangeBand())
        .attr("y", function (d) {
            return y(d.Cost_Variance_M);
        })
        .attr("height", function (d) {
            return height - y(d.Cost_Variance_M);
        });

    svg.selectAll(".text")
        .data(data)
        .enter()
        .append("text")
        .attr("class", "text")
        .attr("font-family", "sans-serif")
        .attr("font-size", "11px")
        .attr("fill", "black");

    d3.select("input").on("change", change);

    var sortTimeout = setTimeout(function () {
        d3.select("input").property("checked", true).each(change);
    }, 2000);

    function change() {
        clearTimeout(sortTimeout);


        var x0 = x.domain(data.sort(this.checked ? function (a, b) {
                return b.Cost_Variance_M - a.Cost_Variance_M;
            } : function (a, b) {
                return d3.ascending(a.Agency_Name, b.Agency_Name);
            })
            .map(function (d) {
                return d.Agency_Name;
            }))
            .copy();

        var transition = svg.transition().duration(750),
            delay = function (d, i) {
                return i * 50;
            };

        transition.selectAll(".bar")
            .delay(delay)
            .attr("x", function (d) {
                return x0(d.Agency_Name);
            });

        transition.selectAll(".text")
            .delay(delay)
            .attr("x", function (d) {
                console.log(d.Cost_Variance_M);
                return x0(d.Agency_Name);
            })
            .attr("y", function (d) {
                return height - (d.Cost_Variance_M);
            });

        transition.select(".x.axis")
            .call(xAxis)
            .selectAll("g")
            .delay(delay);
    }
});


function type(d) {
    d.Cost_Variance_M = d.Cost_Variance_M;
    return d;
}

1 个答案:

答案 0 :(得分:0)

尝试在将数据输入图表之前对数据进行排序

data.sort(function(a, b){
 return a.Agency_Name-b.Agency_Name
})