score:3
Accepted answer
You can do that using a simple SVG rectangle appended before the black bars. For horizontally positioning the rectangle, just use your x
scale:
var redBox = svg.append("rect")
.attr("x", x(200) + x.bandwidth() / 2)
.attr("y", 0)
.attr("width", x(x.domain()[x.domain().length - 1]) - x(200) + x.bandwidth() / 2)
.attr("height", height)
.attr("fill", "red")
.attr("opacity", 0.2);
Here is your code with that change:
var mydata = {
"min": 68.9813,
"avg": 177.5037,
"max": 672.6713,
"values": [{
"bin": -50.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 0.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 50.0,
"percent": 6.7028,
"samples": 309
}, {
"bin": 100.0,
"percent": 32.2897,
"samples": 2407
}, {
"bin": 150.0,
"percent": 32.4565,
"samples": 3207
}, {
"bin": 200.0,
"percent": 17.1745,
"samples": 2064
}, {
"bin": 250.0,
"percent": 6.1833,
"samples": 940
}, {
"bin": 300.0,
"percent": 2.4971,
"samples": 444
}, {
"bin": 350.0,
"percent": 1.2438,
"samples": 279
}, {
"bin": 400.0,
"percent": 0.9262,
"samples": 182
}, {
"bin": 450.0,
"percent": 0.2781,
"samples": 71
}, {
"bin": 500.0,
"percent": 0.0962,
"samples": 24
}, {
"bin": 550.0,
"percent": 0.074,
"samples": 25
}, {
"bin": 600.0,
"percent": 0.0535,
"samples": 24
}, {
"bin": 650.0,
"percent": 0.0243,
"samples": 6
}, {
"bin": 700.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 750.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 800.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 850.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 900.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 950.0,
"percent": 0.0,
"samples": 0
}, {
"bin": 1000.0,
"percent": 0.0,
"samples": 0
}],
"index": 7,
"time_h": 13.8529,
"stddev": 67.8836,
"samples": 9982
};
//set the dimensions and margins of the graph
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
function make_x_gridlines() {
return d3.axisBottom(x)
.ticks(2)
}
// gridlines in y axis function
function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(10)
}
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")")
.style('fill', 'black');;
// get the data
// d3.csv("sales.csv", function(error, data) {
// if (error) throw error;
// // format the data
// data.forEach(function(d) {
// d.sales = +d.sales;
// });
// Scale the range of the data in the domains
x.domain(mydata.values.map(function(d) {
return d.bin;
}));
y.domain([0, d3.max(mydata.values, function(d) {
return d.percent;
})]);
var redBox = svg.append("rect")
.attr("x", x(200) + x.bandwidth()/2)
.attr("y", 0)
.attr("width", x(x.domain()[x.domain().length - 1]) - x(200) + x.bandwidth() / 2)
.attr("height", height)
.attr("fill", "red")
.attr("opacity", 0.2);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(mydata.values)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.bin) + (x.bandwidth() - 4) / 2;
})
.attr("width", Math.min(x.bandwidth(), 5))
.attr("y", function(d) {
return y(d.percent);
})
.attr("height", function(d) {
return height - y(d.percent);
});
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_gridlines()
.tickSize(-height)
.tickFormat("")
);
// add the Y gridlines
svg.append("g")
.attr("class", "grid")
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat("")
);
// add the x Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- Adding background color in d3 js bar chart using data values
- d3.js - group 2 data values in a stacked bar chart
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- 2 arrays one of values one of colors make D3 bar chart have color at same index of value
- Filtering in stacked bar chart using d3 v3.js by changing data on input selection
- Transition for grouped bar chart in d3 when updating the data values
- Adding tooltip to bar chart generated using svg path
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- D3js v5 Trying to select multiple bars in bar chart using brush and saving values to variable and table
- d3.js bar chart new data adding onto bars,
- Add data values to D3.js bar chart in the middle or above
- Infinite scrolling bar chart without re-rendering of data using d3.js
- How do I pull nested data into a bar chart using D3?
- D3.js bar chart generate bar color on weight or value of data
- Unable to create a bar chart from json data using d3.js
- Adding Data Label and Marker to the Line chart using d3
- How to create multi color vertical bar chart in d3.js using specific json data?
- Creating a stack bar chart using XML data
- Make a Group Bar Chart after nesting data in D3 using CSV
- D3 bar chart same values on chart but data different
- D3.js. Grouping data in bar chart using d3.nest()
- Display Bar Chart for Last week, Last Month, and Last Year Data from JSON using D3.js
- The values on x-axis are descending values on grouped bar chart using d3.js
- Facing issues while grouping data for bar chart in dc js using crossfilter
- Bar chart with negative values
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- How to update d3.js bar chart with new data
- how to create labels for data in donut chart using d3.js
- Adding label on a D3 bar chart
- d3 accessing nested data in grouped bar chart
More Query from same tag
- how to display date and time both in xScale in D3
- How to add a shape on a chart node in d3.js?
- Find the points lying between Start and End Angle of an ARC.
- Create line break in D3
- Show array values in D3.js html tooltip
- Filter d3.s to show specific data
- Embed D3 + HTML with Dash Components
- Ordinal scale behaviour
- Using tooltip in D3 chord diagram to get value - Error: "property of target undefined"
- How to draw arc with d3.js with only outer radius, or sector of circle?
- d3.js barchart not quite like I want it
- d3.js - how to delete a group element
- d3: Zoom to Bounding Box II for countries instead of states
- D3 js collapsible chart increasing the arc curve radius
- d3.js Dendrogram: How to toggle visibility of nodes by click
- Render text with nested data in D3 js version 5
- In d3, how to get the interpolated line data from a SVG line?
- Why is my SVG rendered by D3 inside a Polymer component unstyled?
- d3.js - .exit().remove() a group element is not working
- d3.js donut chart with legend outside donut
- c3JS Load Function
- D3 scale how to put only the minimum and the max number on the scale
- how to make d3. charts customizable?
- D3JS Tree with Axis
- Prevent D3 graph from scaling
- D3 JS - Force Graph - Doesn't restart layout after removing Node
- Calculating the number of bars in a bar chart in dc.js
- D3 SVG Move zoom to rect inside <g> having different angle
- how to run d3.js code in jsfiddle
- d3 label are overlaping