Hi,
I'm using FusionCharts in my Python Django project and I need to dynamically feed charts with data. That's why I need to render json data objects to the FusionCharts written in javascript, which are included in the HTML template but somehow it does not work. It seems that my problem is json independent so I have the same problem passing a string or a number variable.
I found the following instructions on StackOverflow: http://stackoverflow.com/questions/1445989/passing-python-data-to-javascript-via-django
So to call the variable one has to declare a new variable in javascript:
data_from_django = {{ my_data }};
or
var data_from_django = {{ my_data }};
My javascript looks like:
<script type="text/javascript">
FusionCharts.ready(function(){
var revenueChart = new FusionCharts({
"type": "column2d",
"renderAt": "bar_chart_container",
"width": "100%",
"height": "250",
"dataFormat": "json",
"dataSource": {
"chart": {
"xAxisName": "Month",
"yAxisName": "Expenses (€)",
....
},
"data": [
{"label": "Jan", "value": "420000"},
{"label": "Feb", "value": "810000"},
....]
}
});
revenueChart.render();
})
</script>
The charts work fine until I declare a new variable to pass the Python variable
<script type="text/javascript">
FusionCharts.ready(function(){
var test = {{ json_data }};
var revenueChart = new FusionCharts({
"type": "column2d",
"renderAt": "bar_chart_container",
..
..
..
Strange is that it also fails when I try to pass a normal string variable like for example
var test = {{ username }};
I'm not an expert in javascript but what do I miss here?