Results 1 to 7 of 7

Thread: Help with histograms

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

    Default Help with histograms

    Hi, I need to plot a histogram but I do not know how. Documentation was not helpful enough. Here is my code

    Qt Code:
    1. void Plot(QwtPlot *plt)
    2. {
    3. plt->setTitle(dataName);
    4. plt->setAxisTitle(QwtPlot::xBottom, "Label");
    5. plt->setAxisTitle(QwtPlot::yLeft, "Values");
    6. QwtPlotHistogram *hist = new QwtPlotHistogram("blabla");
    7. QVector<QwtIntervalSample> data;
    8. data.append(QwtIntervalSample(1,0,4));
    9. data.append(QwtIntervalSample(3,0,7));
    10. hist->setSamples(data);
    11. hist->attach(plt);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Basically I want two columns, one labelled "1" ranging from 0 to 4 and one labelled "3" ranging from 0 to 7. Something like this image


    Any help is appreciated

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

    Default Re: Help with histograms

    You want to have a bar chart not a histogram !

    Unfortunatley Qwt offers no plot item for bar charts ( yet ) so you have to misuse other plot items ( or write your own ). In case of misusing QwtPlotHistogram you would have the following samples for your example:

    [0.5-1.5[ -> 3
    [2,5-3.5[ -> 7

    In some plot items a value is mapped to an interval, in others ( like the histogram ) the interval is mapped to a value. QwtIntervalSample is used for both situations - that's why the order of the parameters is different to what you might expect.

    Uwe

    PS: You could also try to use QwtPlotCurve in QwtPlotCurve::Sticks style and a fat pen.

  3. The following user says thank you to Uwe for this useful post:

    Patrik (21st December 2010)

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

    Default Re: Help with histograms

    Thanks, I'll try

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

    Default Re: Help with histograms

    I've writter a little class to be used as QwtScaleDraw.

    Qt Code:
    1. #ifndef _BARCHARTSCALEDRAW_
    2. #define _BARCHARTSCALEDRAW_
    3. #include <QMap>
    4. #include <qwt_scale_draw.h>
    5.  
    6. class BarChartScaleDraw: public QwtScaleDraw
    7. {
    8. public:
    9. BarChartScaleDraw(QMap<double,QString> *x) : ids(x) { }
    10.  
    11. virtual QwtText label(double v) const
    12. {
    13. if (ids->contains(v))
    14. return ids->find(v).value();
    15. else
    16. return "";
    17. }
    18. private:
    19. QMap<double,QString> *ids;
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    It works but requires manual positioning of the labels.

    Now I would like to know how to remove the "notches" from the X axis. Any ideas?

  6. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with histograms

    I'm not sure which "notches" you mean, maybe it's called "ticks" in qwt.

    this removes everything:
    Qt Code:
    1. QwtScaleDraw *sd = myPlot->axisScaleDraw(QwtPlot::xBottom);
    2. sd->enableComponent(QwtAbstractScaleDraw::Labels, false);
    3. sd->enableComponent(QwtAbstractScaleDraw::Ticks, false);
    4. sd->enableComponent(QwtAbstractScaleDraw::Backbone, false);
    To copy to clipboard, switch view to plain text mode 

    if you want to remove just the small ticks, use this:
    Qt Code:
    1. myPlot->setAxisMaxMinor(QwtPlot::yLeft, 0);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to FelixB for this useful post:

    Patrik (21st December 2010)

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

    Default Re: Help with histograms

    Quote Originally Posted by FelixB View Post
    I'm not sure which "notches" you mean, maybe it's called "ticks" in qwt.
    Yes, ticks. Sorry, my bad

    this removes everything:
    Qt Code:
    1. QwtScaleDraw *sd = myPlot->axisScaleDraw(QwtPlot::xBottom);
    2. sd->enableComponent(QwtAbstractScaleDraw::Labels, false);
    3. sd->enableComponent(QwtAbstractScaleDraw::Ticks, false);
    4. sd->enableComponent(QwtAbstractScaleDraw::Backbone, false);
    To copy to clipboard, switch view to plain text mode 
    Thanks, this did it

    Now, just one little thing, is it possible to display horizontal reference lines like in the image above?

  9. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with histograms

    yes, thats called a "QwtPlotGrid". you have to "enableX(false)", then only horizontal lines are shown.

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.