admin管理员组

文章数量:1277401

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 or ? I'm trying to avoid always writing a .csv file.

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/3885304 or http://bl.ocks/3887051 ? I'm trying to avoid always writing a .csv file.

Share Improve this question asked Nov 8, 2012 at 16:23 onoono 3,10410 gold badges45 silver badges88 bronze badges 3
  • 1 github./mbostock/d3/wiki/Requests#wiki-d3_json – Martin Commented Nov 8, 2012 at 16:34
  • If I'm using d3.json, what is the URL and callback? – ono Commented Nov 8, 2012 at 16:40
  • I misunderstood your question, I thought you wanted to serve it as json using http. See my answer for details. – Martin Commented Nov 8, 2012 at 17:05
Add a ment  | 

2 Answers 2

Reset to default 6

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')

Instead of loading data asynchronously (ajax-style), you can use correctly formatted JSON in a string passed to your template tag variable and |safed.

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

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

本文标签: javascriptDirect data input from Django for a D3 graphStack Overflow