Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QChart -problem with time axis X
Qt 6.11 is out! See what's new in the release blog

QChart -problem with time axis X

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 572 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,

    I did a class with my own QChart. I would like incoming messages to be displayed in a chart. I would like the X axis to show the time when value was added. I also made the chart in such a way that it was dynamic. Unfortunately , when it is displayed, the X axis is not shown to me and I do not know what it may be coused by. I used timer as a messaging simulator.

    My .cpp file

    #include "chart.h"
    #include <QtCharts/QAbstractAxis>
    #include <QtCharts/QSplineSeries>
    #include <QtCharts/QValueAxis>
    #include <QtCore/QRandomGenerator>
    #include <QtCore/QDebug>
    
    Chart::Chart(const QColor color, const qreal minYRange, const qreal maxYRange, const QString &units, QGraphicsItem *parent, Qt::WindowFlags wFlags):
        QChart(QChart::ChartTypeCartesian, parent, wFlags),
        series(nullptr),
        axisX(new QDateTimeAxis()),
        axisY(new QValueAxis()),
        m_step(0),
        m_x(9),
        m_y(1)
    {
        QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
        m_timer.setInterval(1000);
    
        visualConfig();
    
        series = new QSplineSeries(this);
        QPen green(color);
        green.setWidth(3);
        series->setPen(green);
    
        series->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), m_y);
    
        addSeries(series);
    
        addAxis(axisX,Qt::AlignBottom);
        addAxis(axisY,Qt::AlignLeft);
        series->attachAxis(axisX);
        series->attachAxis(axisY);
        axisX->setTickCount(12);
        axisX->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime());
        axisX->setFormat("h mm");
    
    
        axisY->setRange(minYRange, maxYRange);
        axisY->setTickCount(5);
        axisY->setTitleText(units);
    
        m_timer.start();
    }
    
    Chart::~Chart()
    {
    
    }
    
    void Chart::handleTimeout()
    {
        QPointF last_sample = series->at(series->count()-1);
        qreal last_time = last_sample.x();
    
        QDateTime actual_time = QDateTime::currentDateTime();
        m_y = QRandomGenerator::global()->bounded(10, 50);
        series->append(actual_time.toMSecsSinceEpoch(), m_y);
    
        qreal time_difference = actual_time.toMSecsSinceEpoch() - last_time;
        scroll(time_difference, 0);
    }
    
    void Chart::visualConfig()
    {
        setMargins(QMargins(0,0,0,0));
        legend()->hide();
        setAnimationOptions(QChart::AllAnimations);
    
        setBackgroundVisible(false);
        axisX->setGridLineVisible(false);
        axisY->setGridLineVisible(false);
    
        QFont labelsFont;
        labelsFont.setPixelSize(10);
        axisX->setLabelsFont(labelsFont);
        axisY->setLabelsFont(labelsFont);
    
        axisX->setLabelsBrush(Qt::white);
        axisY->setLabelsBrush(Qt::white);
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • C Creatorczyk

      Hi,

      I did a class with my own QChart. I would like incoming messages to be displayed in a chart. I would like the X axis to show the time when value was added. I also made the chart in such a way that it was dynamic. Unfortunately , when it is displayed, the X axis is not shown to me and I do not know what it may be coused by. I used timer as a messaging simulator.

      My .cpp file

      #include "chart.h"
      #include <QtCharts/QAbstractAxis>
      #include <QtCharts/QSplineSeries>
      #include <QtCharts/QValueAxis>
      #include <QtCore/QRandomGenerator>
      #include <QtCore/QDebug>
      
      Chart::Chart(const QColor color, const qreal minYRange, const qreal maxYRange, const QString &units, QGraphicsItem *parent, Qt::WindowFlags wFlags):
          QChart(QChart::ChartTypeCartesian, parent, wFlags),
          series(nullptr),
          axisX(new QDateTimeAxis()),
          axisY(new QValueAxis()),
          m_step(0),
          m_x(9),
          m_y(1)
      {
          QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
          m_timer.setInterval(1000);
      
          visualConfig();
      
          series = new QSplineSeries(this);
          QPen green(color);
          green.setWidth(3);
          series->setPen(green);
      
          series->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), m_y);
      
          addSeries(series);
      
          addAxis(axisX,Qt::AlignBottom);
          addAxis(axisY,Qt::AlignLeft);
          series->attachAxis(axisX);
          series->attachAxis(axisY);
          axisX->setTickCount(12);
          axisX->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime());
          axisX->setFormat("h mm");
      
      
          axisY->setRange(minYRange, maxYRange);
          axisY->setTickCount(5);
          axisY->setTitleText(units);
      
          m_timer.start();
      }
      
      Chart::~Chart()
      {
      
      }
      
      void Chart::handleTimeout()
      {
          QPointF last_sample = series->at(series->count()-1);
          qreal last_time = last_sample.x();
      
          QDateTime actual_time = QDateTime::currentDateTime();
          m_y = QRandomGenerator::global()->bounded(10, 50);
          series->append(actual_time.toMSecsSinceEpoch(), m_y);
      
          qreal time_difference = actual_time.toMSecsSinceEpoch() - last_time;
          scroll(time_difference, 0);
      }
      
      void Chart::visualConfig()
      {
          setMargins(QMargins(0,0,0,0));
          legend()->hide();
          setAnimationOptions(QChart::AllAnimations);
      
          setBackgroundVisible(false);
          axisX->setGridLineVisible(false);
          axisY->setGridLineVisible(false);
      
          QFont labelsFont;
          labelsFont.setPixelSize(10);
          axisX->setLabelsFont(labelsFont);
          axisY->setLabelsFont(labelsFont);
      
          axisX->setLabelsBrush(Qt::white);
          axisY->setLabelsBrush(Qt::white);
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Creatorczyk
      I don't know. Did you try removing lines of code till it did work? Does the Y axis show? Is axisX->setLabelsBrush(Qt::white); by any chance writing on a while background? In axisX->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime()); is it a good idea to set a range where the minimum & maximum are equal?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved