How to make the dc.js charts responsive

杀马特。学长 韩版系。学妹 提交于 2019-12-03 23:10:34

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:

<body>
  <h1>Test</h1>
  <iframe src="iframe.html" class="iframe"></iframe>
</body>

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

<body> <h2>inside iframe</h2> 
   <div id="box-test"></div>
   <div id="pie-chart"></div> 
   <script src="script.js" type="text/javascript"></script> 
</body>

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);
};

When you pass null when calling .width() and .height() methods (or not calling them at all) on the dc.js Chart instance, it will use width and height of the anchor element (which can then be styled with some evergreen responsive css classes either from bootstrap or some other responsive css framework ;)).

For more info see: https://github.com/dc-js/dc.js/blob/master/web/docs/api-latest.md#dc.baseMixin+height

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