所以我一直在尝试用django-nvd3显示一个图表。它与django-nvd3简介中的代码基本相同。但它不显示图表,它只打印显示图表所需的javascript。我希望有人能给我指出正确的方向。
我看得到剧本了。Firebug在标题中显示d3.min.js
和nv.d3.min.js
的内容。
我还尝试使用jquery来查看是否可以让javascript正常工作,并且成功了。在
def temp_chart_view(request):
xdata = ["Apple", "Apricot", "avocado"]
ydata = [10, 20, 30]
chartdata = {'x': xdata, 'y': ydata}
charttype = "pieChart"
chartcontainer = 'piechart_container'
data = {
'charttype': charttype,
'chartdata': chartdata,
'chartcontainer': chartcontainer,
'extra': {
'x_is_date': False,
'x_axis_format': '',
'tag_script_js': False,
'jquery_on_ready': False,
}
}
return render_to_response('temperatures/chart.html', data)
{% load nvd3_tags %}
<head>
{% include_chart_jscss %}
{% load_chart charttype chartdata chartcontainer extra %}
</head>
<body>
<h1>tere</h1>
{% include_container chartcontainer 400 600 %}
</body>
<head>
<link rel="stylesheet" type="text/css"
href="/static/nvd3/src/nv.d3.css" media="all">
<script type="text/javascript" src="/static/d3/d3.min.js">
<script type="text/javascript" src="/static/nvd3/nv.d3.min.js">
</head>
<body>
nv.addGraph(function() {
var chart = nv.models.pieChart();
chart.x(function(d) { return d.label })
.y(function(d) { return d.value });
chart.height(450);
chart.showLegend(true);
chart.showLabels(true);
d3.select('#piechart_container svg')
.datum(data_piechart_container[0].values)
.transition().duration(500)
.attr('height', 450)
.call(chart);
return chart;
});
data_piechart_container=[{"values": [
{"value": 10, "label": "Apple"},
{"value": 20, "label": "Apricot"},
{"value": 30, "label": "avocado"}
], "key": "Serie 1"}];
<h1>tere</h1>
<div id="piechart_container">
<svg style="width:600px;height:400px;"></svg>
</div>
</body>
<script>{% load_chart charttype chartdata chartcontainer extra %}</script>
似乎
django-nvd3
没有自动将javascript包装为script
。在尝试在
templates/chart.html
中的load_chart
模板标记周围加上<script>
标记:我只是更新文档来纠正这个错误。默认情况下,django-nvd3不包含标记脚本,以便在哪里添加javascript代码提供更大的灵活性。在
有一个额外的设置可以设置为输出标记,它被称为tag_script_js。在
此代码应该可以正常工作:
相关问题 更多 >
编程相关推荐