Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  119] [ 1]  / answers: 1 / hits: 5433  / 11 Years ago, fri, december 27, 2013, 12:00:00

Based on this Highcharts example (javascript code included in a HTML): http://jsfiddle.net/f4Ef7/



I have a template where I want to embed that JavaScript code without having to include any static. Anything not related with JS is being processed by the browser. Currently my views.py looks like:



# -*- encoding: utf-8 -*-

from django.shortcuts import render
from django.http import HttpResponse
from tfgplot.models import estado_foneras
from django.template import RequestContext, loader
def index(request):
template = loader.get_template('tfgplot/index.html')
context = RequestContext(request)
return render(request, 'tfgplot/index.html', context)


My application is called tfgplot and the template index.html looks like:



<div id=container style=min-width: 300px; height: 300px; margin: 1em>
<script src=http://code.highcharts.com/highcharts.js></script>
<script src=http://code.highcharts.com/modules/exporting.js></script>
<head></head>
<body>
<div>
<script type=text/javascript>
{% autoescape off %}
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]

});
{% endautoescape %}
</script>
</div>
</body>


This should create a graphic like the one that can be seen in the link but I'm not able to see that graphic I'm expecting, any ideas?


More From » django

 Answers
5

First of all, your html is quite messy, here are a few things:




  1. Every div or almost any other html tag should reside inside the body tag.

  2. You should load the scripts inside the head tag or at the end of the body tag.

  3. You should wait until the DOM is ready to create the chart or do any other javascript work.

  4. You need jQuery in order to make $('#container') work.

  5. div elements as container must be closed.

  6. You shouldn't add css styles to a html element directly. Instead do it in a separate file (something like styles.css).

  7. There's no need for your script to be inside a div, you could get rid of it.



Here is a code that should do what you want:



<!DOCTYPE html>
<html>
<head>
<script type=text/javascript src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script>
<script src=http://code.highcharts.com/highcharts.js></script>
<script src=http://code.highcharts.com/modules/exporting.js></script>
</head>
<body>
<div id=container style=min-width: 300px; height: 300px; margin: 1em>
</div>
<script type=text/javascript>
{% autoescape off %}
$(document).ready(function(){
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
{% endautoescape %}
</script>
</body>
</html>


For more information about html/css standards check this link from google.



Also, since you are working with django, you need to be aware of template inheritance. If the above code doesn't work, share more code and/or errors information.


[#49193] Thursday, December 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 3 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;