Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct data input from Django for a D3 graph

It seems all the D3 example graphs take an external .csv or .tsv file as input data. Is there any way to modify the code to take data from a variable in Django. Suppose {{ data }} is in JSON format, how do you implement this in a graph such as http://bl.ocks.org/3885304 or http://bl.ocks.org/3887051 ? I'm trying to avoid always writing a .csv file.

like image 433
ono Avatar asked Nov 08 '12 16:11

ono


1 Answers

You can always make a view which will serve dynamic csv file which will be consumed by D3. This way will also allow users to download the data in case they need the raw data instead of a graph.

def foo(request, ...):
    model = get_object_or_404(Foo, ...)
    data = model.get_data() # should return csv formatted string
    return HttpResponse(data, content_type='text/csv')
like image 142
miki725 Avatar answered Oct 14 '22 05:10

miki725