gpt4 book ai didi

Jquery从django对象构造数组

转载 作者:行者123 更新时间:2023-12-01 08:11:07 24 4
gpt4 key购买 nike

我的表中有 2000 万行。我正在尝试使用 http://www.jqplot.com/tests/date-axes.php 绘制时间戳图。 (日期与时间)jquery 将如何处理如此多的数据来绘制它。两个问题:

1.如何从下面的代码构造一个数组:

line1=[['2008-09-30',4], ['2008-10-30',6], ['2008-11-30',5], ['2008-12-30',9], ['2009-01-30',8]];`

2.在这种情况下我该如何改进代码:

def get_data(request):
cdr = Cdr.objects.values_list('start_of_call',flat=True)
context_instance=RequestContext(request, {}))
return render_to_response('cdr/get_data.html', context_instance=RequestContext(request, {'cdr':cdr}))

模板:

<script class="code" type="text/javascript">
$(document).ready(function(){

{% for c in cdr %}
{{ c|date:"d M Y"}}
{% endfor %}
How to construct the below array line from for loop or using jquery methods
//var line1=[['2008-09-30',4], ['2008-10-30',6], ['2008-11-30',5], ['2008-12-30',9], ['2009-01-30',8]];

var plot1 = $.jqplot('chart1', [line1], {
title:'Default Date Axis',
axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}},
series:[{lineWidth:4, markerOptions:{style:'square'}}]
});
});
</script>

编辑

  line1=[['2008-09-30',4], ['2008-10-30',6], ['2008-11-30',5], ['2008-12-30',9], ['2009-01-30',8]];`

上面第 4,6,5,9,8 行是小时

编辑2

CDR 值:

    [datetime.datetime(2012, 9, 13, 15, 54, 52, tzinfo=<UTC>), datetime.datetime(2012, 9, 22, 18, 24, 18, tzinfo=<UTC>), datetime.datetime(2012, 9, 6, 7, 42, 50, tzinfo=<UTC>), datetime.datetime(2012, 9, 7, 21, 27, 13, tzinfo=<UTC>), datetime.datetime(2012, 9, 3, 11, 57, 9, tzinfo=<UTC>), datetime.datetime(2012, 9, 1, 17, 23, 41, tzinfo=<UTC>), datetime.datetime(2012, 9, 15, 6, 14, 33, tzinfo=<UTC>), datetime.datetime(2012, 9, 4, 5, 36, 30, tzinfo=<UTC>), datetime.datetime(2012, 9, 17, 10, 45, 6, tzinfo=<UTC>), datetime.datetime(2012, 10, 1, 3, 1, 17, tzinfo=<UTC>), datetime.datetime(2012, 9, 27, 8, 36, 41, tzinfo=<UTC>), datetime.datetime(2012, 9, 30, 3, 12, 18, tzinfo=<UTC>), datetime.datetime(2012, 9, 18, 4, 8, 49, tzinfo=<UTC>), datetime.datetime(2012, 9, 2, 7, 21, tzinfo=<UTC>), datetime.datetime(2012, 9, 19, 16, 57, 34, tzinfo=<UTC>), datetime.datetime(2012, 9, 17, 17, 14, 49, tzinfo=<UTC>), datetime.datetime(2012, 9, 1, 22, 40, 33, tzinfo=<UTC>), datetime.datetime(2012, 9, 15, 1, 7, 39, tzinfo=<UTC>), datetime.datetime(2012, 9, 22, 17, 11, 45, tzinfo=<UTC>), datetime.datetime(2012, 9, 10, 3, 16, 21, tzinfo=<UTC>), '...(remaining elements truncated)...']

最佳答案

像这样:

from django.utils import simplejson

def get_data(request):
cdr = [(x.strftime('%Y-%M-%D'), x.hour) for x in \
Cdr.objects.values_list('start_of_call',flat=True)]
return render_to_response('cdr/get_data.html',
{'cdr': simplejson.dumps(cdr)},
context_instance=RequestContext(request))

strftime 中的格式字符串可能有点偏差 - 请在此处检查:http://docs.python.org/2.7/library/datetime.html#strftime-strptime-behavior

顺便说一句,您的 View 使用 RequestContext 做了一些奇怪的事情 - 上面是更常见的样式。

模板:

line1 = jQuery.parseJSON({{ cdr }})

至于样本大小的问题 - 没有明确的答案 - 这将取决于您的网络连接、浏览器和机器。 siad 20M 非常大 - 考虑对数据集进行采样,将其缩小到更易于管理的大小 - 实验将是最好的方法。

关于Jquery从django对象构造数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com