QML QtCharts CandlestickSeries Repeater

Viewed 164

I want to populate a CandlestickSeries with data from a custom class.

I thought I would use a Repeater as usual, but it doesn't seem to work:

ChartView {
    title: "Candlestick Series"
    width: 400
    height: 300

    CandlestickSeries {
        name: "Acme Ltd."
        increasingColor: "green"
        decreasingColor: "red"

        /*
        CandlestickSet { timestamp: 1435708800000; open: 690; high: 694; low: 599; close: 660 }
        CandlestickSet { timestamp: 1435795200000; open: 669; high: 669; low: 669; close: 669 }
        CandlestickSet { timestamp: 1436140800000; open: 485; high: 623; low: 485; close: 600 }
        CandlestickSet { timestamp: 1436227200000; open: 589; high: 615; low: 377; close: 569 }
        CandlestickSet { timestamp: 1436313600000; open: 464; high: 464; low: 254; close: 254 }
        */

        Repeater {
            model: 100
            delegate: CandlestickSet {
                timestamp: 1000 * 60 * index + 1436313600000
                open: 400; high: 500; low: 300; close: 380
            }
        }
    }
}

The commented out part (taken straight from the documentation) works fine.

The Repeater-based code produces no data points.

How to dynamically fill a CandlestickSeries?

Note: I also tried an alternative approach using JS, and that failed too.

2 Answers

Instantiator may work, but I didn't test the following code.

Instantiator {
    model: 100
    delegate: CandlestickSet {
        timestamp: 1000 * 60 * index + 1436313600000
        open: 400; high: 500; low: 300; close: 380
    }
    onObjectAdded: series.insert(index, object)
    onObjectRemoved: series.remove(object)
}

ChartView {
    title: "Candlestick Series"
    width: 400
    height: 300

    CandlestickSeries {
        id: series
        name: "Acme Ltd."
        increasingColor: "green"
        decreasingColor: "red"
    }
}

Repeater doesn't work, because CandlestickSet is not a Component. CandlestickSeries has an append method. And createQmlObject can be used to create qml objects to dynamically.

ChartView {
    title: "Candlestick Series"
    width: 400
    height: 300

    CandlestickSeries {
        name: "Acme Ltd."
        increasingColor: "green"
        decreasingColor: "red"

        property int setsModel: 100

        onSetsModelChanged: {
            clear();
            for (var index = 0; index < setsModel; ++index)
                append(Qt.createQmlObject(
                    "import QtQuick 2.0; import QtCharts 2.12; " + 
                    "CandlestickSet { timestamp: " + (1000 * 60 * index + 1436313600000) + "; " 
                    "open: 400; high: 500; low: 300; close: 380}", 
                    null));
        }
    }
}

Note: Online Qml Compilers does not support QtCharts, so I didn't test the code.

Related