30

I using flot pie chart for plotting pie charts. but it shows error in firebug that

uncaught exception: Invalid dimensions for plot, width = null, height = null

I have given the height and width from stylesheet also. and tried also like this

<div id="placeholder1" style="width:140px;height:140px" ></div>

how to resolve this?

user1662343
  • 303
  • 1
  • 3
  • 4

7 Answers7

41

Check these:

  • You include the jQuery library first and then flot js library

  • Wrap the whole code within $(document).ready() handler function.

  • you bind the flot with correct id and there is no repeat of same id.

  • if your div is dynamic i.e. appeared to DOM after page load then bind plot() after the element appeared to DOM.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 4
    I check all these , but result is same. – user1662343 Sep 12 '12 at 04:00
  • 1
    In my case the – lilalinux May 28 '14 at 16:27
  • 1
    also, check all references in JS to make sure you are calling existing DIV id. In my case there was more than one call to the DIV, and I missed it thinking there was only one. – Dennis Aug 08 '14 at 14:52
  • 1
    You need to re-fetch the DOM element for your chart. It gets changed by flot when you call $.plot. i.e. do something like this: $.plot($("#myChart"), data, options); – Brian Corbin Dec 10 '15 at 18:24
  • Bullet #3 (ID related) happens more than expected...thanks! – Koshinae Sep 15 '16 at 08:41
14

I also had the same problem. I set a height and a width to the <div> tag and it fixed the error.

You can use either inline css (as I did) or JS to set the height and the width. But plot() function should be called only after we set the height of the <div>

<div class="bars_two" style="height:300px;"></div>
John Fonseka
  • 793
  • 1
  • 8
  • 15
10

I had this same problem too! The answer was not listed above, so here was my problem and solution.

<div id="chart" class="chart" style="width:100%;height:250px;"></div>

Javascript:

 <script type="text/javascript">
    $(document).ready(function () {
        var data = [...];
        var options = {...};
        $.plot($("#placeholder"), data, options);
    });
</script>

So. Notice the plot function. At first, I didn't have the # in the first parameter. That is necessary apparently. It solved my problems.

GG.

Millar248
  • 403
  • 8
  • 18
7

A quick solution I found is to just type something between the div tags. For example: Change <div id="placeholder"></div> to <div id="placeholder">.</div> or <div id="placeholder">randomtext</div>.

Personally, I found that using space/tabs/newlines doesn't work in this quick solution.

hexicle
  • 2,121
  • 2
  • 24
  • 31
3

If you put your chart html in div which is given "display: none" class you will get this error. You must put your chart html in div which is given "display:block" class when "jquery.flot.js" is loaded.

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

if this html is in any div which has class="display: none" when "jquery.flot.js" is loaded, change it to display: block

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Hubeyb Özkul
  • 646
  • 7
  • 8
2

I had the same problem integrating template to rails. That's a bad way, but I just comment the line which throws the exception and add 2 lines to set width and height to 0 - it helped me.

Canvas.prototype.resize = function(width, height) {

  if (width <= 0 || height <= 0) {
    // COMMENTED NEXT LINE
    // throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height);
    // NEW LINES ADDED
    width = 0;
    height = 0;
  }

  // ... others code 
}
kpblc
  • 903
  • 1
  • 8
  • 24
1

So, it seems that before you render to a chart, you need to re-fetch the DOM element. Since flot manipulates the elements and inserts its own, it looks like it's replacing the element with its own elements.

In your render, do:

$.plot($(".myChartId || #myChartClass"), myData, myOptions);

This has nothing to do with being in $(document).ready(), although it is good practice to place it inside of this.

Hope this helps!

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Brian Corbin
  • 267
  • 1
  • 12