I am trying to do a SVG graph with D3 (https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js) where the bar width is defined manually and the thing has a horizontal scroll..
I have a working fiddle here
https://jsfiddle.net/bikrantsharma/zw264tfc/12/
This is how my scales are defined
var barWidth = 30,
paddingFactor = 2.2,
responsiveDIVHeight = 300,
responsiveDIVWidth = tempData.length * barWidth * paddingFactor;
var x = d3.scale.ordinal().rangeRoundBands([0, responsiveDIVWidth], 0.5)
.domain(tempData.map(function(d) {
return d.Type
})),
y = d3.scale.linear().rangeRound([responsiveDIVHeight, 0])
.domain([0, d3.max(tempData, function(d) {
return (d.Count + 10);
})]),
There are two problems which i need help with:
The Bars are flipped in opposite direction. How to get the div flipped or the bars transformed?
The height of the bar comes a slightly less than the actual value. So for e.g if the Count is '5' bar height comes back as 4.8 or something. What is the issue there?
Your bars are not flipped. What's happening here is very simple: You have two SVGs, one at the top and one at the bottom, and you're drawing the rectangles in the bottom SVG. How can the rectangles magically appear in the top SVG? It's not possible.
Instead of that, draw the SVGs side by side. The left one, with the vertical axis, has
float: left
in the style of the<div>
it is in, and the right one, scrollable, hasoverflow-x: scroll
in the style of the<div>
it is in.Here is a demo making the minimal possible changes in your code: