Results 1 to 4 of 4

Thread: How to show all labels

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to show all labels

    This should be simple; is there a way to show all labels on xBottom axis? I'm currently doing some stuff with QwtPlotHistogram and some labels are hidden when I have lots of columns

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to show all labels

    I think you (and others) might be confusing QwtPlotHistogram with a bar chart (like Microsoft Excel creates). A QwtPlotHistogram is a way to display x-y data as a series of sticks, so all the normal rules for axis labels are applied: choose a set of major tick divisions that creates a "nice" numerical series, and don't plot a label if it would overlap with another label.

    This is not the kind of behavior desired for a bar chart, because the x-axis isn't a numerical axis, it is a category axis, and you want to see all the labels for the categories.

    If you want to change this behavior, then you will have to implement your own axis scale classes for the x axis. This is not really an easy job; you will have to implement a QwtScaleEngine::divideScale() that returns a QwtScaleDiv with a QwtValueList containing a value for every tick label you want to show. You may also need to implement a custom QwtScaleDraw class that will force labels to overlap (or do something fancier like stack them in a staggered fashion).

    It can be done; I did this once for an application that plotted dendrograms inside a QwtPlot. The axis at the leaf end of the dendrogram had to label each leaf with its name. It took a long time to understand how the QwtScale... classes are implemented, how they work together, and how to get them to work correctly along with zooming, panning, and magnification.

    In the attached ZIP file are the classes derived from QwtScaleEngine and QwtScaleDraw. The QwtScaleDraw class was needed to ensure all of the leaf labels were cached and would be drawn, as well as giving individual labels different colors depending on the color-coding of the dendrogram sub-trees. The axis is defined to have a real interval equal to the number of leaves in the dendrogram; if there are 100 leaf nodes, the axis scale goes from 0 - 100 (or maybe -0.5 - 100.5 to make sure the end-most leaf labels are within the ends of the axis). So, the scale engine class takes this range, turns it into an integer range, then creates a scale division for each integer.

    This is basically the same thing you will have to do to make a label visible for every stick in the QwtPlotHistogram. Substitute "stick" for "leaf", and it's all the same.
    Attached Files Attached Files

  3. The following 2 users say thank you to d_stranz for this useful post:

    jccosta (27th April 2012), Patrik (18th January 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show all labels

    Thank you, I had already implemented a QwtScaleDraw derivate class that places text labels where I want

    Qt Code:
    1. class BarChartScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. BarChartScaleDraw(QMap<double,QString> *x) : ids(x) { enableComponent(QwtAbstractScaleDraw::Ticks, false); }
    5.  
    6. virtual QwtText label(double v) const
    7. {
    8. if (ids->contains(v))
    9. return ids->find(v).value();
    10. else
    11. return "";
    12. }
    13. private:
    14. QMap<double,QString> *ids;
    15. };
    To copy to clipboard, switch view to plain text mode 

    Apparently combining my class with your scale engine did the trick

    One more question, how can I reset the scale engine?
    Last edited by Patrik; 18th January 2011 at 11:42.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to show all labels

    Quote Originally Posted by Patrik View Post
    One more question, how can I reset the scale engine?
    I am not sure I know what you mean by "reset". When the axis scale is changed (by a zoom/pan/magnify or by an appplication call to QwtPlot::setAxisScale()), the scale engine's divideScale() should automatically be called by the framework when the plot is updated.

    Perhaps you are having a problem because the QwtAbstractScaleDraw class caches the labels. In your QtScaleDraw class, after you change the label values call the invalidateCache() method (a protected method in QwtAbstractScaleDraw). This will force a recalculation of the labels. See my example code posted earlier.

    You don't show it in your code, but if your scale draw class has a method for updating your label map (other than in the constructor), you should call invalidateCache() before exiting that method. Probably wouldn't hurt to do it in the constructor as well.

Similar Threads

  1. Replies: 3
    Last Post: 12th July 2010, 14:12
  2. how to dynamically add labels
    By mohanakrishnan in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2009, 11:19
  3. Axis with more labels
    By rakkar in forum Qwt
    Replies: 1
    Last Post: 11th October 2009, 10:26
  4. refreshing labels
    By ht1 in forum Qt Programming
    Replies: 4
    Last Post: 29th December 2007, 22:02

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.