Results 1 to 4 of 4

Thread: Axis Tick Labels Overlap

  1. #1
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Axis Tick Labels Overlap

    I am using the Qwt plotting library to draw some basic type plots and charts. In the application, we also allow people to include a grid of plots that are shown in the window.

    My problem is that when the plot gets too small and the user wants to see larger fonts on the axis labels, the axis labels will draw on top of each other and then they can become unreadable. (See attached picture).

    overlap_tick_labe.png

    Background:

    [LIST=1]We are using manual scaling for the axis. I.E. We are setting them manually so that we can control what portion of the curve to draw on the screen.

    Qt Code:
    1. QwtPlot::setAxisScale(_axis, _axisMin, _axisMax);
    To copy to clipboard, switch view to plain text mode 
    1. We do not set the max major, medium, or minor number of ticks.


    Can anyone help me figure this out. I think that I have tried everything that I can think of, but I still can't figure it out.

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,296
    Thanked 878 Times in 826 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Axis Tick Labels Overlap

    When there is not enough space for displaying the tick labels then there is not enough space for displaying them. You can increase the size of the plot reduce the font size or manipulate the number and position of the major ticks.

    Most widgets won't be displayed properly when you don't respect their sizeHint/minimumSizeHint.

    Uwe

  3. #3
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Axis Tick Labels Overlap

    Uwe,

    I would gladly change the number of ticks shown to fit in the space provided for the plot, but I don't know, prior to drawing, how many ticks are going to displayed and how large the fonts will be for each one. If I knew this or if you could tell me how to find this out and what class or sub-class from, then I would be able to remove every other tick label from the axis widget so that they had enough space. Any guidance on this would be appreciated. Also, if there is another way this could be done, I am open to suggestions.

    Amos

  4. #4
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Wink Re: Axis Tick Labels Overlap

    I finally figured out how to prevent the tick labels from overlapping. The key was to re-implement the QwtAbstractScaleDraw::draw(QPainter *, const QPallette &) function and roll my own.

    Once inside the draw function, I can calculate the size of the tick labels and determine if they are going to overlap. If they do, then I start deleting every other one in order so that they don't appear jumpy when panning. Here is my code.

    Warning: I made a couple of presumptions in my code because I always just tick labels which are not rotated and always center aligned.
    Qt Code:
    1. // Get the major tick values
    2. QList<double> major_ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
    3. QList<QRect> label_rects;
    4.  
    5. // Remove the unused ticks
    6. for(i = major_ticks.count(); --i >= 0;)
    7. {
    8. if(!scaleDiv().contains(major_ticks[i]))
    9. {
    10. major_ticks.removeAt(i);
    11. }
    12. }
    13.  
    14. // Compute the rectangles of the major ticks
    15. for (i = 0; i < major_ticks.size(); i++)
    16. {
    17. QRect bounds = boundingLabelRect(painter->font(), major_ticks[i]);
    18. int half_padding = painter->font().pointSize() / 4;
    19.  
    20. if(orientation() == Qt::Horizontal)
    21. {
    22. bounds.adjust(-half_padding, 0, half_padding, 0);
    23. }
    24. else
    25. {
    26. bounds.adjust(0, -half_padding / 2, 0, half_padding / 2);
    27. }
    28. label_rects.append(bounds);
    29. }
    30.  
    31. // Now remove the overlapping tick labels
    32. bool overlap_found = false;
    33. do
    34. {
    35. overlap_found = false;
    36. for(i = 1; i < label_rects.size(); i++)
    37. {
    38. QRect last_rect = label_rects.at(i-1);
    39. QRect current_rect = label_rects.at(i);
    40.  
    41. // Check if they overlap
    42. if(current_rect.intersects(last_rect))
    43. {
    44. overlap_found = true;
    45. break;
    46. }
    47. }
    48.  
    49. if(overlap_found)
    50. {
    51. // Now remove the next to last tick if the number is even
    52. if((major_ticks.size() % 2) == 0)
    53. {
    54. // Remove the next to last tick
    55. if(major_ticks.size() > 2)
    56. {
    57. label_rects.removeAt(major_ticks.size() - 2);
    58. major_ticks.removeAt(major_ticks.size() - 2);
    59. }
    60. }
    61.  
    62. // Remove every other one starting at the second tick mark
    63. for(i = (major_ticks.size() / 2) * 2 - 1; i >= 0; i -= 2)
    64. {
    65. label_rects.removeAt(i);
    66. major_ticks.removeAt(i);
    67. }
    68. }
    69. } while(overlap_found);
    70.  
    71. // Now draw the remaining tick labels
    72. for (int i = 0; i < major_ticks.size(); i++)
    73. {
    74. drawLabel(painter, major_ticks[i]);
    75. }
    To copy to clipboard, switch view to plain text mode 

    I hope that someone else can benefit from my struggles.

    If you have any improvements on my code, please send them on.

Similar Threads

  1. y-axis tick labels trimmed
    By gib in forum Qwt
    Replies: 2
    Last Post: 2nd April 2010, 06:19
  2. Axis with more labels
    By rakkar in forum Qwt
    Replies: 1
    Last Post: 11th October 2009, 10:26
  3. Relocating axis labels
    By malcom2073 in forum Qwt
    Replies: 0
    Last Post: 9th May 2008, 14:01
  4. Tick Labels for Slider
    By jcraig in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 18:21
  5. QSlider with a custom set of labels for the tick marks?
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2007, 17:05

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.