Reputation: 683
I modify code from here
I try to use datetime.timestamp()
as x-point , but it doesn't work.
My totally hour is 5 hours and tickcount is 10, I expect every half hour to cut the time , but it is not.
how to add candlestick at specific time(maybe 8:45) to this qchart ?
import random, datetime
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCharts import QtCharts
from PySide2.QtCore import QPointF, Qt
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.plot = QtCharts.QChart()
self.chart_view = QtCharts.QChartView(self.plot)
self.setCentralWidget(self.chart_view)
self.series = QtCharts.QLineSeries()
self.candle_series = QtCharts.QCandlestickSeries()
self.series.setName("Magnitude")
self.plot.addSeries(self.series)
self.plot.addSeries(self.candle_series)
# self.plot.createDefaultAxes()
# self.plot.legend().hide()
self.candle_series.setDecreasingColor(Qt.green)
self.candle_series.setIncreasingColor(Qt.red)
# Setting X-axis
self.axis_x = QtCharts.QDateTimeAxis()
self.axis_x.setTickCount(11)
self.axis_x.setLabelsAngle(70)
self.axis_x.setFormat("h:mm")
self.axis_x.setTitleText("Date")
self.axis_x.setMax(datetime.datetime.strptime('202005071345','%Y%m%d%H%M'))
self.axis_x.setMin(datetime.datetime.strptime('202005070845','%Y%m%d%H%M'))
# Setting Y-axis
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setTickCount(7)
self.axis_y.setLabelFormat("%i")
self.axis_y.setTitleText("Temperature [celcious]")
self.axis_y.setMax(60)
self.axis_y.setMin(10)
self.plot.setAxisX(self.axis_x, self.series)
self.plot.setAxisY(self.axis_y, self.series)
self.plot.setAxisX(self.axis_x,self.candle_series)
self.plot.setAxisY(self.axis_y,self.candle_series)
self.candle_series.append(QtCharts.QCandlestickSet(30,50,20,25))
self.series.append(float(QtCore.QDateTime.fromString("202005070849", "yyyyMMddhhmm").toMSecsSinceEpoch()),22,)
self.series.append(float(QtCore.QDateTime.fromString("202005070950", "yyyyMMddhhmm").toMSecsSinceEpoch()),20,)
self.series.append(float(QtCore.QDateTime.fromString("202005071051", "yyyyMMddhhmm").toMSecsSinceEpoch()),58,)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 2152
Reputation: 243907
According to the docs:
datetime.timestamp()
Return POSIX timestamp corresponding to the datetime instance. The return value is a float similar to that returned by time.time().Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion. Since datetime supports wider range of values than mktime() on many platforms, this method may raise OverflowError for times far in the past or far in the future.
For aware datetime instances, the return value is computed as:
(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
As you can see from the way it calculates the time difference in seconds but according to the docs that time is needed in milliseconds:
[...]
QDateTimeAxis can be used with any QXYSeries. To add a data point to the series,QDateTime::toMSecsSinceEpoch()
is used:
[...]
Considering the above there are 2 possible solutions:
Multiply timestamp()
by 1000 to get the number of milliseconds:
self.series.append(
datetime.datetime.strptime("202005070849", "%Y%m%d%H%M").timestamp() * 1000,
22,
)
self.series.append(
datetime.datetime.strptime("202005070950", "%Y%m%d%H%M").timestamp() * 1000,
20,
)
self.series.append(
datetime.datetime.strptime("202005071051", "%Y%m%d%H%M").timestamp() * 1000,
58,
)
Use QDateTime:
self.series.append(
QtCore.QDateTime.fromString(
"202005070849", "yyyyMMddhhmm"
).toMSecsSinceEpoch(),
22,
)
self.series.append(
QtCore.QDateTime.fromString(
"202005070950", "yyyyMMddhhmm"
).toMSecsSinceEpoch(),
20,
)
self.series.append(
QtCore.QDateTime.fromString(
"202005071051", "yyyyMMddhhmm"
).toMSecsSinceEpoch(),
58,
)
The tickCount indicates the total number of ticks, and the spacing of the ticks meets the following condition:
dt = (dmax - dmin) / (tickCount -1)
It subtracts "1" from tickCount because it also considers the extremes
self.axis_x.setTickCount(11)
Upvotes: 2