D3 adding text lables to horizontal stacked barchart

user2259146

Hi Thanks to FernOfTheAndes for answering this. I have another query. Please see FIDDLE and original question below.

My question is, how do I add text labels to the example below. Ideally I would like the values to be displayed in the rectangles. However, for stacked bar graphs the process seems to be different in that, nested array information doesn't seem to be picked up.

For example

var dataset = [
[{
    x: 0,
    y: 20 // test with different values
}],
[{
    x: 0,
    y: 30 // test with different values
}],

[{
    x: 0,
    y: 50 // test with different values
}]

];

How do I get 20, 30 , 50 to be displayed in their respective rectangles?

Grateful if someone could update the FIDDLE or paste the full code of what I need to do in order to add the text labels.

Original code below: D3 Horizontal stacked bar chart axis cut off

FIDDLE: http:// jsfiddle.net/zDkWP/

Many thanks,

FernOfTheAndes

Here is what you need to do. Since you already have groups as your nodes, you just need to append text elements to them, using the same scale that you used for the rectangles. To place your text within the rectangles, you will need a little offset on both x and y, particularly on y. For demo purposes, I hardcoded an offset for the y by adding 20 to it, but you should user smarter offsets for both dimensions.

var text = groups.selectAll("text")
    .data(function (d) {
        return d;
    })
  .enter()
  .append("text")
    .attr("class","text")
    .attr("x", function (d) {
        return xScale(d.y0); // could use an offset here as well...
    })
    .attr("y", 70) // just added 20 as an offset...for demo purposes
    .text(function(d) { return d.y;});

And a little CSS to help:

.text {
    fill: white;
}

Updated FIDDLE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

D3, Adding Tooltip to Horizontal Barchart, tooltip not showing

From Dev

D3 Horizontal stacked bar chart axis cut off

From Dev

Text on horizontal bar chart - d3

From Dev

Text on horizontal bar chart - d3

From Dev

D3 adding dots to multiple stacked line charts

From Dev

D3js Stacked Barchart width is not propper

From Dev

d3.js stacked barchart not drawing along x axis?

From Dev

Adding text to d3 circle

From Dev

d3 + adding text labels + formatting

From Dev

Adding text to d3 bars

From Dev

Adding html code to D3 text

From Dev

Adding a horizontal line to d3 graph displays at the wrong value

From Dev

making a stacked barchart in pandas

From Dev

D3.js Barchart, split text

From Dev

D3.js Barchart, split text

From Dev

D3 horizontal tree layout with rect and text wrapping

From Dev

Cannot add text to d3 horizontal bars

From Dev

MPAndroidChart BarChart horizontal / vertical

From Dev

MPAndroidChart customizing horizontal BarChart

From Dev

Stacked ggplot percent barchart in shiny

From Dev

Stacked barchart in highcharter with molten data

From Dev

Adding text to the center of a D3 Donut Graph

From Dev

d3 foreignObjects, adding checkbox and text from data

From Dev

Adding more text to d3 pie chart on mouseover event

From Dev

Adding more text to d3 pie chart on mouseover event

From Dev

Adding text to collapsible d3 tree links

From Dev

D3.js Stacked Bar Chart, from vertical to horizontal

From Dev

Convert vertical stacked bar to horizontal stacked bar D3.js v4

From Dev

Add brush on top of d3 barchart