score:0
Accepted answer
What you're trying to do doesn't work because you're using the wrong scale/ticks with your second axis. The scale is ordinal, but the tick values are numeric, hence you're getting incorrect x
positions.
It's much easier if you don't (ab)use an axis to do this. All you have to do is append text elements as follows.
svg.append("g").selectAll("text")
.data(jsonDataResult).enter()
.append("text")
.attr("dx", "0.1em")
.attr("dy", "-0.5em")
.attr("x", function(d) { return x(d.Key); })
.attr("y", function(d) { return y(d.Stack1 + d.Stack2 + d.Stack3); })
.text(function(d) { return (d.Stack1 + d.Stack2 + d.Stack3); });
score:0
I modified the code a little so the lables are just above the stacked bar.
svg.append("g").selectAll("text")
.data(jsonDataResult).enter()
.append("text")
.attr("dx", "0.1em")
.attr("dy", "-0.5em")
.attr("x", function (d) { return x(d.Key); })
.attr("y", function (d) {
return (-((height / yStackMax) * (d.Stack1 + d.Stack2 + d.Stack3)))+height;
})
.text(function (d) { return (d.Stack1 + d.Stack2 + d.Stack3); });
Source: stackoverflow.com
Related Query
- d3 labels on stack bar position
- How to create horizontal bar stack and align the labels differently
- Why does removing, then re-adding bars to my stack bar chart change the position of the bars?
- How can I position rotated x-axis labels on column chart using nvd3?
- c3js position of data labels
- Set custom x-axis labels in d3 bar charts?
- d3.js bar chart sorting: can't figure out how to sort x-axis labels along with bars
- Add labels to bar chart D3
- Ordering a bar chart labels in dc.js (again)
- border for d3 stack bar chart on selection
- How to update data in stack bar chart in D3
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- x axis labels overlapping for grouped category bar chart in d3
- Unneeded white space before the 1st bar in D3 Stack chart
- d3 show labels only for ticks with data in a bar chart
- How to position NVD3 line graph above all other area / bar graphs?
- Redrawing the stack bar chat on click on the toggle legend using D3.js
- Adding text labels in middle of each bar - stacked bar chart & Mouse Over Events
- calculating start position and width of a bar on 24 hour time scale(D3 JS)
- Stacked Bar Chart using d3.js stack layout; the bar sections not always in correct place
- add Y axis labels to D3 bar from json data name attribute
- c3js - D3js - Customize labels in a simple bar chart
- Stacked Bar Chart Labels - D3
- D3 : How to add labels top of in each bar in grouped bar chart
- NVD3 Horizontal Bar Chart labels too long
- D3.js bar chart - axis and labels not working / transitioning
- Dimple.js - Add data labels to each bar of the bar chart
- trying to get the circles next to the y axis labels in bar chart d3.js
- D3 Positioning of Text Labels on Bar Chart
- Nivo bar chart axis labels overlapping
More Query from same tag
- D3.js Problems Filtering topojson data
- D3.js: Dynamically generate source and target based on identical json values
- text-overflow property not working when using with d3
- Add ellipses to overflowing text in SVG?
- How to add label on SVG circle in d3?
- Need to create a line within x and y axis in d3 v5
- SVG How to make a part of the path draggable/ expandable
- d3js: How to select only x number of values from data
- Is there a mercurial plugin to provide github style calendar contributions?
- Angular svg or canvas to use colour gradients
- D3 stop and restart transition along path to allow click through to geo data coordinates
- D3 pack: setting same radius for the nodes that include the different number of children
- D3.js v4: Zoom on Bubble chart
- d3.js: Change x-axis interval on click
- D3 Sorting Bar Chart Error n.each
- Simple d3 line not working with Angular 8
- d3: multiple lines same graph with varying y domains?
- Starting out with d3.js visualizations
- Preserve color of a state in mouseover function in d3
- D3: Scale graph according the data points
- D3 Javascript plotting function
- how to read path info from json file using d3
- d3js v4: Add nodes to force-directed graph
- Remove specific value from array
- D3 histogram is not responsive even after using preserveAspectRatio
- How do I add bubbles on each of the y-axis values that I have and how do I add mouseover function for each of them?
- How can I detect double click on brush object?
- Get group from D3js selectAll in console?
- tooltip for a scatter plot matrix - using d3
- Problems creating Sankey diagrams using d3.js (2)