I am using Python Django to create an interactive Dashboard. The main reason why I decided to use Django is because I can create users and groups to give different access to different people. Thought this was the better way.
I have massive reports built on excel sheets that I can convert to .CSV and machine readable. I read the best way to load data to D3 is through d3.csv(). I've tried that without success and now have tried with d3.json() but still I have used another code sample to try and plot my own but could have any luck.
The code is as follows. Important note: In dashboard.html
, there is a selector, thus the HTML Template has that extends
the "dashboard" template.
# app/models.py
from django.db import models
class wrtes(models.Model):
name = models.CharField(max_length=150)
date = models.DateTimeField()
# app/urls.py
from django.conf.urls import url
from . import views
from .views import graph, oneModel
urlpatterns = [
# Dashboard parts
url(r'^oneModel/', graph),
url(r'^api/oneModel', oneModel, name='oneModel'),
]
# app/views.py
def graph(request):
return render(request, 'app/oneModel.html')
def oneModel(request):
data = oneModel.objects.all() \
.extra(select={'month': connections[oneModel.objects.db].ops.date.trunc_sql('month', 'date')}) \
.values('month') \
.annotate(count_items=Count('id'))
return JsonResponse(list(data), safe=False)
<!-- app/oneModel.html -->
{% extends 'app/dashboard.html' %}
{% load staticfiles %}
{% block content %}
<script src="https://d3js.org/d3.v3.js"></script> <!-- D3 obligatory tags -->
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d").parse; // for dates like "2014-01-01"
//var parseDate = d3.time.format("%Y-%m-%dT00:00:00Z").parse; // for dates like "2014-01-01T00:00:00Z"
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.month); })
.y(function(d) { return y(d.count_items); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("{% url "oneModel" %}", function(error, data) {
data.forEach(function(d) {
d.month = parseDate(d.month);
d.count_items = +d.count_items;
});
x.domain(d3.extent(data, function(d) { return d.month; }));
y.domain(d3.extent(data, function(d) { return d.count_items; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Play count");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
{% endblock %}
When running the local server, it does not render anything in the graphics part. It renders the remaining parts - navbar, selector & so on. It makes me consider the dataset is not being loaded in any way. This one in particular, does not even make sense :-D But when I tried with the d3.csv() method, it was not working either way.
- What is wrong with the code?
- What is the best way to pass .csv files to D3 to plot cute stuff? If you'll be referencing guides, please consider I am not a programmer nor a very experienced one.
Thank you for your time.