【问题标题】:QML QtCharts CandlestickSeries.append() returns falseQML QtCharts CandlestickSeries.append() 返回 false
【发布时间】:2021-03-10 05:35:21
【问题描述】:

尝试通过 JavaScript 填充 CandlestickSeries(数据来自自定义 QObject dataProvider):

Connections {
    target: dataProvider
    function onDataChanged() {
        for(var i = 0; i < dataProvider.data.length; i++) {
            var x = dataProvider.data[i]
            var jsobj = {timestamp: x.timestamp, open: x.open, high: x.high, low: x.low, close: x.close}
            if(!serie1.append(jsobj))
                console.log('append failed:', JSON.stringify(jsobj))
        }
    }
}

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

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

对每个数据点的.append() 调用均失败:

qml: append failed: {"timestamp":1514764800,"open":11993.6,"high":11995.2,"low":11676,"close":11807.6}
qml: append failed: {"timestamp":1514768400,"open":11807.5,"high":11908.4,"low":11501,"close":11561.1}
qml: append failed: {"timestamp":1514772000,"open":11561.1,"high":11700,"low":11423.5,"close":11648.6}
qml: append failed: {"timestamp":1514775600,"open":11620.6,"high":11843.4,"low":11519.3,"close":11567.5}
qml: append failed: {"timestamp":1514779200,"open":11596.5,"high":11809.7,"low":11505.9,"close":11790.7}
qml: append failed: {"timestamp":1514782800,"open":11790.6,"high":11809.9,"low":11699.5,"close":11699.6}
qml: append failed: {"timestamp":1514786400,"open":11699.1,"high":11890.3,"low":11639.6,"close":11886.6}
qml: append failed: {"timestamp":1514790000,"open":11867.4,"high":11949.9,"low":11770.2,"close":11830.3}
qml: append failed: {"timestamp":1514793600,"open":11807.5,"high":11822.5,"low":11461.9,"close":11559.6}
qml: append failed: {"timestamp":1514797200,"open":11582.6,"high":11807.2,"low":11543.5,"close":11743.6}
qml: append failed: {"timestamp":1514800800,"open":11710.8,"high":11777,"low":11582.2,"close":11592.9}
qml: append failed: {"timestamp":1514804400,"open":11592.9,"high":11598,"low":11250,"close":11400.1}
qml: append failed: {"timestamp":1514808000,"open":11412.1,"high":11440,"low":11376.2,"close":11414.7}
qml: append failed: {"timestamp":1514811600,"open":11413.6,"high":11434.4,"low":11180.3,"close":11217.5}
qml: append failed: {"timestamp":1514815200,"open":11210.1,"high":11457.4,"low":11090,"close":11410}
qml: append failed: {"timestamp":1514818800,"open":11457.2,"high":11470,"low":11300.1,"close":11437.9}
qml: append failed: {"timestamp":1514822400,"open":11426.5,"high":11426.6,"low":11257.5,"close":11310.8}
qml: append failed: {"timestamp":1514826000,"open":11310.5,"high":11439.9,"low":11300,"close":11378.1}
qml: append failed: {"timestamp":1514829600,"open":11378,"high":11470,"low":11300,"close":11391.6}
qml: append failed: {"timestamp":1514833200,"open":11391.6,"high":11469.9,"low":11335,"close":11455.8}
qml: append failed: {"timestamp":1514836800,"open":11455.8,"high":11639,"low":11360,"close":11434.1}
qml: append failed: {"timestamp":1514840400,"open":11455,"high":11588,"low":11434,"close":11498.9}
qml: append failed: {"timestamp":1514844000,"open":11495.6,"high":11508,"low":11346.2,"close":11448.3}
qml: append failed: {"timestamp":1514847600,"open":11448.1,"high":11460,"low":11300,"close":11359}

如何动态填充CandlestickSeries

注意:我也试过an alternative approach using Repeater,但也失败了。

【问题讨论】:

    标签: qt qml qtcharts


    【解决方案1】:

    尝试为我要显示的数据生成整个 QML 代码,然后使用 Qt.createQmlObject() 动态创建它(是的,我知道,糟糕的解决方案):

    function onDataChanged() {
        var qml = 'import QtQuick 2.15\nimport QtCharts 2.15\n\nCandlestickSeries {\n    name: "HelloWorld Ltd."\n    increasingColor: "green"\n    decreasingColor: "red"\n\n'
        for(var i = 0; i < dataProvider.data.length; i++) {
            var x = dataProvider.data[i]
            qml += '    CandlestickSet { timestamp: ' + (x.timestamp * 1000) + '; open: ' + x.open + '; high: ' + x.high + '; low: ' + x.low + '; close: ' + x.close + ' }\n'
        }
        qml += '}\n'
        var series = Qt.createQmlObject(qml, chart, 'dyn')
        if(series == null) {
            console.log("Error creating series")
            return
        }
    }
    

    它不起作用(没有错误)。

    但如果我打印生成的 QML 代码,然后修改我的静态 QML 代码以添加生成的 QML,它就可以工作。

    真的很奇怪

    【讨论】:

      【解决方案2】:

      您可能不再需要这个,但我找到了解决方案。您需要做的是使用 Qt.createQmlObject 创建 CandlestickSet 的整个对象,将其附加到 CandlstickSeries 并创建和分配轴。以下代码将起作用:

          var newObject = Qt.createQmlObject(
                      "import QtQuick 2.0; " +
                      "import QtCharts 2.2; " +
                      "CandlestickSet {" + 
                          "timestamp: " + timestamp +
                          "; open: " + open +
                          "; high: " + high +
                          "; low: " + low +
                          "; close: " + close +
                      "}", candlestickSeries);
          qmlObjects.push(newObject); //Array holding created objects to destroy them when not needed anymore to prevent a memory leak
          candlestickSeries.append(newObject);
          candlestickSeries.axisX = Qt.createQmlObject(
              "import QtQuick 2.0; " +
              "import QtCharts 2.2; " +
              "DateTimeAxis {" + 
                  "min: new Date(" + x_min + ")" +
                  "; max: new Date(" + x_max + ")" +
              "}", candlestickSeries);
          candlestickSeries.axisY = Qt.createQmlObject(
              "import QtQuick 2.0; " +
              "import QtCharts 2.2; " +
              "ValueAxis {" + 
                  "min: " + y_min +
                  "; max: " + y_max +
              "}", candlestickSeries);
      

      【讨论】:

        猜你喜欢
        • 2021-05-22
        • 1970-01-01
        • 2021-12-26
        • 1970-01-01
        • 2019-06-27
        • 1970-01-01
        • 1970-01-01
        • 2014-10-18
        • 2015-08-02
        相关资源
        最近更新 更多