NVD3 Chart responsive issue

邮差的信 提交于 2019-12-11 13:45:41

问题


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.

  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>

回答1:


You need to call chart.update() when your slideToggle function finishes to make nvd3 redraw the chart to the new dimensions.

I.e.:

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



回答2:


I used the .animate function.

    $("#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;
}


来源:https://stackoverflow.com/questions/33445128/nvd3-chart-responsive-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!