Direct Data Entry from Django for D3 Chart

It seems that all D3 graphical examples accept an external .csv or .tsv file as input. Is there a way to change the code to transfer data from a variable in Django. Suppose {{data}} is in JSON format, how do you implement this in a graph, for example http://bl.ocks.org/3885304 or http://bl.ocks.org/3887051 ? I am trying to avoid writing a CSV file.

+6
source share
2 answers

You can always make a presentation that will serve the dynamic csv file that D3 will use. This method will also allow users to download data if they need 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') 
+6
source

Instead of loading data asynchronously (ajax-style), you can use properly formatted JSON in the string passed to the template tag variable, and |safe d.

Check out the working example http://bl.ocks.org/4040034 , which is based on http://bl.ocks.org/3885304

You should also check related questions on SO, there are tons on the topic.

+5
source

All Articles