简体   繁体   中英

Add a label to the bars in a vertical bar chart in D3 and delete Y-axis

My question is I want to delete the Y-axis and instead place the Y value as a label outside the bar. How do I do this? Note I set the Y axis to 40 because I plan on adding another graph to the right and need them to have the Y-axis on the same scale. Also, how do I make the width of the bars smaller. I don't want them to touch. I want there to be white space in between the bars.

Here is the data and code below for the vary basic graph I am trying to fix

var data = {
"age": ["25-29", "30-34", "35-39", "40-44", "45-50"],
"value": [28, 15, 11, 10, 8]
}

draw(data);

function draw(data) {
var margin = {
        "top": 10,
        "right": 10,
        "bottom": 30,
        "left": 50
    },
    width = 700,
    height = 300;

var x = d3.scale.ordinal()
    .domain(data.age.map(function(d) {
        return d.substring(0, 5);}))
    .rangeRoundBands([0, width], 0);


var y = d3.scale.linear()
    .domain([0, 40/*d3.max(data.value)*/])
    .range([height, 0]);

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

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

var svgContainer = d3.select("#root").append("svg")
    .attr("class", "chart")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom).append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.right + ")");

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

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

svgContainer.selectAll(".bar").data(data.value).enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d, i) {
        return i * x.rangeBand();
    })
    .attr("y", function(d) {
        return y(d);
    })
    .attr("width", function(){
        return x.rangeBand();
    })
    .attr("height", function(d) {
        return height -y(d);
    });
}

You don't have to create y-axis if you don't want to show it.

For text labels, you can create them like this:

svg.selectAll(".label")
    .data(data)
    .enter().append("svg:text")
        .attr("class", "label")
        .attr("x", function(d) {
            return x(d[X_AXIS_COLUMN]) + x.rangeBand() / 3;
        })
        .attr("y", function(d) {
            return y(d[Y_AXIS_COLUMN]) - 5;
        })
        .text(function(d) {
            return Y_DATA_FORMAT(d[Y_AXIS_COLUMN]);
        });

Here's my example:

http://vida.io/documents/bYRhZDRS7MGrArSjC

For your 2nd question, play with this block to get where you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM