score:2
Accepted answer
For showing the background for the bars, just copy your selection and chose a value of 100% for the rectangles, in a light gray fill:
var backgroundBar = svg.selectAll(null)
.data(data)
.enter()
.append("rect")
//etc...
.attr("width", function(d) {
return x(100);
});
Of course, you'll have to change the domain of the x scale:
var x = d3.scaleLinear()
.domain([0, 100]);
Then, drop both axis and print the labels using a text selection.
Finally, use another text selection for the values:
var values = svg.selectAll(null)
.data(data)
.enter()
.append("text")
//etc...
.text(function(d) {
return +d.xAxis
})
If you want, you can tween the text:
.attrTween("text", function(d) {
var self = this
var i = d3.interpolateNumber(0, +d.xAxis);
return function(t) {
return d3.select(self).text(~~i(t));
}
});
This is the result:
var data = [{
"yAxis": "score",
"xAxis": "72"
}];
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 80
},
width = 500 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var y = d3.scaleBand()
.range([height, 0])
.padding(0.4);
var x = d3.scaleLinear()
.range([0, width])
.domain([0, 100]);
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 200)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
y.domain(data.map(function(d) {
return d.yAxis;
}));
var backgroundBar = svg.selectAll(null)
.data(data)
.enter()
.append("rect")
.attr("fill", "lightgray")
.attr("y", function(d) {
return y(d.yAxis);
})
.attr("height", y.bandwidth())
.attr("width", function(d) {
return x(100);
});
var bar = svg.selectAll(null)
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("y", function(d) {
return y(d.yAxis);
})
.attr("height", y.bandwidth())
.transition()
.duration(2000)
.delay(function(d, i) {
return i * 100
})
.attr("width", function(d) {
return x(d.xAxis);
});
var labels = svg.selectAll(null)
.data(data)
.enter()
.append("text")
.attr("y", function(d) {
return y(d.yAxis) + y.bandwidth() / 2;
})
.attr("x", -10)
.attr("text-anchor", "end")
.text(function(d) {
return d.yAxis
});
var values = svg.selectAll(null)
.data(data)
.enter()
.append("text")
.attr("y", function(d) {
return y(d.yAxis) + y.bandwidth() / 2;
})
.attr("x", 10)
.text(function(d) {
return +d.xAxis
})
.transition()
.duration(2000)
.delay(function(d, i) {
return i * 100
})
.attr("x", function(d) {
return x(d.xAxis) + 10;
})
.attrTween("text", function(d) {
var self = this
var i = d3.interpolateNumber(0, +d.xAxis);
return function(t) {
return d3.select(self).text(~~i(t));
}
});
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- d3 horizontal bar chart with background and max value of 100%
- d3 bar chart y axis ticks and grid lines above max value
- 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 horizontal stacked bar chart with 2 vertical axes and tooltips
- Showing a bar chart with expected value and current value for a series
- d3,js setting X and Y domains on Horizontal Bar Chart with all negative values
- How to use x and width in a bar chart with scaleTime?
- C3 / D3 bar chart with horizontal scroll
- d3.js stacked bar chart with positive and negative values
- NVD3.js: Stacked and grouped bar chart with two y-axis
- D3 bar chart background with dots
- Grouped bar chart with zoom and updateable data error
- Specifying Ticks on D3 Bar chart with Time Series data and scaleBand
- 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?
- Get unique max value for each y-axis in a bar chart
- D3 Bar and Linear Chart With Multiple Axis
- Bar chart with d3.js and an associative array
- How to create a % difference arrow line with value in a bar chart using D3.js
- D3 bar chart not working properly with all negative and positive values
- d3 bar chart with custom x axis and bar width
- Using d3-tip and CSS hover effect with d3 stacked bar chart
- C3.js horizontal bar chart - Highlight a specific x-axis tick together with bar
- NVD3 Line Plus Bar With Focus Chart only displaying half width of first and last bar
- D3 Bar chart - Remove labels with zero value
- Trying to make dynamic D3 Chart with Bar and Difference line/value
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- dc.js and crossfilter stack bar chart with filtered versus unfiltered
- Generating a simple bar chart with d3.js and an array of form data
- Horizontal Stacked Bar Chart with Two Y Axis Issues
More Query from same tag
- grunt build removes d3.js and google fonts from application
- Some clarification on reusable charts
- How to get the total number of rows with particular value in json file in d3.js
- How to create legend for D3 Sequence Sunburst?
- D3js applying drop shadow on mouseover
- d3.js: legends with dynamic element width
- How to bind d3 generated HTML elements to scope?
- Passing functions to d3 generators in typescript
- random colors for circles in d3.js graph
- d3.js - show large numbers on axis in abbreviated form
- dimple.js filter x axis with date range
- dc Line chart not highlighting properly in Firefox on legend hover
- How does double bracket parameter work in D3?
- Unable to position y axis properly and missing max number on y-axis
- Handling javascript events of a NVD3 chart
- Using D3, I am trying to add and remove lines from a multi line chart when the legend is clicked
- Unable to use CSS from Angular2 Dart application (CSS shim)
- d3 js nodes as images
- dc.js/d3.js - min/max value from group per category
- Create a Vertical Column Chart with D3 using CSV with Similar String Occurrences
- d3 Bubble Chart positioning and text in side bubbles
- D3 - Using a variable as file path
- read width of d3 text element
- How to append text to SVG
- D3, Google Maps, selecting d3 elements
- in angular-nvd3 how to Invert donut Chart
- d3 - how to format date on x-axis
- D3 Categorical Area Chart - Scale Issue
- Error in showing min() and max() result in D3
- Filtering related Nodes and Links D3.js V5