Qt图表(QML)日期时间轴缺少二月份

问题描述 投票:4回答:3

我创建了一个QML图表

ChartView {
    id: chart
    anchors.fill: parent
    antialiasing: true
    ValueAxis {
        id: axisY
        tickCount: 3
    }
    DateTimeAxis {
        id: xTime
    }
    SplineSeries {
        id: chartseries
        pointsVisible: true

        pointLabelsVisible: false
        useOpenGL: true
        axisX: xTime
        axisY: axisY
    }
}

我也在每个月初添加到图表中。刻度点上的工具提示是正确的。在X轴上,Qt本身也是如此。如何手动调整

 Xaxis->setTickCount(commonmap.size());
QMap<qint64,double>::const_iterator i = commonmap.constBegin();
while (i != commonmap.constEnd())
{
  splineseries->append(i.key(),i.value());
  ++i;
}

enter image description here

c++ qt charts qml qchartview
3个回答
2
投票

我看到你正在将DateTimeAxis的tickCount设置为系列中的样本数,所以Qt所做的是除以减去时间的最大值和最小值并除以它们,计算出来大约31天,我们可以看到它将DateTimeAxis的形式修改为“MMM yyyy dd hh:mm:ss”:

enter image description here

所以显示的日期不是月份,而是等间隔的时间,而2月只有28天不会显示该日期。

如果要显示二月,则必须在tickCount上放置一个更大的值,但它也会生成更多日期。不幸的是,目前不可能以不规则的方式放置蜱,例如月份的天数。


1
投票

我找到了这个问题的答案(可能不是正确的方法......但可以解决)步骤实现解决方案 -

  1. 查找上个月的最后日期(例如6月30日)
  2. setMax QDateTimeAxis的日期。

它起作用因为Qt将开始日期和结束日期之间的天数平均分配,并根据滴答计数在X轴上分配。

使用问题中的上述值将获得图像中显示的图表

enter image description here

[

Xaxis->setMin(QDateTime::fromMSecsSinceEpoch(commonmap.firstKey()));
Xaxis->setMax(getLastDate());

getLastDate()
{
   QDate firstdate = 
   QDateTime::fromMSecsSinceEpoch(commonmap.lastKey()).date();
   int month =  firstdate.month();
   int day = firstdate.day();
   int addday = 0;
   switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
       addday = 31- day;
      break;
    case 4:
    case 6:
    case 9:
    case 11:
       addday = 30- day;
     break;
    case 2:
        if(firstdate.isLeapYear(firstdate.year()))
        {
            addday = 29- day;
        }
        else
       {
          addday = 28- day;
       }
       break;

   }

return (QDateTime::fromMSecsSinceEpoch(commonmap.lastKey()).addDays(addday));
 }

0
投票

正如@eyllanesc在他的回答中所解释的那样,不可能使用DateTimeAxis来获得你想要显示的内容。

这里有一些代码可以帮助生成使用CategoryAxis的东西。

注意:在我的示例中,数据是使用QML创建和格式化的,但您可以在数据实际可用的C ++部分中执行此操作。

import QtQuick 2.9
import QtCharts 2.2

Item {
    id: root
    width: 1280
    height: 720

    ListModel {
        id: data

        function toMsecsSinceEpoch(date) {
            var msecs = date.getTime();
            return msecs;
        }

        Component.onCompleted: {
            var minDate = data.get(0).x;
            var maxDate = data.get(0).x;
            var minValue = data.get(0).y;
            var maxValue = data.get(0).y;

            // Find the minimum and maximum values.
            // Fill the SplineSeries data.
            for (var i = 0 ; i < data.count ; i++) {
                if (data.get(i).x < minDate)
                    minDate = data.get(i).x;
                if (data.get(i).x > maxDate)
                    maxDate = data.get(i).x;
                if (data.get(i).y < minValue)
                    minValue = data.get(i).y;
                if (data.get(i).y > maxValue)
                    maxValue = data.get(i).y;
                chartseries.append(data.get(i).x, data.get(i).y);
            }

            // Fill the axis limits (x-axis and y-axis).
            axisY.min = Math.floor(minValue)
            axisY.max = Math.ceil(maxValue)
            xTime.min = minDate;
            xTime.max = maxDate;

            // Fill the CategoryAxis (x-axis).
            var dateReference = new Date(xTime.min);
            while (dateReference < xTime.max) {
                xTime.append(Qt.formatDate(dateReference, "MMM yyyy"), toMsecsSinceEpoch(new Date(dateReference.getFullYear(), dateReference.getMonth(), 1)));
                dateReference.setMonth(dateReference.getMonth() + 1);
            }
            xTime.append(Qt.formatDate(dateReference, "MMM yyyy"), toMsecsSinceEpoch(new Date(dateReference.getFullYear(), dateReference.getMonth(), 1)));
        }

        ListElement { x: 1514770200000; y: 40.311 }
        ListElement { x: 1517434200000; y: 40.4664 }
        ListElement { x: 1519853400000; y: 39.6276 }
        ListElement { x: 1522531803000; y: 39.6238 }
        ListElement { x: 1525123806000; y: 40.3 }
        ListElement { x: 1527802210000; y: 40.5638 }
    }

    ChartView {
        id: chart
        anchors.fill: parent
        antialiasing: true

        ValueAxis {
            id: axisY
            tickCount: 3
            min: 39
            max: 41
        }

        CategoryAxis {
            id: xTime
            labelsPosition: CategoryAxis.AxisLabelsPositionOnValue
        }

        SplineSeries {
            id: chartseries
            pointsVisible: true
            pointLabelsVisible: false
            useOpenGL: true
            axisX: xTime
            axisY: axisY
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.