Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCharts - QLineSeries from independent X and Y arrays



  • Hi,
    What's the best way to plot two arrays in a LineChart with QCharts , one versus the other,

    #define SIZE 100
    int xArray[SIZE];
    int yArray[SIZE];

    Do I have to create a list of QPoints manually from these arrays?
    Is there any way to add xArray or yArray to QLineSeries without modifying the other?


  • Qt Champions 2017

    You can apply some thing simple like follows

    QLineSeries *series0 = new QLineSeries();
    
    series0->append(0,6);
    series0->append(2,4);
    

    chart.addSeries(series0);
    You don't have to create Points.



  • Thanks for your answer. I'd like to append to QLineSeries the X coordinates independent from the Y coordinates, in your exaplme something like,

    QLineSeries *series0 = new QLineSeries();
    
    int xArray[2] = {0, 2};
    int yArray[2] = {6, 4};
    
    series0->setXData(xArray);
    series0->setYData(yArray);
    

    I want the best way to change X or Y data without modifying the other.



  • You can set all the points with one of the coordinates at 0 and then use replace()

    for(int i : xArray)
    series0->append(i,0);
    Q_ASSERT(series0->count()>=std::extent<decltype(yArray)>::value);
    for(int i=0;i<std::extent<decltype(yArray)>::value;++i)
    series0->replace(i,series0->at(i).x(),yArray[i]);
    
    


  • @VRonin
    Thanks! Thats a way, but what about performance?
    I'd like a way to pass a pointer to the x or y data structure, instead of copying it.
    I am migrating an application that used Qwt. That library had some functions meant for performance like,

    series.setRawSamples(xData, yData, numData);
    painter.drawSeries(&series, from, to);
    

    I know it's a different library, but I need to plot data in an environment with constant change in X or Y data.
    I tried to use a QVXYModelMapper but I didn't find a way to manage the X and Y data from the model in a independent way.



  • if you can use QVXYModelMapper then it's already done. For example:

    • mapper is QVXYModelMapper
    • i is the index of the point you want to change the Y coordinate for
    • newY is the new value for Y
    mapper.model()->setData(mapper.model()->index(i,mapper.yColumn()), newY);
    

Log in to reply