QChart line series not shown
-
OK why aren't the points in the line series being displayed?
// // Add the score and update the score chart // if (nullptr == scoreSeries) { scoreSeries = new QLineSeries(this); scoreSeries->setName(tr("Score", "IDC_SCORE")); scoreSeries->setPointsVisible(true); scoreChart->addSeries(scoreSeries); scoreChart->createDefaultAxes(); connect(scoreSeries, &QLineSeries::hovered, this, &ChartTab::scoreHovered); } scoreSeries->append(x, fScore); scoreMap.emplace(name, size - 1); axes = scoreChart->axes(Qt::Vertical); for (const auto& p : axes) { QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) }; if (axis) axis->applyNiceNumbers(); } axes = scoreChart->axes(Qt::Horizontal); for (const auto& p : axes) { QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) }; if (axis) { axis->setRange(1.0, size); axis->setTickAnchor(1.0); axis->setTickType(QValueAxis::TicksDynamic); axis->setTickInterval(interval); } } scoreChart->update();
As more points are added to the series the horizontal axis updates take place just fine, but the actual data is never displayed (I can force this by removing and re-adding the line series but that is just SO WRONG). Here's the disply after adding six data points:
Note the total absence of points and lines between them.
The following HORRID code sort of works:
if (nullptr != fwhmSeries) fwhmChart->removeSeries(fwhmSeries); else { fwhmSeries = new QLineSeries(this); fwhmSeries->setName(tr("FWHM", "IDC_FWHM")); fwhmSeries->setPointsVisible(true); connect(fwhmSeries, &QLineSeries::hovered, this, &ChartTab::fwhmHovered); } fwhmSeries->append(x, fFWHM); fwhmMap.emplace(name, size - 1); fwhmChart->addSeries(fwhmSeries);
With this result:
But I REALLY shouldn't need to do that (should I ???).
Does anyone here know how to make this QChart stuff work? It would seem that I haven't worked it out :(!!
David
-
@Perdrix
I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so..... -
@Perdrix
BTW, in order to keep you happy I have loaded up QtCharts, which I had never used. The follwoing code works fine for me for updating the chart in real time:inline QLineSeries *ChartView::lineSeries(int i) const { return qobject_cast<QLineSeries *>(_chart->series()[i]); } inline QValueAxis *ChartView::axisX() const { return qobject_cast<QValueAxis *>(_chart->axes(Qt::Horizontal)[0]); } inline QValueAxis *ChartView::axisY() const { return qobject_cast<QValueAxis *>(_chart->axes(Qt::Vertical)[0]); } void ChartView::clear() { // clear all series from the chart _chart->removeAllSeries(); } void ChartView::setSeries(const QStringList &names) { // set all the series the chart is to show clear(); for (auto name : names) { QLineSeries *series = new QLineSeries(); series->setName(name); _chart->addSeries(series); } // set the initial state of the axes _chart->createDefaultAxes(); axisX()->setLabelFormat("%d"); axisY()->setLabelFormat("%d"); axisX()->setRange(0, 100); axisY()->setRange(0, 1000); } void ChartView::addSeriesValues(const QList<int> &values) { // add values for all series Q_ASSERT(values.count() == _chart->series().count()); for (int i = 0; i < values.count(); i++) { // append the new point at the end of the last existing one int x = lineSeries(i)->count(), y = values.at(i); lineSeries(i)->append(x, y); // if x or y values exceed current axis maximum, increase axis maximum by 10% int maxX = axisX()->max() + 1; if (x >= maxX) axisX()->setMax(x + (x + 9) / 10); int maxY = axisY()->max() + 1; if (y >= maxY) axisY()->setMax(y + (y + 9) / 10); } }
I am Linux, Qt 5.15. Note that as shown it is your job to reset axes as required when adding new points, as I suggested in the other post....
-
-
@JonB said in QChart line series not shown:
@Perdrix
I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so.....I never received the email notifying me of that update - if I had you can be sure I would have responded.
-
@Perdrix
I think you need to review your notifications then, or look back at your topics regularly :)The gist of that, plus the code shown here I am just playing with this morning, shows there is "nothing special to do" to see points being added and updated in real time (assuming you're allowing the vent loop to run). Note that if you plot a point outside the current axes' limits it is your job to adjust the axes' range to allow, else you don't see it.
-
@JonB >Note that if you plot a point outside the current axes' limits it is your job to adjust the axes' range to allow, else you don't see it.
Indeed that would appear to be the crux of the problem.
I think that some additional tutorial stuff in the docs would help a LOT :)
Thanks
David -
-
@JonB I can't quite understand this code, he seems to be the implementation of library functions? At the end of this page, the problem that is not displayed seems to be an axis problem, but my problem does not seem to use the axis, do I need to modify the axis?
-
@printfne
I believe (though not certain) that it is indeed an axes issue. When adding points to an existing series on the chart I think you have to adjust the axes to ensure it is within range to show,.All that matters in my code is the updating of the axes' ranges in
addSeriesValues()
.What does
qDebug() << chart->axes().count()
report?I assume the chart must always have some idea of axes and range/scale, else how would it know where to plot anything?
If you have some points visible initially, you might also trying adding new points within the range of what is already there instead of beyond it. Maybe you would see such points updated.
This question is about not seeing newly added points after the initial display of the chart. Even if you don't add points later you may need to call
createDefaultAxes()
anyway, I don't know.