PDA

View Full Version : Curve and horizontal bar chart from same set of samples?



estan
19th September 2016, 10:48
Hi,

I'm experimenting with Qwt for my plotting needs. It's working very well, but I have a question: Is it possible to make a curve plot and a horizontal bar plot from the same set of samples?

E.g. like this:




// Create a test curve
QVector<QPointF> samples;
for (double y = 0.0; y < 1000.0; y += 10.0) {
auto x = 0.1 + 0.08 * std::sin(y / 50.0);
samples.append(QPointF(x, y));
}

// Create a bar chart
auto barChart = new QwtPlotBarChart("Test Bar Chart");
barChart->setSamples(samples);
barChart->setXAxis(QwtPlot::xTop);
barChart->setRenderHint(QwtPlotItem::RenderAntialiased);
//barChart->setOrientation(Qt::Horizontal);
barChart->attach(this);

// Create a curve
auto curve = new QwtPlotCurve("Test Curve");
curve->setSamples(samples);
curve->setXAxis(QwtPlot::xTop);
curve->setRenderHint(QwtPlotItem::RenderAntialiased);
curve->attach(this);

With the above code, I get the result in the attache without_horizontal_alignment.png. If I uncomment the
barChart->setOrientation(Qt::Horizontal) line, I get the result in with_horizontal_alignment.png.

If I *also* switch the x / y values in my samples vector, I can get the correct result for the bar chart item (see working.png), but then the curve is wrong.

I'm looking for a way to use the same vector of samples and get the look I want ("vertical" line chart, but "horizontal" bar chart).

Thanks in advance.

without_horizontal_alignment.png:

12110

with_horizontal_alignment.png:

12111

working.png:

12112

Uwe
19th September 2016, 11:12
Is it possible to make a curve plot and a horizontal bar plot from the same set of samples?
Have you considered to use QwtPlotCurve::Sticks ?

I'm looking for a way to use the same vector of samples and get the look I want ("vertical" line chart, but "horizontal" bar chart).
All setSamples methods are only convenience ending up in setData. So you could derive from QwtPointSeriesData returning a sample with x/y being swapped.

Uwe

estan
19th September 2016, 11:54
Thanks a lot for the quick reply Uwe.


Have you considered to use QwtPlotCurve::Sticks ?

Sorry, my example was not quite complete (should have used setMargin(0) and setSpacing(0) on the bar chart). I actually want the look of a real bar chart, since my data points are measurements taken by a spectrometer over a segment of a rock core, so it makes sense to have actual bars that span over this segment. In the end I also think I want to let the user activate error bars, and I think those looks best on a bar chart.


All setSamples methods are only convenience ending up in setData. So you could derive from QwtPointSeriesData returning a sample with x/y being swapped.

I see, yes in the end I think I'll probably make a custom QwtPointSeriesData which is backed by my real data store (a dataset in a HDF5 file).

It was just initially that I was using a QVector of points because it was easy to generate.

But it's good that you confirm that I'll have to swap x/y in order to get the look I want. I somehow thought I'd be able to get the bars to go horizontally using the same input vector as I used for the curve.

(In my real app, I'm not planning to have the two shown at the same time, but let the user switch between line and bar chart, and it would have been convenient if I could use the exact same input data.)

Thanks for the answers!

Uwe
19th September 2016, 13:20
I actually want the look of a real bar chart, since my data points are measurements taken by a spectrometer over a segment of a rock core, so it makes sense to have actual bars that span over this segment.

QwtPlotHistogram might be an option here too. It is different to a bar chart as it displays [x1-x2, y] type of samples.

Uwe

estan
20th September 2016, 06:49
QwtPlotHistogram might be an option here too. It is different to a bar chart as it displays [x1-x2, y] type of samples.

Ah, thanks. I'll have a look at that too.