Can't dynamically update CandlestickSeries in QtCharts
Unsolved
QML and Qt Quick
-
import QtQuick 2.15 import QtCharts 2.15 Rectangle{ width: 400; height: 400; radius: 40; property alias series: series; ChartView{ id: chartView; anchors.fill: parent; title: "Candlestick Chart"; legend.visible: false; antialiasing: true; CandlestickSeries{ id: series; increasingColor: "green"; decreasingColor: "red"; function updateData(){ series.clear(); console.log("updateData called"); for (var i = 0; i < stockProvider.stockData.length / 10; i++) { console.log(stockProvider.stockData[i].timestamp); var candlestick = Qt.createQmlObject( "import QtQuick 2.15; import QtCharts 2.15; CandlestickSet { " + "timestamp: " + stockProvider.stockData[i].timestamp + "; " + "open: " + stockProvider.stockData[i].open + "; " + "high: " + stockProvider.stockData[i].high + "; " + "low: " + stockProvider.stockData[i].low + "; " + "close: " + stockProvider.stockData[i].close + "; " + "}", series ); series.append(candlestick); console.log(stockProvider.stockData[i].open); } } } } }
This is how I tried to update the CandlestickSeries dynamically but it doesn't work. I had console.log the data for debug purposes and they are all present. For some reason the graph remains blank. Can someone help me please?
-
import QtQuick 2.15 import QtCharts 2.15 Rectangle{ width: 400; height: 400; radius: 40; ChartView{ id: chartView; anchors.fill: parent; title: "Candlestick Chart"; legend.visible: false; antialiasing: true; CandlestickSeries{ id: series; increasingColor: "green"; decreasingColor: "red"; VCandlestickModelMapper{ id: modelMapper; model: candleStickModel; timestampRow: 1; openRow: 2; highRow: 3; lowRow: 4; closeRow: 5; onModelChanged: { console.log("Candlestick series data model updated"); } } } } }
QVariant CandleStickModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= _candleStickData.size()) { return QVariant(); } const class ::CandleStickData *candle{_candleStickData.at(index.row())}; switch (CandleStickRoles(role)) { case TimestampRole: return candle->timestamp(); case OpenRole: return candle->open(); case CloseRole: return candle->close(); case HighRole: return candle->high(); case LowRole: return candle->low(); default: return QVariant(); } return QVariant(); }
I tried using a VCandlestickModelMapper but it still doesn't work. Where did I go wrong please?