Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QChart
Forum Updated to NodeBB v4.3 + New Features

Problem with QChart

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 144 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • franco.amatoF Offline
    franco.amatoF Offline
    franco.amato
    wrote on last edited by franco.amato
    #1

    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):

    05e3191f-6d30-43e3-bbe3-8783bac75aa3-image.png

    But I got this instead:

    1a2c3db0-8679-42e4-96d6-6b2f81bea62f-image.png

    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 ctor

    MainWindow::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 problem

    void 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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved