【发布时间】:2014-01-29 19:53:01
【问题描述】:
我想用 django-chartit 图表处理事件点击,下面是 python 代码。
def basicpie(request, title, code, doc, sidebar_items):
def monthname(month_num):
names ={1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
return names[month_num]
ds = DataPool(
series=
[{'options': {
'source': MonthlyWeatherByCity.objects.all()},
'terms': [
'month',
'boston_temp']}
])
cht = Chart(
datasource = ds,
series_options =
[{'options':{
'type': 'pie',
'stacking': False},
'terms':{
'month': [
'boston_temp']
}}],
chart_options =
{'title': {
'text': 'Monthly Temperature of Boston'},
'plotOptions': {
'series': {
"cursor": "pointer",
'point': {
'events': {
'click': 'function() {alert(\'clicked\');}'
}
}
}
}
},
x_sortf_mapf_mts = (None, monthname, False))
return render_to_response('chart_code.html', {'chart_list': cht,
'code': code,
'title': title,
'doc': doc,
'sidebar_items': sidebar_items})
和django-chartit的javascript代码
$(document).ready(function() {
$.each(_chartit_hco_array, function(index, chartoptions) {
chart = new Highcharts.Chart(chartoptions);
});
});
但是当我点击图表时出现以下错误。
Uncaught TypeError: Object function() {alert('clicked');} has no method 'apply'.
请帮我解决这个问题。
【问题讨论】:
-
应该是
'click': function() {alert('clicked');}? -
为什么
'click': 'function() {alert(\'clicked\');}'在引号中? -
"foobar".apply()抛出同样的错误 -
我的猜测:因为它可能来自错误的 JSON 复制/粘贴。出于某种原因,语法高亮对于新手来说是不可见的,当它应该是出现问题的第一条线索时。
-
@elclanrs 在我看来,他似乎打算用引号括起来,因为他避开了内部引号。
标签: javascript jquery python django highcharts