XcodeX
XcodeX

Reputation: 99

Nvd3 chart huge with oficial Css

I am having unexpected problems with a couple of Nvd3 charts. I coded them withouht using the nvd3 css file (nv.d3.min.css). Without it everything was ok but when I added it suddendly the second chart took a lot of space (1500x1500). The normal size was 450x450 but now it is

If i look in the console of chrome and uncheck the style atributes "width: 100%;" and "height: 100%;" it works (actually with only one). The other thing that changes de css atributes is the "user agent stylesheet".

I can´t understand why because i thought that the size was explicitely coded while the configuration of the chart

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 anybody see what is happening?

Upvotes: 3

Views: 941

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

Just override the default width and height properties of the nvd3.css stylesheet, creating a rule in your stylesheet, and ensuring it is loaded after the nvd3 stylesheet.

The last rule (with the same specificity) wins:

svg {
     width : auto;
    height : auto;
}

or create a more specific rule to act on your svgs only, like:

#charts svg {
     width : 450px;
    height : 450px;
}

Upvotes: 1

Related Questions