Results 1 to 2 of 2

Thread: QCustomPlot - overlapping graphs, fixed origin and y-axis rescaling

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

    Default QCustomPlot - overlapping graphs, fixed origin and y-axis rescaling

    Hello everyone!

    I use QCustomPlot to draw my graphs and I tried different methods and nothing seems to give me the result I search.

    I want to draw multiple graphs, one below the other. The separation between each graph should be the same for all of them.
    I also want to be able to change the scale of both the x- and y-axis, keeping the origin of each graph the same (their "0" shouldn't move when rescaling the y-axis). Which also mean that each graph could overlap with the others if needed (this also mean that it can happen that data can go out of the screen, but that's ok).
    For the x-axis, I don't have a problem, its quite easy. Its the rescaling of the y-axis that gives me problems...

    So here's what I've got for now:

    In this first code, I'm able to fix the position of each plot, but I don't see how to change the y-axis scale during execution.
    Qt Code:
    1. plot = new QCustomPlot;
    2.  
    3. xAxis.resize(timeScale);
    4. for (int i = 0; i < timeScale; i++)
    5. {
    6. xAxis[i] = i;
    7. }
    8.  
    9. // Offset the data so they're not on top of each other
    10. //--------------------
    11. QCPRange defaultRange(-100., 100.);
    12. for (int i = 0; i < nbPlot; i++)
    13. {
    14. QCPAxis *axis = plot->axisRect()->addAxis(QCPAxis::atLeft);
    15. axis->setRange(defaultRange.lower - (nbPlot - 1 - i) * defaultRange.size(),
    16. defaultRange.upper + i * defaultRange.size());
    17. axis->setVisible(false);
    18.  
    19. QCPGraph *graph = new QCPGraph(plot->xAxis, axis);
    20. }
    21.  
    22. // Set the data to the plot
    23. //--------------------
    24. for (int i = 0; i < plot->graphCount(); i++)
    25. {
    26. plot->graph(i)->setData(xAxis, plotMatrix[i], true);
    27. }
    To copy to clipboard, switch view to plain text mode 


    In this second code, I offset the data, and I'm able to dynamically change the y-axis during execution. But the graphs's origins aren't fixed, and move when rescaling.
    Qt Code:
    1. plot = new QCustomPlot;
    2.  
    3. xAxis.resize(timeScale);
    4. for (int i = 0; i < timeScale; i++)
    5. {
    6. xAxis[i] = i;
    7. }
    8.  
    9. // Set the data to the plot
    10. //--------------------
    11. for (int i=0; i<nbPlot; ++i)
    12. {
    13. plot->addGraph();
    14. // Offset data:
    15. for (int j = 0; j < plotMatrix[i].length(); j++)
    16. {
    17. plotMatrix[i][j] -= (i + 1) * 200;
    18. }
    19. plot->graph()->setData(xAxis, plotMatrix[i]);
    20. plot->graph()->rescaleAxes(true);
    21. }
    To copy to clipboard, switch view to plain text mode 

    In both cases, the graphs can overlap with the other if the data ask for it. But none of those codes satisfy my needs...

    If you have any advice on how to make it or even code example, I'll take it ^^

    Have a good day!

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

    Default Re: QCustomPlot - overlapping graphs, fixed origin and y-axis rescaling

    Hi again!

    So, after some testing, I found something that's working! (don't know why I didn't thought of it earlier...)

    Here's the solution for anyone having the same problem (I kept the first code I gave to create the graphs):
    Qt Code:
    1. QCPRange newRange(-50., 50.); // Choose your range according to your data
    2. for (int i = 0; i < nbPlot; i++)
    3. {
    4. QCPAxis *newAxis = new QCPAxis(plot->axisRect(), QCPAxis::atLeft);
    5.  
    6. newAxis->setRange(newRange.lower - (nbPlot - 1 - i) * newRange.size(),
    7. newRange.upper + i * newRange.size());
    8. newAxis->setVisible(false);
    9.  
    10. plot->graph(i)->setValueAxis(newAxis);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Fast and easy.

Similar Threads

  1. Replies: 1
    Last Post: 16th March 2018, 23:53
  2. QCustomPlot using timestamp to set Date along one axis
    By bandito in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2016, 19:32
  3. Replies: 2
    Last Post: 24th June 2012, 02:17
  4. QwtPlot fixed X-axis display
    By micc13 in forum Qwt
    Replies: 2
    Last Post: 3rd December 2010, 13:28
  5. fixed axis scales
    By oskarmellow in forum Qwt
    Replies: 1
    Last Post: 17th December 2008, 07:20

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.