[Interest] How to populate ChartView CandlestickSeries from JS?

Jeremy Katz jeremy.k.list at gmail.com
Sun Feb 21 10:33:27 CET 2021



On 19/Feb/21 07:30, Federico Ferri wrote:
> Has anybody a solution to this?
> 
> Tried two different approaches:
> 
> https://stackoverflow.com/questions/66279947/qml-qtcharts-candlestickseries-repeater
> <https://stackoverflow.com/questions/66279947/qml-qtcharts-candlestickseries-repeater>
> https://stackoverflow.com/questions/66280361/qml-qtcharts-candlestickseries-append-returns-false
> <https://stackoverflow.com/questions/66280361/qml-qtcharts-candlestickseries-append-returns-false>
> 
> Both failed.
> 

This works for me with 5.15.2.


import QtQuick 2.15
import QtQuick.Window 2.15
import QtCharts 2.15

Window {
    width: 640
    height: 480
    visible: true
    ChartView {
        id: view
        anchors.fill: parent
        CandlestickSeries {
            id: series
            CandlestickSet { timestamp: 1; open: 5; high: 10; low: 1;
close: 4 }
        }
    }

    Component {
        id: nextSet
        CandlestickSet {}
    }

    MouseArea {
        anchors.fill: parent
        Component.onCompleted: view.zoom(0.1)
        onClicked: {
            var last = series.at(series.count - 1);
            var when = last.timestamp + 1;
            var open = last.close;
            var low = open - Math.random() * 2;
            var high = open + Math.random() * 2;
            var close = (high + low) / 2;
            series.append(nextSet.createObject(null, {timestamp: when,
open: open, low: low, high: high, close: close}));
        }
    }
}


More information about the Interest mailing list