Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDateTimeAxis not rendering labels when manually setting range



  • Hello,

    I'm trying QCharts for the first time using version 5.7 that I compiled on a CentOS 7 machine. There are several time series that contain different time periods that should share both the X and Y axes. I tried to attach each axis to all the series, but that just had the plot adjust to one series instead of zoom fitting to show them all. This was a problem for both the X and Y axes. So then I tried to set the range for each axis, but the QDateTimeAxis then doesn't render. The Y axis renders fine, though.

    Here is some example code:

    void Plot::beginPlotting() {
        chart()->removeAllSeries();
    
        if (axisX != nullptr) {
            chart()->removeAxis(axisX);
            axisX->deleteLater();
            axisX = nullptr;
        }
    
        if (axisY != nullptr) {
            chart()->removeAxis(axisY);
            axisY->deleteLater();
            axisY = nullptr;
        }
    
        series    = nullptr;
        boundsSet = false;
    }
    
    void Plot::beginSeries() {
        if (series != nullptr) {
            chart()->addSeries(series);
        }
    
        series = new QLineSeries;
    
        QPen pen(QRgb(0x00B2EE));
        series->setPen(pen);
    }
    
    void Plot::addPoint(qint64 time, double value) {
        if (series != nullptr) {
            time *= 1000;
            series->append(time, value);
    
            if (!boundsSet) {
                minVal    = value;
                maxVal    = value;
                minTime   = time;
                maxTime   = time;
                boundsSet = true;
            }
            else {
                if (minTime > time) { minTime = time; }
                if (maxTime < time) { maxTime = time; }
                if (minVal > value) { minVal = value; }
                if (maxVal < value) { maxVal = value; }
            }
        }
    }
    
    void Plot::endPlotting() {
        if (series != nullptr) {
            chart()->addSeries(series);
            series = nullptr;
        }
    
        axisX = new QDateTimeAxis;
        axisX->setTickCount(10);
        axisX->setFormat("hh:mm:ss");
        axisX->setTitleText("Date");
    
        chart()->addAxis(axisX, Qt::AlignBottom);
    
        axisY = new QValueAxis;
        axisY->setLabelFormat("%3.1lf");
        axisY->setTitleText("Value");
    
        chart()->addAxis(axisY, Qt::AlignLeft);
    
        /*
        foreach (QAbstractSeries *series, chart()->series()) {
            series->attachAxis(axisX);
            series->attachAxis(axisY);
        }
        */
    
        if (boundsSet) {
            QDateTime min, max;
    
            min.setMSecsSinceEpoch(minTime);
            max.setMSecsSinceEpoch(minTime);
            axisX->setRange(min, max);
            axisY->setRange(minVal, maxVal);
        }
    }
    


  • Found the error. Used minTime to set both min and max. Sorry to bother you with my stupid mistake.


Log in to reply