kingbrain
kingbrain

Reputation: 116

Django d3.js load JSON data

I am trying to make a histogram with d3.js. I am new at javascript and JSON so I need your help.

Basically I am trying to use this code here: How to make a bar chart for time duration with d3?

The shown code here with the data in the code work fine, but how do I refer to my JSON outputted data?

urls.py for my JSON data

url(r'^api/playAround', views.playAround, name='playAround'),

view.py

def playAround(request):
data = Customer.objects.annotate(month=TruncMonth('Update')).values('month').annotate(countItems=Count('id')) 
return JsonResponse(list(data), safe=False)

localhost/api/playAround

[
    {
     "month": "2017-10-01",
     "countItems": 16,
    },
    {
     "month": "2018-04-01",
     "countItems": 1,
    },
    {
     "month": "2018-10-01",
     "countItems": 1,
    },
]

How do I have to change the line

var data =....

that I get my JSON data read and displayed?

The line

d3.json("{% url "playAround" %}", function(error, data) {}

did not work. I think I do not get a main point in d3.js+JSON+javascript

template

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}

.bar:hover {
fill: brown;
}

.axis--x path {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="/static/js/d3.v5.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {
  top: 20,
  right: 20,
  bottom: 30,
  left: 40
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;

var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);

var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var data = d3.json("{% url 'playAround' %}", function(error, data) {

x.domain(data.map(function(d) {
return d.month;
}));
y.domain([0, d3.max(data, function(d) {
return d.countItems;
})]);
});

g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");

g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
  return x(d.month);
})
.attr("y", function(d) {
  return y(d.countItems);
})
.attr("width", x.bandwidth())
.attr("height", function(d) {
  return height - y(d.countItems);
});

Upvotes: 1

Views: 1732

Answers (1)

kingbrain
kingbrain

Reputation: 116

I found the solution. You have to do one thing when accessing the JSON objects in contrast to having the data in the code:

As you do not have a global variable of your data, you have to include all the domain or append information within d3.json.

So here is the relevant code:

d3.json("{% url "playAround" %}", function(error, data) {   

    .....   // insert here the x./y.domain and g.append information

});

Plus that the code above is only working with d3.v4.min.js.

Upvotes: 1

Related Questions