【发布时间】:2019-03-26 07:30:01
【问题描述】:
我正在尝试使用 Highcharts stockChart 在我的网络应用程序上显示公司的历史股价图表。正在从 CSV 文件加载带有股票价格数据的时间戳。我面临的问题是日期时间转换。在 CSV 文件中,每天只有 5 年的字符串形式的日期。这个字符串,我正在使用 strptime() 转换为 datetime 对象,并将其转换为时间戳,以作为参数发送到 javascript 中的 stockChart。但问题在于 - CSV 文件的每日日期为 2014-2019,但在图表中,转换后,它仅显示 1970 年的两天。
我认为这可能是与时区有关的某种转换问题。
Python 后端代码(Django views.py 函数)
csvFile = company + ".csv"
file = open(csvFile)
reader = csv.reader(file)
data = list(reader)
prices = []
for row in data:
temp = []
temp.append(datetime.timestamp(datetime.strptime((row[0]) + " 09:30:00 +0000", '%Y-%m-%d %H:%M:%S %z')))
temp.append(float(row[1]))
prices.append(temp)
arg = {'symbol':company, 'prices':prices}
return render(request, 'history.html', arg)
JavaScript 代码
<script type="text/javascript">
// Create the chart
Highcharts.stockChart('container', {
time: {
useUTC: false
},
rangeSelector: {
buttons: [{
count: 7,
type: 'day',
text: '1W'
}, {
count: 1,
type: 'month',
text: '1M'
}, {
count: 6,
type: 'month',
text: '6M'
}, {
count: 1,
type: 'year',
text: '1Y'
}, {
count: 2,
type: 'year',
text: '2Y'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: true,
selected: 1
},
title: {
text: 'Historical Stock prices'
},
exporting: {
enabled: true
},
series: [{
name: "{{ symbol }}",
data: {{ prices }},
tooltip: {
valueDecimals: 2
}
}]
});
</script>
CSV 文件的日期为 2014-2019 [
但在图表中,仅显示了 1970 年的两天。 [
我猜这是将日期时间转换为时间戳的问题。有人可以帮我吗?
【问题讨论】:
标签: python datetime highcharts timestamp