score:1
Accepted answer
Append text labels for each bar and set x, y values as shown below.
var textPadding = 15;
canvas.selectAll(".bartext")
.data(dataArray)
.enter()
.append("text")
.attr("class", "bartext")
.attr("text-anchor", "middle")
.attr("fill", "black")
.attr("x", function(d, i) {
return margin.left + textPadding + widthScale(d) * scaleMultiplier;
})
.attr("y", function(d, i) {
return margin.top + 20 + i * verticalGap;
})
.text(function(d) {
return d;
});
var id = "#container",
dataArray = [10, 20, 45, 24, 36, 10, 26];
var width = 1000,
height = 500;
var margin = {
left: 250,
right: 0,
top: 20,
bottom: 70
}
var verticalGap = 60;
var horizontalGap = 10;
var scaleMultiplier = 7;
var widthScale = d3.scale.linear().domain([0, 100]).range([0, 100])
var colorScale = d3.scale.ordinal().domain([0, 1, 2, 3, 4, 5, 6])
.range(["#D7DF21", "#FCB040", "#F1592A", "#0F75BC", "#61CCEE", "#C49A6C", "#13A89E"])
var canvas = d3.select(id).append("svg").attr("width", width).attr("height", height);
canvas.append("line").attr("x1", margin.left).attr("y1", height - margin.bottom).attr("x2", width)
.attr("y2", height - margin.bottom).attr("stroke-width", 2).attr("stroke", "#A6A7A6");
canvas.append("line").attr("x1", margin.left).attr("y1", height - margin.bottom + 1)
.attr("x2", margin.left).attr("y2", 5).attr("stroke-width", 2).attr("stroke", "#A6A7A6");
canvas.selectAll(".facilitiesBar").append("rect").data(dataArray).enter().append("rect").attr("class", "facilitiesBar")
.attr("width", function(d) {
return widthScale(d) * scaleMultiplier;
}).attr("height", 25)
.attr("x", margin.left + 1).attr("y", function(d, i) {
return margin.top + i * verticalGap;
})
.attr("fill", function(d, i) {
return colorScale(i);
})
var verticalLabel = ["Sports Field", "Sports Instruments", "Musical Instruments", "Different types of competition",
"Scout,Girls/Boys Guide", "No Instrument Available", "Library"
]
canvas.selectAll(".facilitiesY").append("text")
.data(verticalLabel).enter().append("text").text(function(d) {
return d
}).attr("class", "facilitiesY")
.attr("x", 0).attr("y", function(d, i) {
return margin.top + 20 + i * verticalGap
})
.attr("text-align", "left")
var horizontalLabel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
canvas.selectAll(".facilitiesX").append("text").data(horizontalLabel).enter().append("text")
.text(function(d) {
return "" + d * 10;
}).attr("class", "facilitiesX")
.attr("x", function(d) {
return (margin.left + 10 * d * scaleMultiplier);
})
.attr("y", 450).attr("text-align", "left")
canvas.append("text").text("(Response in %)").attr("x", 500).attr("y", 480);
var textPadding = 15;
canvas.selectAll(".bartext")
.data(dataArray)
.enter()
.append("text")
.attr("class", "bartext")
.attr("text-anchor", "middle")
.attr("fill", "black")
.attr("x", function(d, i) {
return margin.left + textPadding + widthScale(d) * scaleMultiplier;
})
.attr("y", function(d, i) {
return margin.top + 20 + i * verticalGap;
})
.text(function(d) {
return d;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="container">
</div>
Source: stackoverflow.com
Related Query
- How to add value of bar on the barChart?
- d3.js: How to add value below the label in donut chart
- How to add a second value to the links of a Sankey diagram in d3.jS :
- How to display the total value of a stacked bar chart in d3js
- How to set pattern origin at the top left point of each bar of a barchart in d3.js?
- D3.js - How to add the general update pattern to a stacked bar chart
- D3js horizontal bar chart: how to add numbers from data at the end of every bar?
- d3 Plus bar chart how to force fixed max y axis range independent of the max data value
- How to add text on top the heatmap to properly show value for each rectangle when using D3.js?
- How can I add a label with value to every point of the chart?
- How to add more metrics on the country_map in Apache-superset?
- D3 grouped bar chart: How to rotate the text of x axis ticks?
- How to display the value for the final tick on axis in D3?
- d3.js How to add lines to a bar chart
- how do i format the value shown in a d3 graph
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- D3.js - How to highlight the biggest and smallest (minimum and max) columns in a bar chart?
- how to add legend to a pie chart using D3js? And how to centralise the pie chart?
- D3.js: Changing the color of the bar depending on the value
- How do you change the last tick value in D3.js?
- How to set circle at the right end of the last bar / item using D3
- D3.js - how to add zoom button with the default wheelmouse zoom behavior
- How to add a line on x-axis on a horizontal bar chart in d3
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- How can I add zooming to the d3 collapsible tree example?
- How get the returned value of d3.ease?
- How to add a legend and labels to the pie chart?
- How to get value from the element using selection 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
- How to add text at the end of Arc
More Query from same tag
- D3 Sankey chart using circle node instead of rectangle node
- How do I flip the text in my D3 collapsible radial tree?
- dimple d3 plot, average of the values, quick command fix
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- How to select the color of node on data value and the Data value of tool as name?
- How do implement d3 v3 brush by specifying a increment brush slide value in steps programmatically
- Displaying map using d3.js and geojson
- How to make the cursor change in d3.js when I want?
- D3.js manually creating a diverging color scale? Newbie here
- Using on click & selectAll to remove existing data
- D3 tooltip display on page load
- D3-tip undefined in line chart
- D3.js / Javascript Node-Lines pop-up window and color change
- Two input forms side-by-side programatically via JavaScript & d3
- Appending maps as part of group (multiple maps)
- D3 Accessing array in Json object to attribute
- Mouseover to display/hide force layout links
- Incremental scatter plot in c3.js
- d3js ticks positioning
- How to distribute horizontally a bubble chart in d3js v4
- angular2 ng2d3 functionality is missing?
- How to Call a Function From a JSON Array
- D3: .transition() not working with events
- "Uncaught ReferenceError" from d3 with importmaps on Ruby On Rails
- Add a vertex in the middle of a path with d3js
- x3dom chart does not display using d3
- Delete/Change labels in Force Directed Graph
- How to create vertically grouped bar chart in d3.js using json data?
- How do I color d3 bars with randomly generated data?
- Responsive D3 js chart