调整浏览器大小时jqplot调整图表大小

hac*_*ket 15 jqplot

在调整浏览器大小时,是否有一种简单的方法可以自动调整jqplot图表的大小?我一直在寻找谷歌,但没有找到任何东西.

Mar*_*ark 26

这里讨论调整jqplots的大小.

要在调整浏览器大小时使其工作,请将replot函数与window.resize事件绑定:

$(window).resize(function() {
      plot1.replot( { resetAxes: true } );
});
Run Code Online (Sandbox Code Playgroud)

运行代码:

$(document).ready(function(){
    var plot1 = $.jqplot ('container', [[3,7,9,1,4,6,8,2,5]], {});
    
    $(window).resize(function() {
          plot1.replot( { resetAxes: true } );
    });
    
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.8/jquery.jqplot.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.8/jquery.jqplot.min.css">
     
<div id="container"></div>
Run Code Online (Sandbox Code Playgroud)

  • 是的,这应该做+1.在我的resize方法中,我还添加了下面的代码 - 就在我调用`replot()之前.这样我确保`barWidth`也将被重新调整.`$ .each(plot.series,function(index,series){series.barWidth = undefined;}); ` (9认同)
  • [示例显示问题.](http://jsfiddle.net/RNPxQ/22/)[示例显示解决方案.](http://jsfiddle.net/RNPxQ/21/) (3认同)

Gil*_*rts 5

我发现如果你的图表上有很多选项,使用replot并不总能给出一致的结果.我发现复杂图形处理窗口大小调整的唯一方法是获得残酷并摧毁它并重建它.

function () {

    var plot;

    var renderGraph = function() {
        plot = $.jqplot('chartId', yourChartData, yourChartOptions);
    }

    renderGraph();

    var resizeGraph = function() {
        if (plot)
            plot.destroy();
        renderGraph();
    }

    $(window).resize(function() {
        resizeGraph();
    });
}
Run Code Online (Sandbox Code Playgroud)