Results 1 to 10 of 10

Thread: How to plot a simple function with QDateTime X axis and int/double Y axis?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default Re: How to plot a simple function with QDateTime X axis and int/double Y axis?

    OK, I have managed to fill the gaps in the chart by using Floating attribute:

    Qt Code:
    1. scaleEngineX->setAttribute(QwtScaleEngine::Floating, true);
    2. setAxisScaleEngine(QwtPlot::xBottom, scaleEngineX);
    To copy to clipboard, switch view to plain text mode 

    Now, the question is still, how to set major ticks to exact values of the original, source data, not some intermediate, calculated ones. (And of course, if there are too many discrete values on axis, to show only part of them based on an algorithm.)

    I have tried to play around with:

    setAxisMaxMinor(QwtPlot::xBottom, <some value>);
    setAxisMaxMajor(QwtPlot::xBottom, <model->rowCount() or other values>);

    No success. They always put ticks to wrong positions.
    Thank you for the help in advance! This would be the only critical thing to me.

  2. #2
    Join Date
    May 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default Re: How to plot a simple function with QDateTime X axis and int/double Y axis?

    Now I know i would have had to use QwtScaleDiv class for this purpose:

    Qt Code:
    1. QwtScaleDiv ChartPlot::scaleDiv()
    2. {
    3. QList<double> ticks[QwtScaleDiv::NTickTypes];
    4.  
    5. QList<double> &majorTicks = ticks[QwtScaleDiv::MajorTick];
    6.  
    7. foreach (double val, xData)
    8. majorTicks += val;
    9.  
    10. QList<double> &mediumTicks = ticks[QwtScaleDiv::MediumTick];
    11. for (int i = 0; i < xData.count() - 1; ++i)
    12. mediumTicks += QDateTime::fromTime_t(xData[i]).addSecs(900).toTime_t();
    13.  
    14. QList<double> &minorTicks = ticks[QwtScaleDiv::MinorTick];
    15. for (int i = 0; i < xData.count() - 1; ++i)
    16. minorTicks += QDateTime::fromTime_t(xData[i]).addSecs(300).toTime_t();
    17.  
    18. return QwtScaleDiv(xData.first(), xData.last(), ticks);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Simply call this function to set axis scale divisions in your QwtPlot instance as following:

    Qt Code:
    1. setAxisScaleDiv(QwtPlot::xBottom, scaleDiv());
    To copy to clipboard, switch view to plain text mode 

    The only problem now I have is to set one minor tick to 1/10 of major tick and one medium tick to 1/5 of major tick. Thanks for suggestions in advance!

  3. #3
    Join Date
    May 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default Re: How to plot a simple function with QDateTime X axis and int/double Y axis?

    I solved it, please, find it here for future reference:

    Qt Code:
    1. QwtScaleDiv ChartPlot::scaleDiv()
    2. {
    3. QList<double> ticks[QwtScaleDiv::NTickTypes];
    4.  
    5. QList<double> &majorTicks = ticks[QwtScaleDiv::MajorTick];
    6.  
    7. foreach (double val, xData)
    8. majorTicks += val;
    9.  
    10. if (xData.count()>1)
    11. {
    12. QList<double> &mediumTicks = ticks[QwtScaleDiv::MediumTick];
    13. for (int i = 0; i < xData.count() - 1; i++)
    14. for (int j = 1; j < 6; j++)
    15. mediumTicks += xData[i] + j * (xData[i+1] - xData[i]) / 5;
    16.  
    17. QList<double> &minorTicks = ticks[QwtScaleDiv::MinorTick];
    18. for (int i = 0; i < xData.count()*10 - 1; i++)
    19. for (int j = 1; j < 11; j++)
    20. minorTicks += xData[i] + j * (xData[i+1] - xData[i]) / 10;
    21. }
    22.  
    23. return QwtScaleDiv(xData.first(), xData.last(), ticks);
    24. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to falconium for this useful post:

    corrado1972 (4th May 2011)

Similar Threads

  1. Replies: 2
    Last Post: 8th February 2011, 15:53
  2. Replies: 0
    Last Post: 9th August 2010, 11:46
  3. Replies: 1
    Last Post: 30th July 2010, 08:23
  4. Virtual axis ?
    By viridis in forum Qwt
    Replies: 2
    Last Post: 1st July 2008, 16:28
  5. Qwt and custom axis
    By jiveaxe in forum Qwt
    Replies: 3
    Last Post: 14th November 2007, 16:50

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
  •  
Qt is a trademark of The Qt Company.