ไม่สามารถเพิ่มข้อความลงในแถบแนวนอน d3

ฉันใช้แผนภูมิแท่งแนวนอนต่อไปนี้ (http://bl.ocks.org/juan-cb/ab9a30d0e2ace0d2dc8c) ซึ่งจะอัปเดตตามการเลือกบางส่วน ฉันกำลังพยายามเพิ่มป้ายกำกับให้กับแท่งซึ่งจะแสดงค่าภายในแต่ละแท่ง ไม่แน่ใจว่าฉันผิดตรงไหน ในตอนแรกฉันมีองค์ประกอบ SVG rect ซึ่งฉันจัดกลุ่มไว้ภายใต้องค์ประกอบ "g" และลองวิธีนั้น แต่ก็ยังไม่มีโชค ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม!

JsFiddle - https://jsfiddle.net/b772s5mg/3/

จส

  datasetTotal = [
        {label:"Category 1", value:19},
        {label:"Category 2", value:5},
        {label:"Category 3", value:13},
        {label:"Category 4", value:17},
        {label:"Category 5", value:21},
        {label:"Category 6", value:25}
    ];

    datasetOption1 = [
        {label:"Category 1", value:22},
        {label:"Category 2", value:33},
        {label:"Category 3", value:4},
        {label:"Category 4", value:15},
        {label:"Category 5", value:36},
        {label:"Category 6", value:0}
    ];

    datasetOption2 = [
        {label:"Category 1", value:10},
        {label:"Category 2", value:20},
        {label:"Category 3", value:30},
        {label:"Category 4", value:5},
        {label:"Category 5", value:12},
        {label:"Category 6", value:23}
    ];

    d3.selectAll("input").on("change", selectDataset);

    function selectDataset()
    {
        var value = this.value;
        if (value == "total")
        {
            change(datasetTotal);
        }
        else if (value == "option1")
        {
            change(datasetOption1);
        }
        else if (value == "option2")
        {
            change(datasetOption2);
        }
    }

    var margin = {top: (parseInt(d3.select('body').style('height'), 10)/20), right: (parseInt(d3.select('body').style('width'), 10)/20), bottom: (parseInt(d3.select('body').style('height'), 10)/20), left: (parseInt(d3.select('body').style('width'), 10)/5)},
            width = parseInt(d3.select('body').style('width'), 10) - margin.left - margin.right,
            height = parseInt(d3.select('body').style('height'), 10) - margin.top - margin.bottom;

    var div = d3.select("body").append("div").attr("class", "toolTip");

    var formatPercent = d3.format("");

    var y = d3.scale.ordinal()
            .rangeRoundBands([height, 0], .2, 0.5);

    var x = d3.scale.linear()
            .range([0, width]);

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

    var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left");
    //.tickFormat(formatPercent);

    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 + ")");

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

    d3.select("input[value=\"total\"]").property("checked", true);
    change(datasetTotal);

    function change(dataset) {

        y.domain(dataset.map(function(d) { return d.label; }));
        x.domain([0, d3.max(dataset, function(d) { return d.value; })]);

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

        svg.select(".y.axis").remove();
        svg.select(".x.axis").remove();

        svg.append("g")
                .attr("class", "y axis")
                .call(yAxis)
                .append("text")
                .attr("transform", "rotate(0)")
                .attr("x", 50)
                .attr("dx", ".1em")
                .style("text-anchor", "end")
                .text("Option %");


        var bar = svg.selectAll(".bar")
                .data(dataset, function(d) { return d.label; })
        // new data:
        .enter().append("g").append("rect")
                .attr("class", "bar")
                .attr("x", function(d) { return x(d.value); })
                .attr("y", function(d) { return y(d.label); })
                .attr("width", function(d) { return width-x(d.value); })
                .attr("height", y.rangeBand());

                bar.append("text")
    .attr("x", function(d) { return x(d) - 3; })
    .attr("y", 30)
    .attr("dy", ".35em")
    .text(function(d) { return d.value; });


        var bars = d3.select("svg").selectAll("g.rects").data(dataset);

        // removed data:
        bars.exit().remove();

        // updated data:
        bars.transition()
                .duration(750)
                .attr("x", function(d) { return 0; })
                .attr("y", function(d) { return y(d.label); })
                .attr("width", function(d) { return x(d.value); })
                .attr("height", y.rangeBand());

    };

