Reputation: 675
What's the most elegant solution in Django to take data from database and pass it into Highcharts? I stumbled across this question, but I keep on getting the following error: JSerror: Invalid property id at var chart_id = {{ chartID|safe }}
, if I try to use it in my example.
template looks like this:
<html>
<div id={{ chartID|safe }} class="chart" style="height: 100px; width: 500px"></div>
<script>
var chart_id = {{ chartID|safe }}
var series = {{ series|safe }}
var title = {{ title|safe }}
var xAxis = {{ xAxis|safe }}
var yAxis = {{ yAxis|safe }}
var chart = {{ chart|safe }}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
In case there are better ways to pass data to highcharts please let me know.
Upvotes: 2
Views: 628
Reputation: 151
You must pass your data as a context to the template, change it to a list in python. Then in your javascript block create a script tag then declare your data variables that you passed ,then pass them to your plotting script.
Upvotes: 0
Reputation: 25559
Maybe you are missing the quotes in your javascript:
var chart_id = "{{ chartID|safe }}";
Upvotes: 3