Nvd3 graphics are huge with official Css
I am having unexpected problems with multiple Nvd3 diagrams. I coded them using the nvd3 css file (nv.d3.min.css). It was fine without it, but when I added it suddendly the second graph took up a lot of space (1500x1500). The normal size was 450x450, but now it is
If I look in the console for chrome and uncheck the style "width: 100%;" and "height: 100%"; it works (actually with only one). Another thing that changes css attributes is the user agent stylesheet.
I can't figure out why as I thought the size was explicitly encoded and the chart config
Html
<div id="charts">
<div id="piechart" ><svg></svg></div>
<div id="chart"><svg></svg></div>
</div>
NVD3
function setupGraph(data_graph) {
nv.addGraph(function() {
var pieChart = nv.models.pieChart();
pieChart.margin({top: 30, right: 60, bottom: 20, left: 60});
var datum = data_graph[0].values;
pieChart.tooltipContent(function(key, y, e, graph) {
var x = String(key);
var y = String(y);
tooltip_str = '<center><b>'+x+'</b></center>' + y;
return tooltip_str;
});
pieChart.showLabels(true);
pieChart.donut(false);
pieChart.showLegend(true);
pieChart
.x(function(d) { return d.label })
.y(function(d) { return d.value });
pieChart.width(450);
pieChart.height(450);
d3.select('#piechart svg')
.datum(datum)
.transition().duration(350)
.attr('width',450)
.attr('height',450)
.call(pieChart);
nv.utils.windowResize(chart.update);
return chart;
});
}
function setupGraph2(data_graph) {
nv.addGraph(function() {
var chart = nv.models.discreteBarChart()
.x(function(d) { return d.label }) //Specify the data accessors.
.y(function(d) { return d.value })
//.valueFormat(d3.format(',.2f'))
.staggerLabels(true) //Too many bars and not enough room? Try staggering labels.
.tooltips(false) //Don't show tooltips
.showValues(true) //...instead, show the bar value right on top of each bar.
.transitionDuration(350)
;
chart.width(450);
chart.height(450);
d3.select('#chart svg')
.datum(data_graph)
.attr('width',450)
.attr('height', 450)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
Can anyone see what's going on?
source to share
Just override the default width and height properties in the nvd3.css stylesheet by creating a rule in the stylesheet and making sure it loads after the nvd3 stylesheet.
The last rule (with the same specificity ) wins:
svg {
width : auto;
height : auto;
}
or create a more specific rule that only affects your svgs like:
#charts svg {
width : 450px;
height : 450px;
}
source to share