Peterb
Peterb

Reputation: 239

How to include flotr2 charts in rails?

I'm trying to include a chart in a view, but it doesn't work. Can somebody help?

That's what I have in the view (flotr2.min.js is in public/javascripts):

...
<div id="container">
   <!--[if lt IE 9]>
   <script type="text/javascript" src="/static/lib/FlashCanvas/bin/flashcanvas.js">     </script>
   <![endif]-->
   <%= javascript_include_tag "flotr2.min.js" %>
   <script type="text/javascript">

   (function () {
      var d1 = [
        [1, 10], [2, 8], [3, 5],[4, 13]],
    d2 = [[1,12],[2,12],[3,12],[4,12],[5, 12]]

   graph = Flotr.draw(container, [{data: d1, label: "Test"}, {data: d2, lines:{show: true}, points: {show: true}}], {
mode: 'time',
    xaxis: {
    ticks: [[1, "Jan"],[2, "Feb"], [3, "Mrz"], [4, "Apr"], [5, "Mai"], [6, "Jun"],
                [7, "Jul"],[8, "Aug"], [9, "Sep"], [10, "Okt"], [11, "Nov"], [12, "Dez"]]     

    },
    grid: {
        minorVerticalLines: true,
    backgroundColor: 'white'
     }
  });
 })();
 </script>

</div>

Upvotes: 2

Views: 1415

Answers (2)

Kuba
Kuba

Reputation: 2089

You are missing container variable in your Script. Include flotr2.min.js in the head. Container MUST have positive width:

<div id="container" style="height: 420px; width: 100%;"></div>

Try it like that:

 <script type="text/javascript">
    var container = document.getElementById('container'),
    d1 = [[1, 10], [2, 8], [3, 5],[4, 13]], 
    d2 = [[1,12],[2,12],[3,12],[4,12],[5, 12]];

       Flotr.draw(container, [{data: d1, label: "Test"}, {data: d2, lines:{show: true}, points: {show: true}}],
     {mode: 'time',
        xaxis: {
        ticks: [[1, "Jan"],[2, "Feb"], [3, "Mrz"], [4, "Apr"], [5, "Mai"], [6, "Jun"],
                    [7, "Jul"],[8, "Aug"], [9, "Sep"], [10, "Okt"], [11, "Nov"], [12, "Dez"]]     
        },
        grid: {
            minorVerticalLines: true,
        backgroundColor: 'white'
         }
      });
    </script>

Upvotes: 0

Steven
Steven

Reputation: 3894

You might need to fix your container size, try having this style in your head:

<style type="text/css">
  #container {
    width : 600px;
    height: 384px;
    margin: 8px auto;
  }
</style>

I have also found the current version on github isnt working. Try www.humblesoftware.com/static/js/flotr2.min.js

Upvotes: 1

Related Questions