First time poster, long time reader. I've spend quite awhile looking for an answer to this, which makes me think its something fundamental I'm missing.
I'm trying to pull data held in a database table and pass it through for display in a Highcharts plot. I'm not getting any errors from Django or by the client when inspecting source.
Using: Django 1.7 and Python 3.4
The views.py:
#unit/views.py
from django.http import JsonResponse
from django.shortcuts import render
from unit.analysis_funcs import ChartData
def chart_data_json(request):
data = {}
data['chart_data'] = ChartData.get_data()
return JsonResponse(data, safe = True)
def plot(request):
return render(request, 'unit/data_plot.html', {})
The get_data() function:
#unit/analysis_funcs.py
from unit.models import CheckValve
class ChartData(object):
def get_data():
data = {'serial_numbers': [], 'mass': []}
valves = CheckValve.objects.all()
for unit in valves:
data['serial_numbers'].append(unit.serial_number)
data['mass'].append(unit.mass)
return data
The template:
<!-- templates/unit/data_plot.html -->
{% extends "base.html" %}
{% block extrahead %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
{% endblock %}
{% block rootcontainer %}
<div id="container" style="width:100%; height:400px;"></div>
{% endblock %}
{% block overwrite %}
<!-- Overwrite the base.html jQuery load and put in head for Highcharts to work -->
{% endblock %}
{% block extrajs %}
<script>
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line'
},
series: [{}]
};
var ChartDataURL = "{% url 'chart_data_json' %}";
$.getJSON('ChartDataURL', function(data) {
options.xAxis.categories = data['chart_data']['serial_numbers'];
options.series[0].name = 'Serial Numbers';
options.series[0].data = data['chart_data']['mass'];
var chart = new Highcharts.Chart(options);
});
});
</script>
{% endblock %}
Finally the urls:
from unit import views, graphs
urlpatterns = patterns('',
url(r'^chart_data_json/', views.chart_data_json, name = 'chart_data_json'),
url(r'^plot/', views.plot, name = 'plot'),
)
Everything seems to run, but the Highchart plot doesn't render. I think its in how I'm moving the JSON data from the view.py to the template.html, but after so long staring at it I'm going cross-eyed.
Any help would be awesome!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…