Problem with QChart
-
I need to plot a simple chart that basically is composed by points connected by lines so I though to use a mixture of the QScatterSeries and QLineSeries. What I would like to achieve is shown in the following graph (the blue line only):
But I got this instead:
The first point should have the Y value of 1.2 but in the second graph it seems to be 0. The X axis has no integer values because I don't need it, I just need to plot the Y value of some points corresponding to specific AOI names.
This is the code that draw the graphs:void MainWindow::generatePointChartForAOI(const QString& aoiName, const std::vector<AOI>& aoiList) { m_chart->removeAllSeries(); m_chart->removeAxis(m_axisY); // Target metric type and metric name const QString targetMetricTypeName = "Time to First Fixation"; const QString targetMetricName = "Mean"; // Create a single series for the target metric QLineSeries* series = new QLineSeries(); series->setName(targetMetricTypeName); // Create a QScatterSeries for the dots QScatterSeries* scatterSeries = new QScatterSeries(); scatterSeries->setName(targetMetricTypeName + " (dots)"); // Optional: Set a name for the scatter series scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeCircle); // Set the marker shape to a circle scatterSeries->setMarkerSize(10.0); // Adjust the marker size as needed QStringList categories; int aoiIndex = 0; for (const AOI& aoi : aoiList) { if (aoi.aoiName == aoiName) { continue; } // Find the target metric type auto itMetricType = std::find_if(aoi.metricTypes.begin(), aoi.metricTypes.end(), [&targetMetricTypeName](const MetricType& mt) { return mt.metricTypeName == targetMetricTypeName; }); if (itMetricType != aoi.metricTypes.end()) { // Find the target metric within the metric type auto itMetric = std::find_if(itMetricType->metrics.begin(), itMetricType->metrics.end(), [&targetMetricName](const Metric& m) { return m.descriptiveStat == targetMetricName; }); if (itMetric != itMetricType->metrics.end()) { bool ok; double metricValue = itMetric->value.toDouble(&ok); if (ok) { series->append(aoiIndex, metricValue); scatterSeries->append(aoiIndex, metricValue); qDebug() << "Adding:" << metricValue; // Prints correctly the values } else { qDebug() << "Failed to convert metric value to double:" << itMetric->value; } } } categories.append(aoi.aoiName); aoiIndex++; } m_chart->setTitle(aoiName); double minY = series->at(0).y(); double maxY = minY; for (const QPointF& point : series->points()) { maxY = qMax(maxY, point.y()); } m_chart->addSeries(series); m_chart->addSeries(scatterSeries); m_axisY->setRange(0, maxY + 1); qDebug() << m_axisY->min() << m_axisY->max(); m_axisX->append(categories); m_chart->addAxis(m_axisY, Qt::AlignLeft); }
The values are correct, the first value prints 1.2
I did some initializations in the MainWindow ctorMainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->loadFileButton, &QPushButton::clicked, this, &MainWindow::loadFile); connect(ui->expandPushButton, &QPushButton::clicked, this, &MainWindow::handleExpandButton); m_chart = new QChart; m_axisY = new QValueAxis(); m_axisX = new QBarCategoryAxis(); m_axisX->setTitleText("AOI Names"); m_axisX->setLabelsAngle(-45); m_chart->addAxis(m_axisX, Qt::AlignBottom); m_chart->addAxis(m_axisY, Qt::AlignLeft); ui->chartView->setChart(m_chart); ui->chartView->setRenderHint(QPainter::Antialiasing); }
The generatePointChartForAOI method is executed every time the user clicks on some items in a treeview.
I also noticed that at every call, the labels in the X axis are added to the labels of the previous call.
I tried also to remove the X axis at every call and add again but it didn't solve the problemvoid MainWindow::generatePointChartForAOI(const QString& aoiName, const std::vector<AOI>& aoiList) { m_chart->removeAllSeries(); m_chart->removeAxis(m_axisX);
I also would like the x position of the points so stay in the middle of the AOI labels
I would like to have some help