PDA

View Full Version : QChart Questions



rhb327
17th May 2017, 12:03
I am creating a simple chart application:

* Create chart with two series using a value y-axis and a datetime x-axis
* Add data to the chart via a timer
* Scale the chart manually when data is added using setRange

The issues I'm having are:

* In general, I don't call createdefaultaxes()...I can't get the x-axis to adjust ever using setRange of setMax...why?
* I've tried updating the chartview as well...still no update

This is the chart setup:


// Create line series
ser1 = new QLineSeries();
ser2 = new QLineSeries();

// Create chart
chart = new QChart();
chart->addSeries(ser1);
chart->addSeries(ser2);
chart->legend()->hide();
chart->setTitle("Test Chart");
//chart->createDefaultAxes();

QDateTimeAxis *axisX = new QDateTimeAxis;
axisX->setTickCount(10);
axisX->setFormat("MM-dd-yy hh:mm:ss");
axisX->setLabelsAngle(-90);
axisX->setTitleText("Date");
//chart->addAxis(axisX, Qt::AlignBottom);
chart->setAxisX(axisX);
chart->axisX()->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime().addSecs(15));
ser1->attachAxis(axisX);
ser2->attachAxis(axisX);

QValueAxis *axisY = new QValueAxis;
axisY->setLabelFormat("%i");
axisY->setTitleText("Values");
//chart->addAxis(axisY, Qt::AlignLeft);
chart->setAxisY(axisY);
chart->axisY()->setRange(-2, 2);
ser1->attachAxis(axisY);
ser2->attachAxis(axisY);

// createDefaultAxes() - critical for autoscaling
//chart->createDefaultAxes();
//chart->setAxisX(axisX);
//chart->setAxisY(axisY);
//chart->axisX()->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime().addSecs(15));
//chart->axisY()->setRange(-2, 2);
//ser1->attachAxis(axisX);
//ser2->attachAxis(axisX);
//ser1->attachAxis(axisY);
//ser2->attachAxis(axisY);

// Setup view
chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->vLayout->addWidget(chartView);
chartView->setUpdatesEnabled(true);


This is the timer:


void MainWindow::AddData()
{
static int data = 0;

++data;
if (data > 359) {
data = 0;
}
ser1->append(QDateTime::currentDateTime().toMSecsSinceEp och(), qSin(qDegreesToRadians(double(data))));
ser2->append(QDateTime::currentDateTime().toMSecsSinceEp och(), qCos(qDegreesToRadians(double(data))));
// Rescale
QVector<QPointF> vec(ser1->pointsVector());
std::sort(vec.begin(), vec.end(), xLessThan);
chart->axisX()->setRange(vec.first().x(), vec.last().x());
chart->axisX()->setMax(vec.last().x());
chartView->update();
//ui->vLayout->update();
qDebug() << "Added data for " << data << " : " << QDateTime::currentDateTime().toMSecsSinceEpoch();
qDebug() << QString::number(long(vec.first().x())) << " : " << QString::number(long((vec.last().x())));
}


Added after 58 minutes:

Disregard...wrong units! The chart xAxis is data time but the series values are epoch cnt:

QVector<QPointF> vec(ser1->points().toVector());
std::sort(vec.begin(), vec.end(), xLessThan);
chart->axisX()->setMax(QDateTime::fromMSecsSinceEpoch(vec.last().x ()).addSecs(15));