Results 1 to 2 of 2

Thread: QCustomPlot, changing graph scale and resizing data

  1. #1
    Join Date
    Nov 2019
    Location
    Lyon, France
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QCustomPlot, changing graph scale and resizing data

    Hi!

    I have a file with lots of data in it (for this example, 32 plots with several million points each). So to prevent long loading time, I want to just load the data that will be displayed on the graphs. If the user want to see the next page or change the scale of the plots, the new data are loaded.
    I can display the graphs one below the other, and when changing the page, it works good (for example, the first page contain the points from 0 to 10000, the second from 10001 to 20000...).
    But I have a problem when changing the scales, either on the x- or the y-axis.

    Here's how I create my plots :

    Qt Code:
    1. void CentralMiddleLayer::displayFirstLoading()
    2. {
    3. // Creation of the x axis
    4. //--------------------
    5. x.resize(scale);
    6. for (int i = 0; i < scale; i++)
    7. x[i] = i;
    8.  
    9. // Matrix dimensions resize
    10. //--------------------
    11. matrix.resize(channelNb); // First dimension
    12. for (int i = 0; i < channelNb; i ++)
    13. matrix[i].resize(scale); // Second dimension
    14.  
    15. // Get data from file, put into the matrix
    16. //--------------------
    17. FileReader::yAxisData(filename, matrix, actualRange.lower, scale); // This is the function that will read the file and put the data of interest in the matrix
    18.  
    19.  
    20.  
    21. plot = new QCustomPlot;
    22. nbGraph = 32;
    23.  
    24. // Offset the data so they're not on top of each other
    25. //----------------------------------------
    26. QCPRange defaultRange(-100., 100.); // y-axis range
    27. for (int i = 0; i < nbGraph; i++)
    28. {
    29. QCPAxis *axis = plot->axisRect()->addAxis(QCPAxis::atLeft);
    30. axis->setRange(defaultRange.lower - (nbGraph - 1 - i) * defaultRange.size(), defaultRange.upper + i * defaultRange.size()); // I don't really understand why this work to be honest...
    31. axis->setVisible(false);
    32.  
    33. QCPGraph *graph = new QCPGraph(plot->xAxis, axis);
    34. }
    35.  
    36.  
    37. // Set the data to the plot
    38. //----------------------------------------
    39. for(int i = 0; i < plot->graphCount(); i++)
    40. {
    41. plot->graph(i)->setData(x, matrix[i], true);
    42. }
    43.  
    44.  
    45. // Set plot characteristics
    46. //----------------------------------------
    47. plot->xAxis->setRange(0, 10000.0); // Number of point per page
    48.  
    49. plot->xAxis->setVisible(false);
    50. plot->yAxis->setVisible(false);
    51.  
    52. plot->axisRect()->setAutoMargins(QCP::msNone);
    53. plot->axisRect()->setMargins(QMargins(0, 10, 0, 0));
    54. plot->setStyleSheet(("background:hsva(255, 255, 255, 0%);"));
    55. plot->setBackground(QBrush(Qt::NoBrush));
    56.  
    57. plot->setFixedHeight(700);
    58. }
    To copy to clipboard, switch view to plain text mode 


    Now, I don't see how to change the scales.

    For the x-axis (time scale), if I rescale it when the application is running (for example I want 15000 points per graph instead of 10000), I get this error message :
    void QCPGraph::addData(const QVector<double>&, const QVector<double>&, bool) keys and values have different sizes: 10000 15000
    Here's the code I use for this :

    Qt Code:
    1. void CentralMiddleLayer::timeScaleChange(double time)
    2. {
    3. scale = time; // the new time scale = how many point displayed per page and per graph
    4.  
    5. for (int i = 0; i < channelNb; i ++)
    6. matrix[i].resize(scale); // the data matrix get its new size
    7.  
    8. setActualRange(actualRange.lower);
    9. FileReader::yAxisData(filename, matrix, actualRange.lower, actualRange.upper); // the new data are read, the data corresponding to the new range are put inside the matrix
    10.  
    11. for(int i = 0; i< plot->graphCount(); i++)
    12. plot->graph(i)->setData(x, matrix[i]); // the graphs receives their new data. From the documentation, this should work fine...
    13.  
    14. plot->xAxis->setRange(0, scale);
    15.  
    16. plot->replot();
    17. }
    To copy to clipboard, switch view to plain text mode 

    And for the y-axis, If I change the scale nothing happen...


    If someone is used to QCustomPlot, do you have any advice ?
    I think my problem comes from the way I construct my graph, but on important point that I can't ignore is that the graphs have to be able to overlap each other (if the datas requires it). And I only find this way to do it. Is there an other one ?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,720
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QCustomPlot, changing graph scale and resizing data

    For the x-axis (time scale), if I rescale it when the application is running (for example I want 15000 points per graph instead of 10000), I get this error message :
    If you add data to the graph in vectors, obviously both vectors have to be the same size. If you specify 10000 for x and 15000 for y, then what is the graph supposed to do for the extra 5000 points missing from the x vector? Make something up?

    plot->graph(i)->setData(x, matrix[i]); // the graphs receives their new data. From the documentation, this should work fine...
    I think you misunderstand the documentation. Where do you resize "x" to be the same size as "matrix[i]"? There has to be a one-to-one correspondence between the x and y vectors - every x value is matched to the corresponding y value (x[i], y[i]) when creating the point or line segment on the graph. So if you add 5000 more y points to the graph, you have to also specify the extra 5000 matching x coordinates.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 10th June 2015, 16:48
  2. Replies: 2
    Last Post: 7th June 2015, 09:21
  3. Replies: 1
    Last Post: 11th February 2014, 22:31
  4. Replies: 8
    Last Post: 27th May 2011, 13:22
  5. Replies: 8
    Last Post: 25th April 2010, 22:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.