1

I'm studying this jqplot chart an I would like to change

      var y = Math.random();

with some variables from a "real-time-external-function" (i.d. not from DB, txt, etc.).

I tried with:

c.py

import time

def prova(z):

    z = 0
    while z < 10:
        time.sleep(1)
        z = z + 1
        print(z)

home.html

...

            <!--[if lt IE 9]><script src="{% static 'jqplot/excanvas.js"></script><![endif]-->
            <script src="{% static 'jquery-1.11.2.js' %}"></script> 

            <script src="{% static 'jqplot/jquery.jqplot.min.js' %}"></script>
            <script src="{% static 'jqplot/plugins/jqplot.dateAxisRenderer.min.js' %}"></script>
            <link rel="stylesheet" type="text/css" href="static/jqplot/jquery.jqplot.min.css" />

            <script src="{% static 'js/provachart.js' %}"></script>
            <div id="myChart" style="height:300px; width:500px;"></div>
            <button>Start Updates</button>

...

provachart.js

    $(document).ready(function(){   

    ... //the code is at the link above


    $('button').click( function(){  
        doUpdate();
        $(this).hide();
    });

    function doUpdate() {

        if(data.length > n-1){
            data.shift();
        }

        // instead of var y = Math.random();
        var y = $get("/conta/");        
 // or  var y = $get("/conta/",function( num ) {
 //                   $( "y" )
 //                   .append(num);});      
        var x = (new Date()).getTime();

        data.push([x,y]);
        if (plot1) {
            plot1.destroy();                                
        }
        plot1.series[0].data = data;                        

        options.axes.xaxis.min = data[0][0];                
        options.axes.xaxis.max = data[data.length-1][0];
        plot1 = $.jqplot ('myChart', [data],options);       
        setTimeout(doUpdate, t);
    }

    })

views.py

        ...
    import c
    def conta(request):
         y= c.prova(0)
         return JsonResponse({'y': y})

// or def conta(request):
//      num = c.prova(0)
//      return JsonResponse({'num': num})
        ...

So I call my views with this url: urls.py

...
url(r'^conta/$', views.conta, name='conta'),
...

I know, it does not plot y... but I have not any ideas. I thought it was like this.

Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27

0 Answers0