person sparta93    schedule 10.01.2017    source แหล่งที่มา


คำตอบ (2)


คุณอาจคิดว่าคุณกำลังเพิ่มองค์ประกอบ <text> ให้กับองค์ประกอบ <g> (กลุ่ม) แต่คุณไม่ได้เป็นเช่นนั้น!

ทันทีที่คุณทำเช่นนี้...

.enter().append("g").append("rect")

... ขณะนี้คุณกำลังพยายามเพิ่มองค์ประกอบข้อความต่อท้ายองค์ประกอบ <rect> และการดำเนินการนี้จะไม่ทำงาน

วิธีแก้ปัญหา: แบ่งตัวแปร bars ของคุณ:

//appending the <g> elements
var bar = svg.selectAll(".bar")
    .data(dataset, function(d) {
        return d.label;
    })
    .enter().append("g");

//now you append the <rect> to the <g>
bar.append("rect")
    .attr("class", "bar")
    .attr("x", function(d) {
        return x(d.value);
    })
    .attr("y", function(d) {
        return y(d.label);
    })
    .attr("width", function(d) {
        return width - x(d.value);
    })
    .attr("height", y.rangeBand());

//and then you append the <text> to the <g>
bar.append("text")
    .attr("x", function(d) {
        return x(d.value) - 3;
    })
    .attr("text-anchor", "end")
    .attr("y", function(d) {
        return y(d.label) + y.rangeBand() / 2;
    })
    .attr("dy", ".35em")
    .text(function(d) {
        return d.value;
    });

นี่คือซอที่อัปเดตของคุณ: https://jsfiddle.net/v8razxc8/

person Gerardo Furtado    schedule 10.01.2017
comment
ฉันได้ 404 บนซอ - person sparta93; 10.01.2017
comment
ลองอีกครั้ง. ฉันลบชื่อผู้ใช้ของฉันออกจากซอที่ฉันแยกเสมอ มีปัญหาบางอย่างเกิดขึ้นในกระบวนการนี้ - person Gerardo Furtado; 10.01.2017
comment
ขอบคุณ! ตอนนี้ฉันเห็นป้ายกำกับแล้ว แต่กราฟจะไม่อัปเดตตามการเลือกปุ่มตัวเลือก - person sparta93; 10.01.2017
comment
นั่นเป็นอีกคำถาม ปัญหาในคำถามนี้ได้รับการแก้ไขอย่างถูกต้องแล้ว ที่นี่ใน S.O. เราแนะนำให้หลีกเลี่ยงการตั้งคำถามหลายข้อในโพสต์เดียว หรือแย่กว่านั้นคือตั้งคำถามในความคิดเห็น ดังนั้น ให้ปิดคำถามนี้เมื่อแก้ไขแล้ว และโพสต์คำถาม อื่น เกี่ยวกับปัญหาอื่นนี้ - person Gerardo Furtado; 10.01.2017
comment
ใช่แล้ว ดีกว่า! คุณจะมีคนดูมากขึ้น ถ้าถามตรงนี้ในคอมเม้นท์คงเป็นคนเดียวที่อ่านนะครับ - person Gerardo Furtado; 10.01.2017
comment
โปรดทราบ ในกรณีที่คุณต้องการผ่านมัน ขอบคุณ. stackoverflow.com/questions/41560077/ - person sparta93; 10.01.2017

ฉันชอบที่จะเพิ่มทั้งการแก้ไขและข้อความภายในองค์ประกอบ g จากนั้นจึงแปลงการแปลเป็นตำแหน่ง https://jsfiddle.net/sjp700/b772s5mg/4/

 var bar = svg.selectAll(".bar")
                .data(dataset, function(d) { return d.label; })
        // new data:
       var bar_g = bar.enter().append("g")
             .attr("transform", function (d) { return "translate(" + x(d.value) + "," + y(d.label) + ")"; });


        bar_g.append("rect")
                .attr("class", "bar")            
                .attr("width", function(d) { return width-x(d.value); })
                .attr("height", y.rangeBand());


         bar_g.append("text")
           .attr("transform", function (d) { return "translate(" + 10 + "," + 10 + ")"; })
           .attr("y", 30)
           .attr("dy", ".35em")
           .text(function(d) { return d.value; 

});

person user3359706    schedule 10.01.2017