13--Qt的chartView的x和y轴触发

标签(空格分隔): Qt


使用chartView实现触发x轴y轴显示相应的坐标信息

demo

import QtQuick 2.9
import QtCharts 2.2

Rectangle {
    width: 1000;
    height: 400;
    color: "skyblue"
    property var xdata: [1,2,3,4,5,6,7,8,9,10,11,12];
    property var ydata: [300,220,115,445,112,446,100,341,220,118,148, 200];
    property real oneMouseH: 0;
    property real oneMouseW: 0;
    property real xtip: 0; //浮动显示的x,y提示值
    property real ytip: 0; //浮动显示的x,y提示值
    property var realTimeArray: new Array;
    property bool bShowTip: false;
    property int tipRectW: 150;
    property int tipRectH: 36;
    property int tipRectLeftMargin: 5;
    property int tipRectTopMargin: 5;

    function updateChart()
    {
        axisY.max = Math.max.apply(null, ydata);
        axisY.min = Math.min.apply(null, ydata);
        for (var i = 0; i < 11; i++)
        {
            line.append(xdata[i],ydata[i]);
        }
    }

    //
    function getPositionValue(pointX, pointY)
    {
        var tempx, tempy;
        if(xdata.length > 0)
        {
            tempx = xdata[0];
            tempy = ydata[0];

            ytip = ydata[0];
            xtip = ydata[0];//(realTimeArray[0] === undefined ? 0 : realTimeArray[0]);

            var i;
            for(i = 1; i < xdata.length; i++)
            {
                if(Math.abs(pointX - xdata[i]) < Math.abs(pointX - tempx))
                {   //var j = i-1;
                    tempx = xdata[i];
                    tempy = ydata[i];
                    //print("yOrdinate===========", yOrdinate, i, realTimeArray[i])
                }
            }
            //利用当前的值映射出坐标,然后画出两条线
            var mapPoint = achartview.mapToPosition(Qt.point(tempx, tempy), testArea);

            //获取提示窗口出现的位置
            getTipPosFun(mapPoint);

            oneMouseH = mapPoint.y;
            oneMouseW = mapPoint.x;
            xtip = tempx;
            ytip = tempy;
        }
    }

    function getTipPosFun(onePoint)
    {
        //最常见的时候都是处于1:左上
        tipRectLeftMargin = onePoint.x - tipRectW - 5;
        tipRectTopMargin = onePoint.y - tipRectH - 5;
        print("tipRectPosArray--------", onePoint, tipRectLeftMargin, tipRectTopMargin)
        if(tipRectLeftMargin < 20)  //右上的位置
        {
            tipRectLeftMargin = onePoint.x + 5
        }
        if(onePoint.y - tipRectH <  20) //右下或者左下
        {
            tipRectTopMargin = onePoint.y + 5;
        }

    }

    ChartView {
        id: achartview
        anchors.fill: parent;

        //plotAreaColor: "#3c3f45"
        legend.visible: false
        legend.alignment: Qt.AlignBottom
        antialiasing: true;
        dropShadowEnabled: true
        backgroundColor: "#444851"
        //backgroundRoundness: 20
        animationOptions: ChartView.SeriesAnimations
        //count: 2
        theme: ChartView.ChartThemeQt;//设置主题 也可以用backgroundColor;

//        DateTimeAxis {
//            id: axisX;
//            format: "HH:mm"; //"yyyy MMM dd";HH:mm mm:ss
//            tickCount: 12;
//            //            min : Date.fromLocaleString(Qt.locale(), "2018-05-09 8:00:00", "yyyy-MM-dd hh:mm:ss");
//            //            max : Date.fromLocaleString(Qt.locale(), "2018-09-17 20:00:00", "yyyy-MM-dd hh:mm:ss");
//            min: new Date(new Date() - 11*24*60*60*1000);
//            max: new Date();

//        }
        ValueAxis {
            id: axisX;
            min: xdata[0];
            max: xdata[xdata.length - 1];
            tickCount: 5;
            minorTickCount: 5;
            labelFormat: "%.0f";
            labelsColor: "#999999";//坐标轴上的字体颜色
            gridLineColor: "#3c3f45"
            color: "#3c3f45"
            minorGridLineColor: "#3c3f45"
        }

        ValueAxis {
            id: axisY;
            min: 100;
            max: 446;
            tickCount: ydata.length;
            labelFormat: "%.0f";
            labelsColor: "#999999";//坐标轴上的字体颜色
            gridLineColor: "#3c3f45"
            color: "#3c3f45"
        }
        AreaSeries {
            id:testArea;
            axisX: axisX;
            axisY: axisY;
            color: "#0ddeff";
            borderColor: "#1699ae";
            opacity: 0.3;
            borderWidth: 3;
            pointLabelsClipping: false
            useOpenGL: false
            upperSeries: LineSeries {
                id: line;
            }

            Component.onCompleted: {
                updateChart();
            }
        }

        MouseArea {
            id: chartArea;
            anchors.fill: parent;// parent;//
           // focus:true;
            hoverEnabled:true;
            Keys.enabled: true;
            Keys.onPressed: {
           }
            onEntered: {
                bShowTip = true;
            }
            onPositionChanged: {
                var xmin = achartview.mapToValue(Qt.point(mouseX, mouseY), testArea)
                print("xmin==========", xmin)
                getPositionValue(xmin.x, xmin.y)
                print("xxxxxxxxxxxxxx----onPositionChanged------", mouseX, mouseY)

            }
            onExited: {
                bShowTip = false;
            }
            Rectangle{
                id: oneline;
                anchors.top: parent.top;
                anchors.topMargin: oneMouseH;
                anchors.left: parent.left
                anchors.leftMargin: 30;
                anchors.right: parent.right
                anchors.rightMargin: 30;
                height: 1;
                color: "red";
                visible: bShowTip;
            }

            Rectangle{
                id: twoline;
                anchors.top: parent.top;
                anchors.topMargin: 30;
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 30;
                anchors.left: parent.left;
                anchors.leftMargin: oneMouseW
                width: 1
                color: "red";
                visible: bShowTip;
            }

            Rectangle{
                id: tipRect;
                anchors.left: parent.left;
                anchors.leftMargin: tipRectLeftMargin;
                anchors.top: parent.top;
                anchors.topMargin: tipRectTopMargin;
                width: tipRectW;
                height: tipRectH;
                color: "#1690f5"
                border.color: "#d5d5d5"
                visible: bShowTip;
                //oneLeft + 3 , oneTop + 15
                Text{
                    id: ytext;
                    anchors.left: parent.left;
                    anchors.leftMargin: 3;
                    anchors.top: parent.top;
                    anchors.topMargin: 2;
                    text: "y: " + ytip
                    color: "#d5d5d5"
                    font.family: "微软雅黑";
                    font.pixelSize: 13;
                }
                Text{
                    id: xtext;
                    anchors.left: parent.left;
                    anchors.leftMargin: 3;
                    anchors.top: parent.top;
                    anchors.topMargin: 17;
                    text: "x: " + xtip;
                    color: "#d5d5d5"
                    font.family: "微软雅黑";
                    font.pixelSize: 13;
                }
            }
        }
    }
}




x和y轴触发
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,710评论 4 376
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,839评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,295评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,776评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,198评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,074评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,200评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,986评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,733评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,877评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,348评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,675评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,393评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,209评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,212评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,003评论 2 280

推荐阅读更多精彩内容

  • e之助, 青辉阳科技旗下企业工作平台 - 让工作更高效, 流程更清晰, 管理更简单 高科技行业的竞争就是人才的竞争...
    青辉阳阅读 267评论 0 0
  • 地铁上人生百态,各自观察,你在地铁上遇到哪些有趣或无趣的事? 1.旁边的女生在研究欧诗漫卖点分析,想想在画廊销售时...
    芫荽菜阅读 204评论 0 0