Qwt User's Guide: qwt_plot_barchart.cpp Source File

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;
34 QwtPlotBarChart::LegendMode legendMode;
35};
36
41 QwtPlotBarChart::QwtPlotBarChart( const QwtText& title )
42 : QwtPlotAbstractBarChart( title )
43{
44 init();
45}
46
51QwtPlotBarChart::QwtPlotBarChart( const QString& title )
52 : QwtPlotAbstractBarChart( QwtText( title ) )
53{
54 init();
55}
56
58QwtPlotBarChart::~QwtPlotBarChart()
59{
60 delete m_data;
61}
62
63void QwtPlotBarChart::init()
64{
65 m_data = new PrivateData;
66 setData( new QwtPointSeriesData() );
67}
68
70 int QwtPlotBarChart::rtti() const
71{
72 return QwtPlotItem::Rtti_PlotBarChart;
73}
74
82 void QwtPlotBarChart::setSamples(
83 const QVector< QPointF >& samples )
84{
85 setData( new QwtPointSeriesData( samples ) );
86}
87
97 void QwtPlotBarChart::setSamples(
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
119 void QwtPlotBarChart::setSamples( QwtSeriesData< QPointF >* data )
120{
121 setData( data );
122}
123
134 void QwtPlotBarChart::setSymbol( QwtColumnSymbol* symbol )
135{
136 if ( symbol != m_data->symbol )
137 {
138 delete m_data->symbol;
139 m_data->symbol = symbol;
140
141 legendChanged();
142 itemChanged();
143 }
144}
145
150 const QwtColumnSymbol* QwtPlotBarChart::symbol() const
151{
152 return m_data->symbol;
153}
154
164 void QwtPlotBarChart::setLegendMode( LegendMode mode )
165{
166 if ( mode != m_data->legendMode )
167 {
168 m_data->legendMode = mode;
169 legendChanged();
170 }
171}
172
177 QwtPlotBarChart::LegendMode QwtPlotBarChart::legendMode() const
178{
179 return m_data->legendMode;
180}
181
186QRectF QwtPlotBarChart::boundingRect() const
187{
188 const size_t numSamples = dataSize();
189 if ( numSamples == 0 )
190 return QwtPlotSeriesItem::boundingRect();
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
223 void 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
262 QwtColumnRect QwtPlotBarChart::columnRect(
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 ) ?
282 QwtColumnRect::LeftToRight : QwtColumnRect::RightToLeft;
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 ) ?
300 QwtColumnRect::TopToBottom : QwtColumnRect::BottomToTop;
301
302 barRect.hInterval = QwtInterval( x1, x2 );
303 barRect.vInterval = QwtInterval( y1, y2 ).normalized();
304 }
305
306 return barRect;
307}
308
322 void 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
341 void 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
359 QwtColumnSymbol columnSymbol( QwtColumnSymbol::Box );
360 columnSymbol.setLineWidth( 1 );
361 columnSymbol.setFrameStyle( QwtColumnSymbol::Plain );
362 columnSymbol.draw( painter, rect );
363 }
364
365 delete specialSym;
366}
367
377 QwtColumnSymbol* QwtPlotBarChart::specialSymbol(
378 int sampleIndex, const QPointF& sample ) const
379{
380 Q_UNUSED( sampleIndex );
381 Q_UNUSED( sample );
382
383 return NULL;
384}
385
399 QwtText QwtPlotBarChart::barTitle( int sampleIndex ) const
400{
401 Q_UNUSED( sampleIndex );
402 return QwtText();
403}
404
416 QList< QwtLegendData > QwtPlotBarChart::legendData() const
417{
418 QList< QwtLegendData > list;
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 {
427 QwtLegendData data;
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 {
443 return QwtPlotAbstractBarChart::legendData();
444 }
445
446 return list;
447}
448
462 QwtGraphic QwtPlotBarChart::legendIcon(
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 );
471 icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
472
473 QPainter painter( &icon );
474 painter.setRenderHint( QPainter::Antialiasing,
475 testRenderHint( QwtPlotItem::RenderAntialiased ) );
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}
QList
Definition qwt_abstract_legend.h:17
QVector
Definition qwt_clipper.h:23
QwtArraySeriesData
Template class for data, that is organized as QVector.
Definition qwt_series_data.h:153
QwtColumnRect
Directed rectangle representing bounding rectangle and orientation of a column.
Definition qwt_column_symbol.h:27
QwtColumnRect::vInterval
QwtInterval vInterval
Interval for the vertical coordinates.
Definition qwt_column_symbol.h:67
QwtColumnRect::hInterval
QwtInterval hInterval
Interval for the horizontal coordinates.
Definition qwt_column_symbol.h:64
QwtColumnRect::LeftToRight
@ LeftToRight
From left to right.
Definition qwt_column_symbol.h:33
QwtColumnRect::TopToBottom
@ TopToBottom
From top to bottom.
Definition qwt_column_symbol.h:42
QwtColumnRect::BottomToTop
@ BottomToTop
From bottom to top.
Definition qwt_column_symbol.h:39
QwtColumnRect::RightToLeft
@ RightToLeft
From right to left.
Definition qwt_column_symbol.h:36
QwtColumnRect::direction
Direction direction
Direction.
Definition qwt_column_symbol.h:70
QwtColumnSymbol
A drawing primitive for columns.
Definition qwt_column_symbol.h:75
QwtColumnSymbol::Plain
@ Plain
A plain frame style.
Definition qwt_column_symbol.h:110
QwtColumnSymbol::draw
virtual void draw(QPainter *, const QwtColumnRect &) const
Definition qwt_column_symbol.cpp:238
QwtColumnSymbol::Box
@ Box
Definition qwt_column_symbol.h:90
QwtColumnSymbol::setFrameStyle
void setFrameStyle(FrameStyle)
Definition qwt_column_symbol.cpp:193
QwtColumnSymbol::setLineWidth
void setLineWidth(int width)
Definition qwt_column_symbol.cpp:213
QwtGraphic
A paint device for scalable graphics.
Definition qwt_graphic.h:76
QwtGraphic::RenderPensUnscaled
@ RenderPensUnscaled
Definition qwt_graphic.h:96
QwtGraphic::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition qwt_graphic.cpp:443
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition qwt_graphic.cpp:553
QwtInterval
A class representing an interval.
Definition qwt_interval.h:23
QwtInterval::normalized
QwtInterval normalized() const
Normalize the limits of the interval.
Definition qwt_interval.cpp:29
QwtInterval::width
double width() const
Return the width of an interval.
Definition qwt_interval.h:227
QwtLegendData
Attributes of an entry on a legend.
Definition qwt_legend_data.h:37
QwtPlotAbstractBarChart
Abstract base class for bar chart items.
Definition qwt_plot_abstract_barchart.h:25
QwtPlotAbstractBarChart::sampleWidth
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
Definition qwt_plot_abstract_barchart.cpp:225
QwtPlotAbstractBarChart::baseline
double baseline() const
Definition qwt_plot_abstract_barchart.cpp:208
QwtPlotBarChart
QwtPlotBarChart displays a series of a values as bars.
Definition qwt_plot_barchart.h:44
QwtPlotBarChart::QwtPlotBarChart
QwtPlotBarChart(const QString &title=QString())
Definition qwt_plot_barchart.cpp:51
QwtPlotBarChart::setLegendMode
void setLegendMode(LegendMode)
Definition qwt_plot_barchart.cpp:164
QwtPlotBarChart::legendMode
LegendMode legendMode() const
Definition qwt_plot_barchart.cpp:177
QwtPlotBarChart::setSymbol
void setSymbol(QwtColumnSymbol *)
Assign a symbol.
Definition qwt_plot_barchart.cpp:134
QwtPlotBarChart::specialSymbol
virtual QwtColumnSymbol * specialSymbol(int sampleIndex, const QPointF &) const
Definition qwt_plot_barchart.cpp:377
QwtPlotBarChart::LegendMode
LegendMode
Legend modes.
Definition qwt_plot_barchart.h:53
QwtPlotBarChart::LegendBarTitles
@ LegendBarTitles
Definition qwt_plot_barchart.h:68
QwtPlotBarChart::LegendChartTitle
@ LegendChartTitle
Definition qwt_plot_barchart.h:60
QwtPlotBarChart::legendData
QList< QwtLegendData > legendData() const override
Return all information, that is needed to represent the item on the legend.
Definition qwt_plot_barchart.cpp:416
QwtPlotBarChart::setSamples
void setSamples(const QVector< QPointF > &)
Definition qwt_plot_barchart.cpp:82
QwtPlotBarChart::drawSeries
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const override
Definition qwt_plot_barchart.cpp:223
QwtPlotBarChart::rtti
virtual int rtti() const override
Definition qwt_plot_barchart.cpp:70
QwtPlotBarChart::barTitle
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
Definition qwt_plot_barchart.cpp:399
QwtPlotBarChart::symbol
const QwtColumnSymbol * symbol() const
Definition qwt_plot_barchart.cpp:150
QwtPlotBarChart::drawBar
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &sample, const QwtColumnRect &) const
Definition qwt_plot_barchart.cpp:341
QwtPlotBarChart::columnRect
QwtColumnRect columnRect(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, const QPointF &sample) const
Definition qwt_plot_barchart.cpp:262
QwtPlotBarChart::~QwtPlotBarChart
virtual ~QwtPlotBarChart()
Destructor.
Definition qwt_plot_barchart.cpp:58
QwtPlotBarChart::boundingRect
virtual QRectF boundingRect() const override
Definition qwt_plot_barchart.cpp:186
QwtPlotBarChart::legendIcon
QwtGraphic legendIcon(int index, const QSizeF &) const override
Definition qwt_plot_barchart.cpp:462
QwtPlotBarChart::drawSample
virtual void drawSample(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, int index, const QPointF &sample) const
Definition qwt_plot_barchart.cpp:322
QwtPlotItem::legendData
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
Definition qwt_plot_item.cpp:626
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition qwt_plot_item.cpp:491
QwtPlotItem::Rtti_PlotBarChart
@ Rtti_PlotBarChart
For QwtPlotBarChart.
Definition qwt_plot_item.h:114
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition qwt_plot_item.h:206
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition qwt_plot_item.cpp:332
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::legendIconSize
QSize legendIconSize() const
Definition qwt_plot_item.cpp:386
QwtPlotSeriesItem::orientation
Qt::Orientation orientation() const
Definition qwt_plot_seriesitem.cpp:77
QwtPlotSeriesItem::boundingRect
virtual QRectF boundingRect() const override
Definition qwt_plot_seriesitem.cpp:97
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtSeriesData< QPointF >
QwtSeriesData::boundingRect
virtual QRectF boundingRect() const
Definition qwt_series_data.h:100
QwtSeriesStore< QPointF >::sample
QPointF sample(int index) const
Definition qwt_series_store.h:158
QwtSeriesStore< QPointF >::dataSize
virtual size_t dataSize() const override
Definition qwt_series_store.h:175
QwtSeriesStore< QPointF >::data
QwtSeriesData< QPointF > * data()
Definition qwt_series_store.h:146
QwtSeriesStore< QPointF >::setData
void setData(QwtSeriesData< QPointF > *series)
Definition qwt_series_store.h:164
QwtText
A class representing a text.
Definition qwt_text.h:52