c++qtqchart

QChart points are incorrectly plotted when I change the XAxis and YAxis


I am making a QScatterSeries. I have some points plotted in 3 different series (red, green, blue). It all plots perfectly when I use chart->createDefaultAxes(). All points are where they should be. The issue is that when I change the X Axis and Y Axis in my chart, all my points become incorrectly plotted. None of them are where they should be. This is strange because I have hard-coded my points.

I have tried adding my series to the chart before and after adding the X and Y Axis to my chart, thinking that may have caused my points to become confused. But that did not fix it.

Chart when I use the default axes:

enter image description here

    char *myargv[2];
    int myargc = 1;
    myargv[0] = strdup("");

    // First, create QApplication
    QApplication a(myargc, myargv);

    // Create our red, green, blue acceleration series
    redSeries = new QScatterSeries;
    redSeries->setMarkerSize(15.0);
    redSeries->append(1, 20);
    redSeries->append(2, 30);
    redSeries->append(3, 44);
    redSeries->setColor(Qt::red);

    greenSeries = new QScatterSeries;
    greenSeries->setMarkerSize(15.0);
    greenSeries->append(1, 10);
    greenSeries->append(2, -9);
    greenSeries->append(3, 20);
    greenSeries->setColor(Qt::green);

    blueSeries = new QScatterSeries;
    blueSeries->setMarkerSize(15.0);
    blueSeries->append(1, -20);
    blueSeries->append(2, -10);
    blueSeries->append(3, 0);
    blueSeries->setColor(Qt::blue);

    // Customize our chart
    QChart *chart = new QChart();
    chart->createDefaultAxes();
    chart->setTitle("Example Plot");
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0, 130);
    axisX->setTickCount(10);
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(-50, 50);
    axisY->setTickCount(10);

    // Add series to chart
    chart->addSeries(redSeries);
    chart->addSeries(greenSeries);
    chart->addSeries(blueSeries);

    // Create chartView
    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    //chartView->chart()->setAxisX(axisX);
    //chartView->chart()->setAxisY(axisY);
    
    // Render everything within our Window
    QMainWindow w;
    w.setCentralWidget(chartView);
    w.resize(400, 300);
    w.show();

    return a.exec();
}

When I try to use my own X and Y axis, I simply comment out the chart->createDefaultAxes() and comment back in the:

//chartView->chart()->setAxisX(axisX);
//chartView->chart()->setAxisY(axisY);

This causes me to get the incorrectly plotted chart: enter image description here

My question is, what am I doing wrong? Also, in other parts of my code, I plan to dynamically receive data and use that data to plot points on my graph, such as this:

while (receivingData) {
    redSeries->append(x, y); // Will not work because my hard-coded points (above) do not work
    greenSeries->append(x, y);
    blueSeries->append(x, y);
}

What do I have to do to ensure that all my points are plotted correctly with my custom X and Y Axis? I am assuming that I will most likely have to do some math within my append to match my QChart's new dimensions but how do I achieve that?


Solution

  • I found the answer to my question in another post.

    Posting here in case it helps someone QtCharts add custom axis