Это ошибка, которая еще не решена: QTBUG-63358 .
Возможный обходной путь - удалить ряд из диаграммы, чтобы создать такое же поведение, как hide()
метод, в случае метода show()
необходимо добавить ряд в график:
import datetime
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
if __name__ == "__main__":
app = QtWidgets.QApplication([])
# data
t = [1557723420.0, 1573102560.0, 1577163360.0]
v = [258, 259, 253]
# candlestick series
candlestick = QtChart.QCandlestickSeries()
# line series
line = QtChart.QLineSeries()
# add data
for _t, _v in zip(t, v):
_open = _v + 10
_high = _v + 30
_low = _v - 20
_close = _v if _v % 2 else _high - 5
_time = QtCore.QDateTime(datetime.date.fromtimestamp(_t))
candlestick.append(
QtChart.QCandlestickSet(
_open, _high, _low, _close, _time.toMSecsSinceEpoch()
)
)
line.append(_time.toMSecsSinceEpoch(), _v)
# main window
window = QtWidgets.QMainWindow()
# axises
axis_x = QtChart.QDateTimeAxis()
axis_x.setFormat("yyyy-MM-dd hh:mm:ss")
axis_x.setTitleText("Date")
axis_x.setLabelsAngle(-90)
axis_y = QtChart.QValueAxis()
axis_y.setTitleText("Ratio")
# chart
chart = QtChart.QChart()
chart.addAxis(axis_x, QtCore.Qt.AlignBottom)
chart.addAxis(axis_y, QtCore.Qt.AlignRight)
chart.addSeries(candlestick)
chart.addSeries(line)
chart.setAxisX(axis_x, candlestick)
chart.setAxisY(axis_y, candlestick)
chart.setAxisX(axis_x, line)
chart.setAxisY(axis_y, line)
# chart view
chart_view = QtChart.QChartView()
chart_view.setChart(chart)
chart_view.setRenderHint(QtGui.QPainter.Antialiasing)
window.setCentralWidget(chart_view)
window.show()
state = True
def on_timeout():
global state
state = not state
line.setVisible(state)
serie = candlestick
if state:
# add == show
chart.addSeries(serie)
chart.setAxisX(axis_x, serie)
chart.setAxisY(axis_y, serie)
# workaround for repaint
chart.resize(chart.size() + QtCore.QSizeF(1, 1))
chart.resize(chart.size() - QtCore.QSizeF(1, 1))
else:
# remove == hide
chart.removeSeries(serie)
timer = QtCore.QTimer(interval=1000, timeout=on_timeout)
timer.start()
sys.exit(app.exec_())