JSFiddle - React, Tailwind, and code Playground
by HemalathaThanigaivel
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="row">
<div class="col-md-12">
<div id="verticalSVG" style="float:left">
</div>
<div id="horizontalSVG" style="overflow-x:scroll">
</div>
</div>
</div>
CSS
.legend-main-div {
padding: 2%;
max-height: 150px;
overflow-y: scroll;
border: 1px solid blue;
}
.axis line,
.axis path {
fill: none;
stroke: black;
}
JavaScript
var tempData = [];
for (var i = 1; i < 2; i++) {
var obj = {
"Id": 1,
"Type": 'A' + i,
"Count": 5
};
tempData.push(obj);
}
var barWidth = 30,
paddingFactor = 2.2,
padding = 20,
responsiveDIVHeight = 300,
responsiveDIVWidth = tempData.length * barWidth * paddingFactor;
var x = d3.scaleBand().rangeRound([0, responsiveDIVWidth], 0.5)
.domain(tempData.map(function(d) {
return d.Type
})),
y = d3.scaleBand().rangeRound([ padding, responsiveDIVHeight])
.domain([0, d3.max(tempData, function(d) {
return (d.Count + 10);
})]),
xAxis = d3.axisBottom(x),
yAxis = d3.axisLeft(y);
console.log(y.domain(), "domain", y(0))
var svgY = d3.select('#verticalSVG')
.append('svg')
.attr('height', responsiveDIVHeight)
.attr("width", 50);
svgY.append('g')
.attr('class', 'y axis')
.call(yAxis)
.attr('dx', '-0.3em')
.attr('transform', "translate(50, 0)");;
var svgX = d3.select('#horizontalSVG')
.append('svg')
.attr('width', responsiveDIVWidth)
.attr('height', responsiveDIVHeight);
svgX.append('g')
.attr('class', 'x axis')
.attr('transform', "translate(0," + (responsiveDIVHeight - padding) + ")")
.call(xAxis)
.selectAll('text')
.attr('dx', '-0.8em')
.attr('dy', '-.15em')
.attr('transform', function(d) {
return "rotate(-60)"
});
var startPoint = 0;
var bar = svgX.selectAll('.bar')
.data(tempData)
.enter()
.append('rect')
.attr('y', function(d) {
console.log(y(d.Count), "y")
return y(d.Count)
})
.attr('x', function(d) {
return x(d.Type)
})
.attr('width', barWidth)
.attr('height', function(d) {
console.log(responsiveDIVHeight,d.Count, y(d.Count))
return responsiveDIVHeight - y(d.Count) - padding
})