QChart -problem with time axis X
-
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); } -
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); }@Creatorczyk
I don't know. Did you try removing lines of code till it did work? Does the Y axis show? IsaxisX->setLabelsBrush(Qt::white);by any chance writing on a while background? InaxisX->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime());is it a good idea to set a range where the minimum & maximum are equal?