I'm just starting out with Python/Django, and am trying to use the Chartit library to create some charts.
I've followed the instructions here, to setup a project with a simple line and a column chart. I can see the values when I hover over the charts.
But the lines or the columns themselves do not display, as seen in the screenshot below.
Following is the code I have in place for these charts.
MODEL:
class MonthlyWeatherByCity(models.Model):
def __str__(self):
return str(self.month) + "(H: " + str(self.houston_temp) + ", B: " +
str(self.boston_temp) + ")"
month = models.IntegerField()
boston_temp = models.DecimalField(max_digits=5, decimal_places=1)
houston_temp = models.DecimalField(max_digits=5, decimal_places=1)
VIEW:
def weather_chart_view(request):
#Step 1: Create a DataPool with the data we want to retrieve.
weatherdata = \
DataPool(
series=
[{'options': {
'source': MonthlyWeatherByCity.objects.all()},
'terms': [
'month',
'houston_temp',
'boston_temp']}
])
#Step 2: Create the Chart object
lcht = Chart(
datasource = weatherdata,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'month': [
'boston_temp',
'houston_temp']
}}],
chart_options =
{'title': {
'text': 'Weather Data of Boston and Houston'},
'xAxis': {
'title': {
'text': 'Month'}},
'yAxis': {
'title': {
'text': 'Temperature'}}
})
pcht = Chart(
datasource = weatherdata,
series_options =
[{'options':{
'type': 'pie',
'stacking': False},
'terms':{
'month': [
'boston_temp',
'houston_temp']
}}],
chart_options =
{'title': {
'text': 'Weather Data of Boston and Houston'},
'xAxis': {
'title': {
'text': 'Month'}},
'yAxis': {
'title': {
'text': 'Temperature'}}
})
#Step 3: Send the chart object to the template.
return render_to_response('polls/template.html', {'weatherCharts': [lcht, pcht]})
TEMPLATE:
<head>
<meta charset="UTF-8">
<title>Results: Question {{ question.id }}</title>
<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type ="text/javascript" src ="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/static/js/highcharts.js"></script>
{% load chartit %}
{{ weatherCharts|load_charts:"lcht, pcht" }}
</head>
<body>
<div id='lcht'> Chart will be rendered here </div>
<div id='pcht'> Chart will be rendered here </div>
</body>
</html>
No idea what I'm doing wrong, and it would be great if someone can point me in the right direction.
P.S. I'm not very experienced with web development, still learning :)
Update 1
Debugging shows me the data being loaded (See following image), and everything seems to be in order. Still at a loss as to why the lines aren't displayed.
Update 2
This link led me to pin-point the issue. Maybe it's a bug with Highcharts. Can anybody suggest a way to resolve it with django?
Adding
<script>
$(window).resize();
</script>
or
<script>
document.getElementById('lcht').style.width = "100%";
</script>
or
<script>
$('#lcht').resize();
</script>
does not resolve the problem.