Important: Please read the 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();

    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};

    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)
    for(int i=0;i<std::extent<decltype(yArray)>::value;++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