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 tryin
I finally got my plotting working. I found this approach here. Thanks to Harrison for posting his method!
My new views.py based on the above approach:
def plot(request, chartID = 'chart_ID', chart_type = 'line', chart_height = 500):
data = ChartData.check_valve_data()
chart = {"renderTo": chartID, "type": chart_type, "height": chart_height,}
title = {"text": 'Check Valve Data'}
xAxis = {"title": {"text": 'Serial Number'}, "categories": data['serial numbers']}
yAxis = {"title": {"text": 'Data'}}
series = [
{"name": 'Mass (kg)', "data": data['mass']},
{"name": 'Pressure Drop (psid)', "data": data['pressure drop']},
{"name": 'Cracking Pressure (psid)', "data": data['cracking pressure']}
]
return render(request, 'unit/data_plot.html', {'chartID': chartID, 'chart': chart,
'series': series, 'title': title,
'xAxis': xAxis, 'yAxis': yAxis})
Quick function for pulling database objects and passing the data:
class ChartData(object):
def check_valve_data():
data = {'serial numbers': [], 'mass': [],
'pressure drop': [], 'cracking pressure': [], 'reseat pressure': []}
valves = CheckValve.objects.all()
for unit in valves:
data['serial numbers'].append(unit.serial_number)
data['mass'].append(unit.mass)
data['cracking pressure'].append(unit.cracking_pressure)
data['pressure drop'].append(unit.pressure_drop)
data['reseat pressure'].append(unit.reseat_pressure)
return data
The key to Harrison's method is a mapping script to translate the Highcharts js to Python template variables:
{% extends "base.html" %}
{% block extrahead %}
<!-- Load in jQuery and HighCharts -->
<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 heading %}
<h1 align="center">Analysis</h1>
{% endblock %}
{% block content %}
<div id={{ chartID|safe }} class="chart" style="height:100px; width:100%"></div>
{% endblock %}
{% block overwrite %}
<!-- Overwrite the base.html jQuery load and put in head for Highcharts to work -->
{% endblock %}
{% block extrajs %}
<!-- Maps the Python template context variables from views.py to the Highchart js variables -->
<script>
var chart_id = {{ chartID|safe }}
var chart = {{ chart|safe }}
var title = {{ title|safe }}
var xAxis = {{ xAxis|safe }}
var yAxis = {{ yAxis|safe }}
var series = {{ series|safe }}
</script>
<!-- Highchart js. Variable map shown above -->
<script>
$(document).ready(function() {
$(chart_id).highcharts({
chart: chart,
title: title,
xAxis: xAxis,
yAxis: yAxis,
series: series
});
});
</script>
{% endblock %}
Everything works and displays correctly now!