score:1
Accepted answer
You can add a second set of bars and assign the x
and width
based on marker's start and end points.
Assuming xValue
to be the starting point and slength
being the width of the highlight, here's the relevant code to add the markers:
// add markers
bars.append("rect")
.attr("class", "highlight")
.attr("y", function (d) {
return y(d.name);
})
.attr("height", y.rangeBand())
.attr("x", function (d) {
return x(d.xValue);
})
.attr("width", function (d) {
return x(d.xValue + d.slength);
});
Code snippet (including the highlights within a couple more rows):
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Simple Bar chart</title>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
body {
font-family: "Arial", sans-serif;
}
.bar {
fill: #5f89ad;
}
.highlight {
fill: #ff0;
}
.axis {
font-size: 13px;
}
.axis path,
.axis line {
fill: none;
display: none;
}
.label {
font-size: 13px;
}
</style>
</head>
<body>
<div id="graphic"></div>
<script>
var data = [{
"name": "Case 01",
"value": 2000,
"xValue": 500,
"slength": 100,
},
{
"name": "Case 02",
"value": 1290,
"xValue": 300,
"slength": 50,
},
{
"name": "Case 03",
"value": 19000,
"xValue": 1000,
"slength": 300,
},
{
"name": "Case 04",
"value": 5000,
},
{
"name": "Case 05",
"value": 1600,
},
{
"name": "Case 06",
"value": 2600,
},
{
"name": "Case 07",
"value": 3000,
},
{
"name": "Case 08",
"value": 7000,
},
{
"name": "Case 09",
"value": 3290,
},
{
"name": "Case 10",
"value": 12000,
},
{
"name": "Case 11",
"value": 10000,
},
{
"name": "Case 12",
"value": 11600,
},
{
"name": "Case 13",
"value": 5600,
},
{
"name": "Case 14",
"value": 6000,
},{
"name": "Case 15",
"value": 8600,
},
{
"name": "Case 16",
"value": 4280,
},
{
"name": "Case 17",
"value": 9000,
},
{
"name": "Case 18",
"value": 5000,
},
{
"name": "Case 19",
"value": 4300,
},
{
"name": "Case 20",
"value": 2600,
},
{
"name": "Case 21",
"value": 6700,
},{
"name": "Case 22",
"value": 12000,
},
{
"name": "Case 23",
"value": 8290,
},
{
"name": "Case 24",
"value": 8000,
},
{
"name": "Case 25",
"value": 7600,
},
{
"name": "Case 26",
"value": 1600,
},
{
"name": "Case 27",
"value": 5500,
},
{
"name": "Case 28",
"value": 4400,
},{
"name": "Case 29",
"value": 8900,
},
{
"name": "Case 30",
"value": 6790,
},
{
"name": "Case 31",
"value": 11000,
},
{
"name": "Case 32",
"value": 5000,
},
{
"name": "Case 33",
"value": 6600,
},
{
"name": "Case 34",
"value": 7700,
},
{
"name": "Case 35",
"value": 3000,
}];
//sort bars based on value
data = data.sort(function (a, b) {
return d3.descending(a.name, b.name);
})
//set up SVG using margin conventions - we'll need plenty of room on the left for labels
var margin = {
top: 15,
right: 100,
bottom: 15,
left: 100
};
var width = window.innerWidth - margin.left - margin.right,
height = window.innerHeight- margin.top - margin.bottom;
var svg = d3.select("#graphic").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 + ")");
var x = d3.scale.linear()
.range([0, width])
.domain([0, d3.max(data, function (d) {
return d.value;
})]);
var y = d3.scale.ordinal()
.rangeRoundBands([height, 0], .1)
.domain(data.map(function (d) {
return d.name;
}));
//make y axis to show bar names
var yAxis = d3.svg.axis()
.scale(y)
//no tick marks
.tickSize(0)
.orient("left");
var gy = svg.append("g")
.attr("class", "y axis")
.call(yAxis)
var bars = svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
//append rects
bars.append("rect")
.attr("class", "bar")
.attr("y", function (d) {
return y(d.name);
})
.attr("height", y.rangeBand())
.attr("x", 0)
.attr("width", function (d) {
return x(d.value);
})
//add a value label to the right of each bar
bars.append("text")
.attr("class", "label")
//y position of the label is halfway down the bar
.attr("y", function (d) {
return y(d.name) + y.rangeBand() / 2 + 4;
})
//x position is 3 pixels to the right of the bar
.attr("x", function (d) {
return x(d.value);
})
.style('fill', 'darkGray')
.text(function (d) {
return d.value;
});
// add markers
bars.append("rect")
.attr("class", "highlight")
.attr("y", function (d) {
return y(d.name);
})
.attr("height", y.rangeBand())
.attr("x", function (d) {
return x(d.xValue);
})
.attr("width", function (d) {
return x(d.xValue + d.slength);
})
</script>
</body>
</html>
And a fiddle link: http://jsfiddle.net/wc4q3r1y/
Hope this helps.
Source: stackoverflow.com
Related Query
- How to add marking in bar chart using d3
- How can I add columns as categories in a bar chart using Dimple.js?
- How do i add a fixed text beside bar chart using D3
- d3.js How to add lines to a bar chart
- how to add legend to a pie chart using D3js? And how to centralise the pie chart?
- 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 to create vertically grouped bar chart in d3.js using json data?
- How to create a stacked bar chart using dc.js?
- D3 : How to add labels top of in each bar in grouped bar chart
- How to create a % difference arrow line with value in a bar chart using D3.js
- How to add text legends in bars of a bar chart in D3?
- Is there a way to add horizontal scroll bar for a bar chart made using dimplejs?
- How to add live data to stacked bar chart
- how i can put more lines in Line Plus Bar Chart using nvd3.js?
- how to add tooltip bar chart d3.js
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- how to add tooltip for donut chart in loop using d3js
- How to use log scale to show proper stacked bar chart using d3 JS?
- How to add a tooltip that shows a bar chart onto a d3.js map
- How to get Average from DOW of Date column using crossfilter to populate in bar chart
- How to center align text labels on bar chart using d3js
- How to add multiple tool tips to a bar chart
- How to give a Bar in a Stacked Bar Chart a minimal height using D3?
- How do I pull nested data into a bar chart using D3?
- How to draw line plus bar chart using highcharts/d3.js or any other chart library?
- D3 How to add rounded top border to a stacked bar chart
- How to Add Multiline Text Tooltips in Bar Chart D3 JS
- How to add a line at top of stacked bar chart with d3.js?
- How to place the bars of a bar chart in the right positions of the xAxis using d3.js?
More Query from same tag
- Hitting browser "back" button shows JSON rather than HTML (using Rails & d3.js)
- Hide legend items if value is equal to 0 in NVD3.js Pie Chart
- pie chart d3.js
- X-axis scaling with D3 JavaScript
- D3 line chart: x line function always renders NaN
- Using d3.js + SoundCloud API - Need to show track titles
- how to detect mousemove if svg is behind another element
- D3: append circle does not work - must be missing something
- How to rotate an image in a canvas?
- NS_ERROR_FAILURE in Firefox when drawing dimple.js chart after jquery
- DC.js choropleth map chart CSS conflicting with colouring, no map showing. How can I turn off the fill:none?
- d3.js voronoi.clipExtent() is returning the error: undefined is not a function
- D3.js charts placed in a bootstrap carousel
- D3.js: real-time sunburst Partition data: recreate Hierarchy?
- D3 - Add dots in middle of lines between two points
- Converting Array of Names to Linear Scale d3
- Why is my number format choking?
- How do I perform append after DOM elements updatein a D3 chart?
- Gremlin: Find all the paths between two nodes and transform the query result into JSON format
- Type error after making standalone d3v5 zoom with tooltip code from Mike Bostocks oberservablehq example
- D3 update circle-pack data new nodes overlap existing nodes
- Is it possible to use D3.tree() with a dataset that includes parents instead of children?
- Heatmap and sparklines
- Any visualization library which does not require user interaction
- D3 - Appending Rectangle Stops Tooltip From Working
- Max length of an array after reading a CSV (JavaScript)
- Reversing d3 bar chart loading animation
- In d3.js, how can I preserve index i (or coerce / force group index j use in it's place) in data bindings subject to nested selections?
- Mirror d3 graph horizontally in JavaScript/html
- AngularJS + D3 Animations Bar Chart Directive