Search code examples
jqueryflotr2

Flotr2 can't see jQuery object


I'm trying to create some simple visualizations with flotr. However when I try to specify the div container for the chart I get an error saying:

Uncaught The target container must be visibleGraph._setEl @ flotr2.min.js:27Graph @ flotr2.min.js:27n.draw @ flotr2.min.js:27(anonymous function) @ lines.html:25i @ jquery.min.js:2j.fireWith @ jquery.min.js:2n.extend.ready @ jquery.min.js:2K @ jquery.min.js:2

This work however when I change the jQuery $("#chart") selector to document.getElementById("chart"). Basically when I use vanilla JavaScript it works but when I switch to jQuery it fails.

A simplified recreation is below.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="JS_libs/flotr2.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>
    <body>
        <div id='chart' style="width:600px;height:300px;"></div>
        <!--[if lt IE 9]><script src="js/excanvas.min.js"></script><![endif]-->

        <script>
            $(function() { var co2 = [
            [ 1959, 315.97 ],
            [ 1960, 316.91 ],
            [ 1961, 317.64 ],
            [ 1962, 318.45 ]];
            var temp = [
            [ 1959,  0.0776 ],
            [ 1960,  0.0280 ],
            [ 1961,  0.1028 ],
            [ 1962,  0.1289 ]];

            Flotr.draw(
            $("#chart"),
            [{ data: co2, lines: {show:true} }]
            );});
        </script>

    </body>
</html>

If I change the $("#chart") to document.getElementById("chart"), it works. But I would like to use the jQuery way.


Solution

  • To get native JS object from jQuery you have to use [0] :

    Flotr.draw(
        $("#chart")[0],
        [{ data: co2, lines: {show:true} }]
    );});