0

I would like to do some graphics in my django application. I'm using Flot. I am a newbee with regard to javascript and I do not know how to pass parameters in my situation. This is my code:

view.py

data = []
query = tab_data.objects.all()
for row in query:
    data.append([str(row.time), str(row.rate)])
data = json.dumps(data, cls=DjangoJSONEncoder)
...
return render_to_response('index.html', {'data' : data}, context_instance=RequestContext(request))

index.html

<div class="portlet-body">
       <div id="site_statistics_loading">
           <img src="/static/assets/img/loading.gif" alt="loading" />
       </div>
       <div id="site_statistics_content" class="hide">
            <div id="site_statistics" class="chart"></div>
       </div>
</div>
...
... # at the end of the page I have this
Index.initCharts(); // init index page's custom scripts

Index.js

initCharts: function () {
   if (!jQuery.plot) {
            return;
        }
    var data = []; # Here I would like my data. 

How do I pass the data to file index.js? Sorry for the trivial question but I do not know how to do ... thanks

4

1 回答 1

2

A series can either be raw data or an object with properties. The raw data format is an array of points:

[ [x1, y1], [x2, y2], ... ]

E.g. (in your case)

data=   [[1, 3], [2, 14.01], [3.5, 3.14]];   //You can build this string in the html generated and/or add it with jquery/ajax callback function.

If you want some more sophisticated (with styles in the lines and dots for example you have to build basically building the "option" parameter.

$.plot(placeholder, data, options);

placeholder is the div container which will contain the graph. data is the data in the format described before. (Take care of the String building) options is the option of the graph (lines, series, style, dots).

You will find more information here http://flot.googlecode.com/svn/trunk/API.txt

于 2013-06-10T14:22:24.033 回答