I am trying to achieve something like https://doc.qt.io/qt-5.9/qtcharts-datetimeaxis-example.html .
In a method I have this code
QDateTime past = QDateTime::currentDateTime().addMonths(-10);
QDateTime now = QDateTime::currentDateTime();
qreal pastvalue = 4;
qreal nowvalue = 4;
axisY = new QValueAxis();
axisX= new QDateTimeAxis();
chart = new QChart();
series = new QLineSeries();
/*Y*/
axisY->setLabelFormat("%i");
axisY->setTitleText("Numero dispositivi");
axisY->setMin(0);
axisY->setMax(5);
/*X*/
axisX->setTickCount(2);
axisX->setMin(past);
axisX->setMax(now);
axisX->setFormat("dd-MM-yyyy h:mm:ss");
/*series/*
series->attachAxis(axisX);
series->attachAxis(axisY);
series->append(past.toSecsSinceEpoch(),pastvalue);
series->append(past.toSecsSinceEpoch(),nowvalue);
/*chart*/
chart->legend()->hide();
chart->setTitle("Dati filtrati durante il periodo temporale");
chart->addAxis(axisY, Qt::AlignLeft);
chart->addAxis(axisX, Qt::AlignBottom);
chart->addSeries(series);
chartView = new QChartView(chart);
ui->verticalLayout->addWidget(chartView);
I don't understand why values are not displayed.
I can't figure out what's wrong.
Your code has the following errors:
toMSecsSinceEpoch()
instead oftoSecsSinceEpoch()
.QChart
first before attaching it to the series.change
series->append(past.toSecsSinceEpoch(),nowvalue);
toseries->append(now.toMSecsSinceEpoch(),nowvalue);
I recommend you to set the ticks so that only the whole points are shown since, for example, the inappropriate value will be displayed on the vertical axis, or set as floating with a certain number of decimal places, in the following image I show the result with %i:
As we can see in the previous image, 4 is very close to 3 and farther than 5, instead of being equidistant, so in the solution I propose, you will use %.2f: