QT图表QML以毫秒为单位滚动X轴

时间:2017-11-24 11:59:07

标签: qt qml

我正在尝试在QT Charts QML中显示实时数据

我每隔25毫秒用一个Timer更新Y值 x轴应显示动态5秒窗口(当前时间+ - 2500毫秒)如何使X轴动态显示实时更新的5秒窗口?

到目前为止,我得到了正确的计时器,每25毫秒触发一次。

我修改了其中一个QT示例,到目前为止,实时更新仍在使用。

import QtQuick 2.0
import QtCharts 2.0

Item {
    anchors.fill: parent

    property double startTime: 0

    ChartView {
        title: startTime
        anchors.fill: parent
        legend.visible: false
        antialiasing: true

        ValueAxis {
            id: axisX
            min: 0
            max: 10
            tickCount: 5
        }

        ValueAxis {
            id: axisY1
            min: -0.5
            max: 1.5
        }
        ValueAxis {
            id: axisY2
            min: 0
            max: 1000
        }

        SplineSeries {
            id: series1
            axisX: axisX
            axisY: axisY1
        }

        SplineSeries {
            id: series2
            axisX: axisX
            axisY: axisY2
        }
    }

    // Add data dynamically to the series
    Timer {
        id: refreshTimer
        interval: 25
        running: true
        repeat: true
        onTriggered: {
            series1.append(2, Dashboard.gpsSpeed);
            series2.append(10, Dashboard.gpsAltitude);
            console.log ("Timer Triggered");
    }
 }

}

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你想让x轴与当前数据动态滚动。因此,您需要调整相应轴的最小和最大属性。

Timer{
    property int amountOfData: 0 //So we know when we need to start scrolling
    id: refreshTimer
    interval: 25
    running: true 
    repeat: true
    onTriggered: {
        series1.append(2, Dashboard.gpsSpeed);
        series2.append(10, Dashboard.gpsAltitude);

        if(amountOfData > axisX.max){
            axisX.min++;
            axisX.max++;
        }else{
            amountOfData++; //This else is just to stop incrementing the variable unnecessarily
        }
    }
}

答案 1 :(得分:1)

这是我的代码现在的样子,我也删除了用户不再可见的数据点。

// Add data dynamically to the series
Timer {
    property int amountOfData: 0 //So we know when we need to start
    property int timeline: 0 // Start of the timeline
    id: refreshTimer
    interval: 50
    running: false
    repeat: true
    onTriggered: {
        timeline++;
        series1.append(timeline, Dashboard.revs);
        series2.append(timeline, Dashboard.speed);

        if(amountOfData > axisX.max){
            axisX.min++;
            axisX.max++;
        }else{
            amountOfData++; //This else is just to stop incrementing the variable unnecessarily
        }
        //remove all data points that are not visible anymore
        if (timeline > 100){
            series1.remove(1);
            series2.remove(1);


        }
    }
}