QML QtChart 中 DateTimeAxis使用

31 篇文章 11 订阅
6 篇文章 1 订阅

QtChart曲线设置时间轴需要用到QDateTimeAxis。在这里主要介绍QML中的使用。

QML中需要用到DateTimeAxis,同时介绍使用QML中的Date对象。

看一下效果


具体代码如下:

import QtQuick 2.9
import QtCharts 2.2
 
 
ChartView{
 
    Rectangle{
        width: 20; height: 20
        color: "red"
        MouseArea
        {
            anchors.fill: parent
            onClicked: {
                console.log(startdate)
                var curd = new Date();
                console.log(curd.toString());
 
                curd.setHours(curd.getHours()+6);
                console.log(curd.toString());
 
                var maxtimestr = curd.toLocaleString(Qt.locale(),"yyyy-MM-dd hh:mm:ss");
                topline.append(x_axis.min.getTime(),80);
 
                for (var i = 0; i < 100; ++i)
                {
                    line.append(1525837369967+i*1000*500,Math.random()*100);
                }
 
                x_axis.max =  Date.fromLocaleString(Qt.locale(), maxtimestr, "yyyy-MM-dd hh:mm:ss")
                topline.append(x_axis.max.getTime(),80);
 
            }
        }
    }
 
    id : view
    anchors.fill: parent
    antialiasing: true
    legend.visible : false
    property real lablefont: 15
    property date curdate: new Date()
    property var startdate: "2018-05-09 12:00:00"
    //property string enddate:
 
 
    DateTimeAxis {
        id : x_axis
        min : Date.fromLocaleString(Qt.locale(), view.startdate, "yyyy-MM-dd hh:mm:ss")
        max : Date.fromLocaleString(Qt.locale(), "2018-05-09 20:00:00", "yyyy-MM-dd hh:mm:ss")
        format: "hh::mm" //设置显示样式
        labelsFont.pointSize: view.lablefont
 
    }
    ValueAxis {
        id : y_axis
        min : 0;
        max : 100;
        labelFormat: "%i" //设置标签的显示精度 样式
        labelsFont.pointSize: view.lablefont
    }
 
    AreaSeries
    {
        axisY: y_axis;
        axisX: x_axis
        color: "#99CA53"
        upperSeries : LineSeries
        {
            id : line;
            useOpenGL: true
        }
    }
    LineSeries {
        id : topline;
        axisX: x_axis
        axisY: y_axis;
        color: "red"
    }
 
    //加载完成时设置x的坐标轴
    Component.onCompleted:
    {
        var curd = new Date();
        var mintimestr = curd.toLocaleString(Qt.locale(),"yyyy-MM-dd hh:mm:ss");
        curd.setHours(curd.getHours()+6);
        var maxtimestr = curd.toLocaleString(Qt.locale(),"yyyy-MM-dd hh:mm:ss");
        //x_axis.min =  Date.fromLocaleString(Qt.locale(), mintimestr, "yyyy-MM-dd hh:mm:ss")
        //x_axis.max =  Date.fromLocaleString(Qt.locale(), maxtimestr, "yyyy-MM-dd hh:mm:ss")
 
    }            
 
}

注意:添加数据时,x轴的值需要是 到毫秒。使用QDateTime::toMSecsSinceEpoch(),而不要QDateTime::toTime_t ()。 





  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值