How to make the dc.js charts responsive

前端 未结 2 1893
萌比男神i
萌比男神i 2021-02-12 14:42

Is there a any way to make the dc.js charts responsive?

I\'m struggling a lot to make the charts fit the desktop size.

I\'m using Twitter Bootstrap

2条回答
  •  攒了一身酷
    2021-02-12 15:18

    I've set up a plunker with a dc.js demo that I have rerendering when resized, and getting the width of the container element to determine the size. This is embedded in an iframe and working as-is. I suggest playing with this plunker to fit your css, as I'm just making the simplest possible iframe setup. I'm imagining you did something similar to this, so I"m not exactly sure where you went wrong. Hopefully this helps.

    responsive DC chart in an iframe

    Given a standard page with an iframe:

    
      

    Test

    And an iframe with containers and scripts to load the chart:

     

    inside iframe

    A call to get the width you need

    var width = document.getElementById('box-test').offsetWidth;
    

    Using that width for the initial render

    chart
      .width(width)
      .height(480)
      .margins({top: 10, right: 50, bottom: 30, left: 50})
      .dimension(experimentDimension)
      .group(speedArrayGroup)
      .elasticY(true)
      .elasticX(true);
    

    And finally a function to handle what to do on window resize

    window.onresize = function(event) {
      var newWidth = document.getElementById('box-test').offsetWidth;
      chart.width(newWidth)
        .transitionDuration(0);
      pie.transitionDuration(0);
      dc.renderAll();
      chart.transitionDuration(750);
      pie.transitionDuration(750);
    };
    

提交回复
热议问题