我正在编写一个django网络应用程序,我使用highcharts打印图形。我必须使用一些技巧才能让它在django中运行,但有些东西我无法理解。
看看这里:
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/spline-symbols/
有最高和最低温度的太阳和云。我想做类似的东西。
我的views.py看起来像这样:
# load data to variable last_day_data
class Chartdata(object):
def load_last_day():
values = Temperature.objects.raw('SELECT...')
for x in values:
last_day_data['avg_temperature'].append(round(x.avg_temperature,2))
last_day_data['date'].append('%s:00' % x.id)
return last_day_data
# set-up highcharts variables and pass to home.html
def home(request):
last_day_data = Chartdata.load_last_day()
chart3 = {"renderTo": 'chart_3', "type": 'line', "height": 300,}
title3 = {"text": 'Last 24 Hours Temperature'}
xAxis3 = {"title": {"text": 'Date'}, "categories": last_day_data['date'], "reversed": 'true'}
yAxis3 = {"title": {"text": 'Temperature [°C]'}}
series3 = [{"name": 'Temperature [°C]', "data": last_day_data['avg_temperature']}]
chart = {
'chartID3': 'chart_3',
'chart3': chart3,
'series3': series3,
'title3': title3,
'xAxis3': xAxis3,
'yAxis3': yAxis3
}
return render(request, 'home.html', chart)
快速查看home.html(实际上只有一部分负责将传递的数据映射到高级变量):
<script>
$(document).ready(function() {
$({{ chartID3|safe }}).highcharts({
chart: {{ chart3|safe }},
title: {{ title3|safe }},
xAxis: {{ xAxis3|safe }},
yAxis: {{ yAxis3|safe }},
series: {{ series3|safe }}
});
});
</script>
现在,在加载我的网站后,上面的代码(确切地说是'系列')如下所示:
series: [{'name': 'Temperature [°C]', 'data': [12.0, 13.11, 14.59, 14.6, 14.36, 14.09, 13.73....]}]
我需要做的事情:找到最高值并替换为(根据jsfiddle):
{{y: highest_value, marker: {symbol: 'url(sun url)'}}
找到最高价值很容易,假设我将它存储在'index'下的'value'中,所以我这样做:
last_day_data['avg_temperature'][index] = "{y: %s, marker: {symbol: 'url(sun url)'}}" % value
但图表无法正确显示并查看网站来源:
series: [{'name': 'Temperature [°C]', 'data': [12.0, 13.11, 14.59, "{y: 14.6, marker: {symbol: 'url(sun url)'}}", 14.36, 14.09, 13.73, 13.29, 12.86, 12.82.... ]}]
结论:它不使用引号,我不知道如何摆脱它们。任何想法如何快速,轻松,轻松地完成?
答案 0 :(得分:1)
最后弄明白了,改成了:
last_day_data['avg_temperature'][index_max] = "{\"y\": %s, \"marker\":" \
" {\"symbol\": \"url(%ssun.png)\"}" \
"}" % (value_max, settings.STATIC_URL)
在home.html内:
var series = {{ series3|safe }};
var obj = series[0]['data'];
for(var x in obj){
if(typeof obj[x] === "string") obj[x] = JSON.parse(obj[x]);
}
series[0]['data'] = obj;
$({{ chartID3|safe }}).highcharts({
chart: {{ chart3|safe }},
title: {{ title3|safe }},
xAxis: {{ xAxis3|safe }},
yAxis: {{ yAxis3|safe }},
series: series
});
现在它完美无缺!