Django网页未响应的NVD3图表

2024-04-25 22:03:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的django项目中,我有以下观点:

def ChartView(request):

connection = SQLSeverConnection('MSSQLServerDataSource')

times = connection.getColumnData('DateTimeStamp', 'reqColumn', '2011-01-01 00:00:00.000', '2011-01-21 19:30:00.000')
string_times = []
for theTime in times:
    string_times.append(theTime.DateTimeStamp)

start = string_times[0]
end = string_times[-1]

start_time = int(time.mktime(start.timetuple()))
end_time = int(time.mktime(end.timetuple()))

xdata = range(start_time, end_time, 1)
xdata = map(lambda x: start_time + x * 1000000000, xdata)

ydata = connection.getColumnData('AIKE0G_1_MVAr', 'DunbarGen', '2011-01-01 00:00:00.000', '2011-01-21 19:30:00.000')

tooltip_date = "%d %b %Y %H:%M:%S %p"
extra_serie = {"tooltip": {"y_start": "Value was ", "y_end": " units"},
               "date_format": tooltip_date}

chartdata = {
    'x': xdata,
    'name1': 'series 1', 'y1': ydata, 'extra1': extra_serie,
}

charttype = "lineWithFocusChart"
chartcontainer = 'linewithfocuschart_container' # container name
data = {
    'charttype': charttype,
    'chartdata': chartdata,
    'chartcontainer': chartcontainer,
    'extra': {
        'x_is_date': True,
        'x_axis_format': '%d %b %Y %H',
        'tag_script_js': True,
        'jquery_on_ready': True,
    }
}

在pdb.set_跟踪()

^{pr2}$

没有错误(这将导致500页),但当转到“图表视图”时,该页永远不会加载。在

.getColumnData()返回的对象类型是pyodbc.row我已经成功地将其转换为int(我假设nvd3解释的是datetime)

有人能帮忙吗?这是使用nvd3的好方法吗?如果有更好的方法,那么我将非常感谢,如果你能提供一个例子。在

模板

^{3}$

谢谢!在


Tags: datestringtimeconnectionextrastartintend
1条回答
网友
1楼 · 发布于 2024-04-25 22:03:50

谢天谢地,我成功了。问题出在我的ydata身上。从数据库查询中,它的格式如下:

[(4.4, ), (4.8, ), (4.2, ), (4.0, ), (4.2, ), (4.8, ), (2.0, ), (2.4, ), (3.0, )]

nvd3应该是这样的

^{pr2}$

进行转换

ydata_badForm = connection.getColumnData('<column>', '<table>', '2011-01-01 00:00:00.000', '2011-01-21 19:30:00.000')

ydata = map(lambda x: x[0], ydata_badForm)

这将创建一个名为ydata的新列表,其中ydata的元组第一个元素被写入每个索引。在

从这里开始,ydata是正确的列表格式(不是元组列表),刷新时得到了一些漂亮的图表!在

相关问题 更多 >