NVD3 Chart responsive issue

时间:2015-10-30 21:23:14

标签: javascript d3.js charts nvd3.js

NVD3 Responsive issue

I have code from my second page where I used the exactly same chart like here. But on that page it is responsive and I am trying to figure out why the chart is not responsive here.

enter image description here

  var colors = ["rgba(74, 210, 255, .8)", "rgba(85, 172, 238, .8)", "rgba(205, 32, 31, .8)"];
  d3.scale.colors = function() {
        return d3.scale.ordinal().range(colors);
    };
  
  var colors = d3.scale.colors();
    /*var colors = d3.scale.category20();*/ 
    var keyColor = function(d, i) {return colors(d.key)};  
  
    var chart;
    nv.addGraph(function() {
        chart = nv.models.stackedAreaChart()
            .useInteractiveGuideline(true)
            .x(function(d) { return d[0] })
            .y(function(d) { return d[1] })
            .showControls(false)
            .showYAxis(true)
            .showLegend(false)
            .rightAlignYAxis(true)
            .controlLabels({stacked: "Stacked"})
            .color(keyColor)
            .duration(500);

        chart.xAxis.tickFormat(function(d) { return d3.time.format('%a')(new Date(d)) });
        chart.yAxis.tickFormat(d3.format('f'));

        chart.legend.margin({
                        top: 30
                    });

        d3.select('#chart1')
            .datum(histcatexplong)
            .transition().duration(1000)
            .call(chart)
            .each('start', function() {
                setTimeout(function() {
                    d3.selectAll('#chart1 *').each(function() {
                        if(this.__transition__)
                            this.__transition__.duration = 1;
                    })
                }, 0)
            });

        nv.utils.windowResize(chart.update);
        return chart;
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

<div class="slide1">
   <div class="col-xs-12" id="margin-chart">
      <svg id="chart1" style="width: 100%; height: 300px;"></svg>
   </div>
</div>

2 个答案:

答案 0 :(得分:3)

当slideToggle函数完成后,您需要调用chart.update()以使nvd3将图表重绘为新尺寸。

即:

$('#chart').slideToggle(function() {
    chart.update();
});

答案 1 :(得分:0)

我使用了.animate函数。

    $("#slide").click(function () {
      if ($('.slide1').hasClass('showtable')) {
          $('.slide1.showtable').animate({height: 300}, 500).removeClass('showtable');
      } else { 
          $('.slide1').animate({height: 0}, 500)
          $('.slide1').addClass('showtable');
      }
    });
.slide1 {
    position: relative;
    width: 100%;
    overflow: hidden;
    right: 25px;
}

.slide1.showtable {
    height: 0;
}