简体   繁体   English

使用 zurb 基础自动调整 ajax 图表 rickshaw.js 的大小

[英]Auto resize ajax charts rickshaw.js with zurb foundation

I also have trouble with resizing charts.我也无法调整图表大小。 I'm using rickshaw and zurb foundation, I need autoresize ajax chart which will resizing within panel container like this:我正在使用人力车和 zurb 基础,我需要 autoresize ajax 图表,它将在面板容器中调整大小,如下所示:

<div class="panel>
  <div id="chart"></div>
</div>

If use non ajax charts, autoresize without zurb works with example from sources:如果使用非 ajax 图表,没有 zurb 的 autoresize 可以与来自来源的示例一起使用:

https://github.com/shutterstock/rickshaw/blob/master/examples/resize.html https://github.com/shutterstock/rickshaw/blob/master/examples/resize.html

I am actually in same situation than you.我其实和你的情况一样。

Thus, I searched in the source code and I've found a solution.因此,我在源代码中搜索并找到了解决方案。

You need to call the property graph before set the sizes.您需要在设置大小之前调用属性graph

For instance:例如:

var ajaxGraph = new Rickshaw.Graph.Ajax({ … });

$( window ).resize(function () {
  ajaxGraph.graph.configure({
    width: $("#chart").width(),
    height: $("#chart").width() * 0.5
  });
  ajaxGraph.graph.render();
});

As you can see, I use the property graph to access to graphic object and set the width and height.如您所见,我使用属性graph来访问图形对象并设置宽度和高度。

Best regards.此致。

I spent much time trying to resolve this and did not see a complete solution anywhere that precisely re-sized rickshaw bargraphs.我花了很多时间试图解决这个问题,但在任何地方都没有看到一个完整的解决方案,可以精确调整人力车条形图的大小。 None of the rickshaw examples shows how to do this correctly.人力车示例都没有显示如何正确执行此操作。 The solution presented here works, but contains are hard number value (height: $("#chart").width() * 0.5 ) to set the width of the chart/graph element.此处提供的解决方案有效,但包含用于设置图表/图形元素宽度的硬数值(高度: $("#chart").width() * 0.5 )。 This will re-size, however it will always look off or skewed within the containing html element.这将重新调整大小,但它总是会在包含的 html 元素中看起来偏离或倾斜。
To ensure correct sizing let the html rendering do the work for you by referencing the dimensions within the chart/graph containing element.为了确保正确调整大小,让 html 渲染通过引用包含元素的图表/图形中的维度来为您完成工作。 To dynamically re-size the rickshaw bargraph (or other rickshaw chart element) do the following:要动态调整人力车条形图(或其他人力车图表元素)的大小,请执行以下操作:

<!--div tag id="barGraphContainer with nested div tag containing the id    "bars" used to contain the rickshaw graph/chart element-->
<div id="barGraphContainer">
<div id="bars"></div>
</div>

<!--script tag to contain the resize() JS function-->
<script>

var graph = new Rickshaw.Graph({
element: document.getElementById("bars"),
renderer: 'bar',
series: [{data: [{ x: 0, y: 10 }, { x: 1, y: 18 }]),color: 'green'}]
});

//get the html element for the container
var barElement = document.getElementById("barGraphContainer"); 
var resize = function () {
graph.configure({
width: barElement.clientWidth, //html is "auto-magically" rendering size
height: barElement.clientHeight //leverage this for precise re-size scalling
});
graph.render();
}
window.addEventListener('resize', resize);
resize();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM