Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqplot resize chart when resizing browser

Tags:

jqplot

Is there an easy way to auto-resize a jqplot chart when resizing the browser? I have been looking on Google, and haven't found anything.

like image 242
hacket Avatar asked Jul 20 '12 18:07

hacket


2 Answers

Resizing jqplots is discussed here.

To make it work when the browser is resized, bind the replot function up with the window.resize event:

$(window).resize(function() {
      plot1.replot( { resetAxes: true } );
});

Running code:

$(document).ready(function(){
    var plot1 = $.jqplot ('container', [[3,7,9,1,4,6,8,2,5]], {});
    
    $(window).resize(function() {
          plot1.replot( { resetAxes: true } );
    });
    
});
<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>
like image 64
Mark Avatar answered Sep 29 '22 03:09

Mark


I've found that using replot doesn't always give consistent results if you've got a lot of options on your graphs. The only way I've found to have complex graphs cope with window resizes is to get brutal and destroy it and rebuild it.

function () {

    var plot;

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

    renderGraph();

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

    $(window).resize(function() {
        resizeGraph();
    });
}
like image 43
Giles Roberts Avatar answered Sep 29 '22 03:09

Giles Roberts