I have made a parallel coordinates chart, now I want to make it responsive.
For the purpose of testing responsiveness, I thought to first make this example by Mike Bostock responsive.
The problem I am facing is that if I put the "svg" directly inside "body" tag, it works perfectly. But when I place the "svg" inside some divs, the chart gets cropped when the window is resized. Also, the height of the svg is mentioned as 70%, if I make it 80% then the chart gets cropped without any resize. I know there is another option which is to use viewBox and preserveAspectRatio, but I used this approach as it was easy to understand, for me or any other person reading my code. Here is the approach:
d3.csv("data.csv",function(data){
....
function resize(){
width = parseInt(d3.select("#mychart").style("width")) - margin.left - margin.right,
height = parseInt(d3.select("#mychart").style("height")) - margin.top - margin.bottom;
x.rangePoints([0, width], 1);
svg.select("#mychart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
return d != "name" && (y[d] = d3.scale.linear()
.domain(d3.extent(cars, function(p) { return +p[d]; }))
.range([height, 0]));
}));
svg.selectAll(".line").attr("d", path);
g = svg.selectAll(".dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; });
g.selectAll(".axis")
.each(function(d) {d3.select(this).call(axis.scale(y[d])); })
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; });
}
d3.select(window).on('resize', resize);
resize();
});
Here is the plunker link for the full code. Can anybody please point out whats wrong with the code?