Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_plot_barchart.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_plot_barchart.h"
11#include "qwt_scale_map.h"
12#include "qwt_column_symbol.h"
13#include "qwt_text.h"
14#include "qwt_graphic.h"
15#include "qwt_legend_data.h"
16
17#include <qpainter.h>
18
19class QwtPlotBarChart::PrivateData
20{
21 public:
22 PrivateData()
23 : symbol( NULL )
24 , legendMode( QwtPlotBarChart::LegendChartTitle )
25 {
26 }
27
28 ~PrivateData()
29 {
30 delete symbol;
31 }
32
33 QwtColumnSymbol* symbol;
35};
36
43{
44 init();
45}
46
51QwtPlotBarChart::QwtPlotBarChart( const QString& title )
53{
54 init();
55}
56
59{
60 delete m_data;
61}
62
63void QwtPlotBarChart::init()
64{
65 m_data = new PrivateData;
67}
68
74
83 const QVector< QPointF >& samples )
84{
85 setData( new QwtPointSeriesData( samples ) );
86}
87
98 const QVector< double >& samples )
99{
100 QVector< QPointF > points;
101 points.reserve( samples.size() );
102
103 for ( int i = 0; i < samples.size(); i++ )
104 points += QPointF( i, samples[ i ] );
105
106 setData( new QwtPointSeriesData( points ) );
107}
108
123
135{
136 if ( symbol != m_data->symbol )
137 {
138 delete m_data->symbol;
139 m_data->symbol = symbol;
140
142 itemChanged();
143 }
144}
145
151{
152 return m_data->symbol;
153}
154
165{
166 if ( mode != m_data->legendMode )
167 {
168 m_data->legendMode = mode;
170 }
171}
172
178{
179 return m_data->legendMode;
180}
181
187{
188 const size_t numSamples = dataSize();
189 if ( numSamples == 0 )
191
192 QRectF rect = QwtPlotSeriesItem::boundingRect();
193 if ( rect.height() >= 0 )
194 {
195 const double baseLine = baseline();
196
197 if ( rect.bottom() < baseLine )
198 rect.setBottom( baseLine );
199
200 if ( rect.top() > baseLine )
201 rect.setTop( baseLine );
202 }
203
204 if ( orientation() == Qt::Horizontal )
205 rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
206
207 return rect;
208}
209
223void QwtPlotBarChart::drawSeries( QPainter* painter,
224 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
225 const QRectF& canvasRect, int from, int to ) const
226{
227 if ( to < 0 )
228 to = dataSize() - 1;
229
230 if ( from < 0 )
231 from = 0;
232
233 if ( from > to )
234 return;
235
236
237 const QRectF br = data()->boundingRect();
238 const QwtInterval interval( br.left(), br.right() );
239
240 painter->save();
241
242 for ( int i = from; i <= to; i++ )
243 {
244 drawSample( painter, xMap, yMap,
245 canvasRect, interval, i, sample( i ) );
246 }
247
248 painter->restore();
249}
250
263 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
264 const QRectF& canvasRect, const QwtInterval& boundingInterval,
265 const QPointF& sample ) const
266{
267 QwtColumnRect barRect;
268
269 if ( orientation() == Qt::Horizontal )
270 {
271 const double barHeight = sampleWidth( yMap, canvasRect.height(),
272 boundingInterval.width(), sample.y() );
273
274 const double x1 = xMap.transform( baseline() );
275 const double x2 = xMap.transform( sample.y() );
276
277 const double y = yMap.transform( sample.x() );
278 const double y1 = y - 0.5 * barHeight;
279 const double y2 = y + 0.5 * barHeight;
280
281 barRect.direction = ( x1 < x2 ) ?
283
284 barRect.hInterval = QwtInterval( x1, x2 ).normalized();
285 barRect.vInterval = QwtInterval( y1, y2 );
286 }
287 else
288 {
289 const double barWidth = sampleWidth( xMap, canvasRect.width(),
290 boundingInterval.width(), sample.y() );
291
292 const double x = xMap.transform( sample.x() );
293 const double x1 = x - 0.5 * barWidth;
294 const double x2 = x + 0.5 * barWidth;
295
296 const double y1 = yMap.transform( baseline() );
297 const double y2 = yMap.transform( sample.y() );
298
299 barRect.direction = ( y1 < y2 ) ?
301
302 barRect.hInterval = QwtInterval( x1, x2 );
303 barRect.vInterval = QwtInterval( y1, y2 ).normalized();
304 }
305
306 return barRect;
307}
308
322void QwtPlotBarChart::drawSample( QPainter* painter,
323 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
324 const QRectF& canvasRect, const QwtInterval& boundingInterval,
325 int index, const QPointF& sample ) const
326{
327 const QwtColumnRect barRect = columnRect( xMap, yMap,
328 canvasRect, boundingInterval, sample );
329
330 drawBar( painter, index, sample, barRect );
331}
332
341void QwtPlotBarChart::drawBar( QPainter* painter,
342 int sampleIndex, const QPointF& sample,
343 const QwtColumnRect& rect ) const
344{
345 const QwtColumnSymbol* specialSym =
346 specialSymbol( sampleIndex, sample );
347
348 const QwtColumnSymbol* sym = specialSym;
349 if ( sym == NULL )
350 sym = m_data->symbol;
351
352 if ( sym )
353 {
354 sym->draw( painter, rect );
355 }
356 else
357 {
358 // we build a temporary default symbol
360 columnSymbol.setLineWidth( 1 );
362 columnSymbol.draw( painter, rect );
363 }
364
365 delete specialSym;
366}
367
378 int sampleIndex, const QPointF& sample ) const
379{
380 Q_UNUSED( sampleIndex );
381 Q_UNUSED( sample );
382
383 return NULL;
384}
385
399QwtText QwtPlotBarChart::barTitle( int sampleIndex ) const
400{
401 Q_UNUSED( sampleIndex );
402 return QwtText();
403}
404
417{
419
420 if ( m_data->legendMode == LegendBarTitles )
421 {
422 const size_t numSamples = dataSize();
423 list.reserve( numSamples );
424
425 for ( size_t i = 0; i < numSamples; i++ )
426 {
428
429 data.setValue( QwtLegendData::TitleRole,
430 QVariant::fromValue( barTitle( i ) ) );
431
432 if ( !legendIconSize().isEmpty() )
433 {
434 data.setValue( QwtLegendData::IconRole,
435 QVariant::fromValue( legendIcon( i, legendIconSize() ) ) );
436 }
437
438 list += data;
439 }
440 }
441 else
442 {
444 }
445
446 return list;
447}
448
463 int index, const QSizeF& size ) const
464{
465 QwtColumnRect column;
466 column.hInterval = QwtInterval( 0.0, size.width() - 1.0 );
467 column.vInterval = QwtInterval( 0.0, size.height() - 1.0 );
468
469 QwtGraphic icon;
470 icon.setDefaultSize( size );
472
473 QPainter painter( &icon );
474 painter.setRenderHint( QPainter::Antialiasing,
476
477 int barIndex = -1;
478 if ( m_data->legendMode == QwtPlotBarChart::LegendBarTitles )
479 barIndex = index;
480
481 drawBar( &painter, barIndex, QPointF(), column );
482
483 return icon;
484}
Template class for data, that is organized as QVector.
Directed rectangle representing bounding rectangle and orientation of a column.
QwtInterval vInterval
Interval for the vertical coordinates.
QwtInterval hInterval
Interval for the horizontal coordinates.
@ LeftToRight
From left to right.
@ TopToBottom
From top to bottom.
@ BottomToTop
From bottom to top.
@ RightToLeft
From right to left.
Direction direction
Direction.
A drawing primitive for columns.
@ Plain
A plain frame style.
virtual void draw(QPainter *, const QwtColumnRect &) const
void setFrameStyle(FrameStyle)
void setLineWidth(int width)
A paint device for scalable graphics.
Definition qwt_graphic.h:76
@ RenderPensUnscaled
Definition qwt_graphic.h:96
void setRenderHint(RenderHint, bool on=true)
void setDefaultSize(const QSizeF &)
Set a default size.
A class representing an interval.
QwtInterval normalized() const
Normalize the limits of the interval.
double width() const
Return the width of an interval.
Attributes of an entry on a legend.
Abstract base class for bar chart items.
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
QwtPlotBarChart displays a series of a values as bars.
QwtPlotBarChart(const QString &title=QString())
void setLegendMode(LegendMode)
LegendMode legendMode() const
void setSymbol(QwtColumnSymbol *)
Assign a symbol.
virtual QwtColumnSymbol * specialSymbol(int sampleIndex, const QPointF &) const
LegendMode
Legend modes.
QList< QwtLegendData > legendData() const override
Return all information, that is needed to represent the item on the legend.
void setSamples(const QVector< QPointF > &)
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const override
virtual int rtti() const override
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
const QwtColumnSymbol * symbol() const
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &sample, const QwtColumnRect &) const
QwtColumnRect columnRect(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, const QPointF &sample) const
virtual ~QwtPlotBarChart()
Destructor.
virtual QRectF boundingRect() const override
QwtGraphic legendIcon(int index, const QSizeF &) const override
virtual void drawSample(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, int index, const QPointF &sample) const
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
virtual void legendChanged()
@ Rtti_PlotBarChart
For QwtPlotBarChart.
@ RenderAntialiased
Enable antialiasing.
bool testRenderHint(RenderHint) const
virtual void itemChanged()
QSize legendIconSize() const
Qt::Orientation orientation() const
virtual QRectF boundingRect() const override
A scale map.
double transform(double s) const
virtual QRectF boundingRect() const
QPointF sample(int index) const
virtual size_t dataSize() const override
QwtSeriesData< QPointF > * data()
void setData(QwtSeriesData< QPointF > *series)
A class representing a text.
Definition qwt_text.h:52