score:1
Accepted answer
Try this way: -
category_g
.append("line")
.style("stroke", "black")
.style("stroke-width", "2px")
.style("stroke-linecap","round")
.attr("x1", function(d) {
var bbox = this.parentNode.getBBox();
return bbox.width+5;
})
.attr("y1", height)
.attr("x2", function(d) {
var bbox = this.parentNode.getBBox();
return bbox.width;
})
.attr("y2", height + 50);
var margin = {
top: 20,
right: 20,
bottom: 60,
left: 40
},
width = 560 - margin.left - margin.right,
height = 360 - margin.top - margin.bottom;
var color = {
Mechanical: '#4A7B9D',
Electrical: '#54577C',
Hydraulic: '#ED6A5A'
};
var barPadding = 40;
var data = [{
key: 'Mechanical',
values: [{
key: 'Gear',
value: 11
}, {
key: 'Bearing',
value: 8
}, {
key: 'Motor',
value: 3
}]
}, {
key: 'Electrical',
values: [{
key: 'Switch',
value: 19
}, {
key: 'Plug',
value: 12
}, {
key: 'Cord',
value: 11
}, {
key: 'Fuse',
value: 3
}, {
key: 'Bulb',
value: 2
}]
}, {
key: 'Hydraulic',
values: [{
key: 'Pump',
value: 4
}, {
key: 'Leak',
value: 3
}, {
key: 'Seals',
value: 1
}]
}];
var rangeBands = [];
var cummulative = 0;
data.forEach(function(val, i) {
val.cummulative = cummulative;
cummulative += val.values.length;
val.values.forEach(function(values) {
values.parentKey = val.key;
rangeBands.push(i);
})
});
//console.log(data);
var x_category = d3.scale.linear()
.range([0, width]);
var x_defect = d3.scale.ordinal().domain(rangeBands).rangeRoundBands([0, width], .1);
var x_category_domain = x_defect.rangeBand() * rangeBands.length;
x_category.domain([0, x_category_domain]);
var y = d3.scale.linear()
.range([height, 0]);
y.domain([0, d3.max(data, function(cat) {
return d3.max(cat.values, function(def) {
return def.value;
});
})]);
var category_axis = d3.svg.axis()
.scale(x_category)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style('background-color', 'EFEFEF')
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value");
var category_g = svg.selectAll(".category")
.data(data)
.enter().append("g")
.attr("class", function(d) {
return 'category category-' + d.key;
})
.attr("transform", function(d) {
return "translate(" + x_category((d.cummulative * x_defect.rangeBand())) + ",0)";
})
.attr("fill", function(d) {
return color[d.key];
});
var category_label = category_g.selectAll(".category-label")
.data(function(d) {
return [d];
})
.enter().append("text")
.attr("class", function(d) {
//console.log(d)
return 'category-label category-label-' + d.key;
})
.attr("transform", function(d) {
var x_label = x_category((d.values.length * x_defect.rangeBand() + barPadding) / 2);
var y_label = height + 30;
return "translate(" + x_label + "," + y_label + ")";
})
.text(function(d) {
return d.key;
})
.attr('text-anchor', 'middle');
var defect_g = category_g.selectAll(".defect")
.data(function(d) {
return d.values;
})
.enter().append("g")
.attr("class", function(d) {
return 'defect defect-' + d.key;
})
.attr("transform", function(d, i) {
return "translate(" + x_category((i * x_defect.rangeBand())) + ",0)";
});
var defect_label = defect_g.selectAll(".defect-label")
.data(function(d) {
return [d];
})
.enter().append("text")
.attr("class", function(d) {
//console.log(d)
return 'defect-label defect-label-' + d.key;
})
.attr("transform", function(d) {
var x_label = x_category((x_defect.rangeBand() + barPadding) / 2);
var y_label = height + 10;
return "translate(" + x_label + "," + y_label + ")";
})
.text(function(d) {
return d.key;
})
.attr('text-anchor', 'middle');
var rects = defect_g.selectAll('.rect')
.data(function(d) {
return [d];
})
.enter().append("rect")
.attr("class", "rect")
.attr("width", x_category(x_defect.rangeBand() - barPadding))
.attr("x", function(d) {
return x_category(barPadding);
})
.attr("y", function(d) {
return y(d.value);
})
.attr("height", function(d) {
return height - y(d.value);
});
category_g
.append("line")
.style("stroke", "black")
.style("stroke-width", "4px")
.style("stroke-linecap","round")
.attr("x1", function(d) {
var bbox = this.parentNode.getBBox();
return bbox.width+7;
})
.attr("y1", height)
.attr("x2", function(d) {
var bbox = this.parentNode.getBBox();
return bbox.width;
})
.attr("y2", height + 50);
/* Styles go here */
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Hello Plunker!</h1>
</body>
</html>
Source: stackoverflow.com
Related Query
- How to make bar chart with additional vertical lines below the x axis
- Make simple bar chart using C3 with separate columns on the x axis
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- How can I make a bar chart starting from the 0 point of the y axis and not from the bottom of the svg?
- How to make axis label of stacked bar chart in the middle of ticks chart in D3?
- How to Make a bar chart with rounded corners with extended grid in d3.js?
- 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 can I make my lines spin with the globe?
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- How to cut y axis to make the chart look better?
- how to make the x axis scrollable in a d3 line chart graph
- horizontal lines along the y axis d3 bar chart
- How to make dynamic Bar Chart in d3js with angularjs
- D3.js how to make the lines not go inside the arc of a donut chart
- How to make the grids of a chart be in line with axes
- How to sort bar charts synchronous with the ticks of x axis
- How can I make the legend's similar to the stacked bar chart (and change the legend's label) in D3 v6?
- How do I make my chart bars align with the chart axis?
- d3.js How to draw line chart with vertical x axis labels
- d3js - Stacked Bar Chart in Horizontal Axis - How do I set x so the bars are seperated?
- d3 Plus bar chart how to force fixed max y axis range independent of the max data value
- How do I manage groups for a grouped bar chart with a dropdown to update the chart in D3?
- How to implement hierarchical X-axis Bar chart in my angular JS application. The Bar should have +/- to open/close the hierarchical data like below
- How to plot stacked bar chart according to the custom Y axis in D3
- How do I make the height of a horizontal bar chart flexible in order to be resized when new data has been added in?
- How to make X axis timescale with equal interval between elements on chart independent of time range
- How do you rotate the x and y axis in D3 bar chart
- NVD3 Multi Bar Chart - Add in grid lines and make sure every y axis value shows up
- How to hide a content on stacked Bar Chart below X - Axis
- How to create a bar chart with C3 that doesn't group all columns in the same set
More Query from same tag
- Replace text value upon dataset change within an existing div using d3
- Use getCtm() at end of d3 translate event
- TopoJSON rendering issues using D3
- Set custom x-axis labels in d3 bar charts?
- Dynamically changing rateById.set in chloropeth map
- Put title above slider in D3 svg element
- d3.js path dom element
- c3 chart background area
- calling an axis based on one of data's attributes
- Links and textPaths in D3 cluster layout
- Creating links between nodes d3.js currently returning Uncaught TypeError: Cannot read property 'weight' of undefined
- javascript class instances handling event inconsistently - this/scoping confusion
- Multiple force-layout graphs with d3 in seperate svg/div's
- Change the format of JavaScript Data object
- Drawing Kenyan counties d3.geo.path()
- d3 How to limit drag and drop area
- d3.js multiple types of boxes
- How to convert CSV Data to JSON Object
- D3 Sankey node color arrangement
- d3 axis and label clipping
- D3 Bar Chart Negative Values not Showing Up
- Scaling d3 v4 map to fit SVG (or at all)
- Positioning and grouping SVG D3/SVG/ionic
- Single Axis Zoom in D3 & React
- doesn't update d3.js elements in .each function
- Draw a map with D3.js: How to get the right scale() and translate()?
- Create d3 linear color legend using d3 colors
- Redraw a d3 force layout without moving nodes around
- nvd3.js will not take data from jquery
- d3js - Create a server side SVG with javascript actions