PDA

View Full Version : Help with histograms



Patrik
9th December 2010, 14:39
Hi, I need to plot a histogram but I do not know how. Documentation was not helpful enough. Here is my code


void Plot(QwtPlot *plt)
{
plt->setTitle(dataName);
plt->setAxisTitle(QwtPlot::xBottom, "Label");
plt->setAxisTitle(QwtPlot::yLeft, "Values");
QwtPlotHistogram *hist = new QwtPlotHistogram("blabla");
QVector<QwtIntervalSample> data;
data.append(QwtIntervalSample(1,0,4));
data.append(QwtIntervalSample(3,0,7));
hist->setSamples(data);
hist->attach(plt);
}
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
http://dl.dropbox.com/u/9287376/example.jpg

Any help is appreciated

Uwe
10th December 2010, 09:35
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.

Patrik
10th December 2010, 17:03
Thanks, I'll try

Patrik
21st December 2010, 13:33
I've writter a little class to be used as QwtScaleDraw.


#ifndef _BARCHARTSCALEDRAW_
#define _BARCHARTSCALEDRAW_
#include <QMap>
#include <qwt_scale_draw.h>

class BarChartScaleDraw: public QwtScaleDraw
{
public:
BarChartScaleDraw(QMap<double,QString> *x) : ids(x) { }

virtual QwtText label(double v) const
{
if (ids->contains(v))
return ids->find(v).value();
else
return "";
}
private:
QMap<double,QString> *ids;
};

#endif

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?

FelixB
21st December 2010, 13:37
I'm not sure which "notches" you mean, maybe it's called "ticks" in qwt.

this removes everything:

QwtScaleDraw *sd = myPlot->axisScaleDraw(QwtPlot::xBottom);
sd->enableComponent(QwtAbstractScaleDraw::Labels, false);
sd->enableComponent(QwtAbstractScaleDraw::Ticks, false);
sd->enableComponent(QwtAbstractScaleDraw::Backbone, false);

if you want to remove just the small ticks, use this:

myPlot->setAxisMaxMinor(QwtPlot::yLeft, 0);

Patrik
21st December 2010, 14:58
I'm not sure which "notches" you mean, maybe it's called "ticks" in qwt. Yes, ticks. Sorry, my bad


this removes everything:

QwtScaleDraw *sd = myPlot->axisScaleDraw(QwtPlot::xBottom);
sd->enableComponent(QwtAbstractScaleDraw::Labels, false);
sd->enableComponent(QwtAbstractScaleDraw::Ticks, false);
sd->enableComponent(QwtAbstractScaleDraw::Backbone, false);Thanks, this did it

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

FelixB
21st December 2010, 15:09
yes, thats called a "QwtPlotGrid". you have to "enableX(false)", then only horizontal lines are shown.