score:0
Accepted answer
When you map dataset
, add the people
property to it (and do the same in the second map):
dataset = dataset.map(function(d) {
return d.data.map(function(o, i) {
return {
people: o.people,
y: o.count,
x: o.value
};
});
});
After that, you'll have the people
property in the bound data. Thus, just change the text
to:
.text(d.people);
Here is your updated code:
var setOfValues = ["Value4", "Value5", "Value6"];
var margins = {
top: 30,
left: 100,
right: 20,
bottom: 0
};
var legendPanel = {
width: 0
};
var width = 500 - margins.left - margins.right - legendPanel.width;
var height = 80 - margins.top - margins.bottom
var dataset = [{
data: [{
value: 'Value1',
count: 3,
people: "Anna, Maria, Peter",
}, {
value: 'Value2',
count: 3,
people: "Michael, Martin, Joe",
}, {
value: 'Value3',
count: 2,
people: "Martin, Joe",
}]
}, {
data: [{
value: 'Value1',
count: 2,
people: "Luis, Kim",
}, {
value: 'Value2',
count: 1,
people: "Richard",
}, {
value: 'Value3',
count: 4,
people: "Michael, Martin, Joe, Maria",
}]
}
, {
data: [{
value: 'Value1',
count: 1,
people: "Linda",
}, {
value: 'Value2',
count: 2,
people: "Ben",
}, {
value: 'Value3',
count: 0,
people: "",
}]
}
];
dataset = dataset.map(function (d) {
return d.data.map(function (o, i) {
return {
people: o.people,
y: o.count,
x: o.value
};
});
});
var stack = d3.layout.stack();
stack(dataset);
var dataset = dataset.map(function (group) {
return group.map(function (d) {
return {
people: d.people,
x: d.y,
y: d.x,
x0: d.y0
};
});
});
var numberOfPeople = 6;
var svg = d3.select('body')
.append('svg')
.attr('width', width + margins.left + margins.right + legendPanel.width)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');
var xMax = numberOfPeople;
var xScale = d3.scale.linear()
.domain([0, xMax])
.range([0, width]);
var values = dataset[0].map(function (d) {
return d.y;
});
var yScale = d3.scale.ordinal()
.domain(values)
.rangeRoundBands([0, height], .2);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient('top')
.tickFormat(function(d) { return parseInt(d, 10) })
.ticks(xMax);
var yAxis = d3.svg.axis()
.scale(yScale)
.outerTickSize(0)
.orient('left');
var colors = d3.scale.ordinal().range(["#3E7EAB","#D89218","#EEEEEE"]);
var groups = svg.selectAll('g')
.data(dataset)
.enter()
.append('g')
.style('fill', function (d, i) {
return colors(i);
});
var rects = groups.selectAll('rect')
.data(function (d) {return d; })
.enter()
.append('rect')
.attr('x', function (d) {return xScale(d.x0);})
.attr('y', function (d, i) {return yScale(d.y);})
.attr('height', function (d) {return yScale.rangeBand();})
.attr('width', function (d) {return xScale(d.x);})
.on('mouseover', function (d) {
var xPos = parseFloat(d3.select(this).attr('x')) / 2 + width / 2;
var yPos = parseFloat(d3.select(this).attr('y')) + yScale.rangeBand() / 2;
d3.select('#tooltip')
.style('left', xPos + 'px')
.style('top', yPos + 'px')
.select('#value')
//Question 1: "How to show in tooltip names of people??"
.text(d.people);
d3.select('#tooltip').classed('hidden', false);
})
.on('mouseout', function () {d3.select('#tooltip').classed('hidden', true); });
svg.append('g')
.attr('class', 'axis')
.call(yAxis);
svg.append('g')
.attr('class', 'axis')
.call(xAxis);
.axis path, .axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
#tooltip {
position: absolute;
text-align: left;
height: auto;
padding: 10px;
background: #162F44;
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 11px;
color: white;
line-height: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="tooltip" class="hidden">
<p><span id="value"></span>
</p>
</div>
PS: Regarding your second question ("how to make a second vertical axis?"), your desired outcome is not exactly clear. Besides that, as it's not a good practice asking more than one question in a single post, I suggest you post another question, better explaining your problem.
Source: stackoverflow.com
Related Query
- d3.js horizontal stacked bar chart with 2 vertical axes and tooltips
- dc.js - Chart not rendering (single row bar chart - horizontal row), with the exception of legend, axes and tick marks/values (which do render)
- d3.js stacked bar chart with positive and negative values
- NVD3.js: Stacked and grouped bar chart with two y-axis
- D3 Chart version 4 Normalized Stacked Bar Chart from vertical to horizontal
- d3 horizontal bar chart with background and max value of 100%
- Using d3-tip and CSS hover effect with d3 stacked bar chart
- Horizontal Stacked Bar Chart with Two Y Axis Issues
- D3 horizontal stacked bar chart add and remove data
- dc.js Stacked Bar Chart having only one column and with elastic X Axis is not rendered properly
- Changing the orientation of the Stacked bar Chart from Vertical to Horizontal in D3
- D3 Horizontal Stacked Bar Chart with Lines
- D3 fill for a horizontal stacked bar chart in Angular with typeScript: .attr("fill", ..) gets error "No overload matches this call" in VSC
- d3 - Update Width and X for smooth transition in Horizontal Stacked Bar Chart
- Horizontal Bar chart with vertical line is possible or not in D3
- d3,js setting X and Y domains on Horizontal Bar Chart with all negative values
- D3js - change Vertical bar chart to Horizontal bar chart
- D3.js Stacked Bar Chart, from vertical to horizontal
- d3 v4 drag line chart with x and y axes
- How to use x and width in a bar chart with scaleTime?
- C3 / D3 bar chart with horizontal scroll
- d3 v4 nested data and stacked bar chart
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- d3.js Stacked bar chart with Logarithmic Scaling
- Grouped bar chart with zoom and updateable data error
- Specifying Ticks on D3 Bar chart with Time Series data and scaleBand
- D3 updating a vertical stacked bar chart
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- Updating a bar chart with .update, .enter, and .exit?
More Query from same tag
- D3 Force layout fix positions
- d3.js vertical line on a time axis
- D3.js v5 - simple streamgraph not rendering
- D3.js HTML data table filter by dropdown
- Highlight parent path to the root
- Issue creating overlapping multiple pie charts (circular progress bars)
- Dropdown Menu over SVG?
- how to set up the scale of yAxis in D3.js with a csv file with multiple columns
- any idea why my d3 line graph is always off?
- instead of ready data how can i read from csv?
- Ordering chords based on some property of the source data
- Specifying hard coded input data for D3 Multi-Series Line Chart
- Append Circles to 1 Line in a D3 Multi-Line chart
- D3: show years on first month and on January, and move ticks to bar midpoints
- d3 custom bar chart bars using svg paths instead of rects
- D3.js sunburst partition rendering incorrectly
- How to prevent/minimize reflows for this d3 example?
- How to expand D3js tree chart onclick a button
- Fisheye effect D3js image slideshow works on rects/objects but not image files, Error: <image> attribute x: Expected length, "NaN"
- How to select certain time period in d3 js?
- D3.js multiple y-Axis with same position of ticks?
- Assigning two class names from functions in D3
- Change tooltips color d3.js
- How to make bars have different colors in d3?
- multiple d3 graphs in one svg
- How to scale a TopoJSON map in d3.js
- d3js, detect click above chart
- D3 Uncaught TypeError: Cannot read property 'age' of undefined
- Scatter Chart in analog clock
- How to flip horizontally the text on the left part of the canvas with d3.js?