How to make flot responsive?

2019-02-03 08:25发布

问题:

In the following fiddle:

http://jsfiddle.net/jamitzky/9x7aJ/

How can I make the graph's width change if the window width changes?

code:

$(function () {
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
    d1.push([i, Math.sin(i)]);

var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

// a null signifies separate line segments
var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];

$.plot($("#placeholder"), [ d1, d2, d3 ]);
});

回答1:

Try something like this:

Flot will auto draw to the container it's in. So if your div is responsive Flot will be responsive.

http://jsfiddle.net/9x7aJ/2029/

then all you have to do is redraw the flot if your window resizes:

You can watch to see if the window resizes with:

    window.onresize = function(event) {
    ...
}

(see: JavaScript window resize event)



回答2:

Also there's a official flot resize plugin

http://people.iola.dk/olau/flot/examples/resize.html



回答3:

Just include jquery.flot.resize.min.js script from https://github.com/flot/flot in your page. It will become responsive.



回答4:

I had the same problem and the solution was:

<script language="javascript" type="text/javascript" src="http://people.iola.dk/olau/flot/jquery.js"></script>
<script language="javascript" type="text/javascript" src="http://people.iola.dk/olau/flot/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="http://people.iola.dk/olau/flot/jquery.flot.resize.js"></script>
<script src="./jquery.flot.axislabels.js"></script>


回答5:

Just use some css. Any modern browser can use (inline, stylesheet):

<div id="chart" style="width: 100%!important"></div>


标签: flot