Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_plot_histogram.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_histogram.h"
11#include "qwt_painter.h"
12#include "qwt_column_symbol.h"
13#include "qwt_scale_map.h"
14#include "qwt_graphic.h"
15
16#include <qstring.h>
17#include <qpainter.h>
18
19static inline bool qwtIsCombinable( const QwtInterval& d1,
20 const QwtInterval& d2 )
21{
22 if ( d1.isValid() && d2.isValid() )
23 {
24 if ( d1.maxValue() == d2.minValue() )
25 {
28 {
29 return true;
30 }
31 }
32 }
33
34 return false;
35}
36
37class QwtPlotHistogram::PrivateData
38{
39 public:
40 PrivateData()
41 : baseline( 0.0 )
42 , style( Columns )
43 , symbol( NULL )
44 {
45 }
46
47 ~PrivateData()
48 {
49 delete symbol;
50 }
51
52 double baseline;
53
54 QPen pen;
55 QBrush brush;
57 const QwtColumnSymbol* symbol;
58};
59
65 : QwtPlotSeriesItem( title )
66{
67 init();
68}
69
75 : QwtPlotSeriesItem( title )
76{
77 init();
78}
79
82{
83 delete m_data;
84}
85
87void QwtPlotHistogram::init()
88{
89 m_data = new PrivateData();
91
94
95 setZ( 20.0 );
96}
97
105{
106 if ( style != m_data->style )
107 {
108 m_data->style = style;
109
111 itemChanged();
112 }
113}
114
120{
121 return m_data->style;
122}
123
137void QwtPlotHistogram::setPen( const QColor& color, qreal width, Qt::PenStyle style )
138{
139 setPen( QPen( color, width, style ) );
140}
141
148void QwtPlotHistogram::setPen( const QPen& pen )
149{
150 if ( pen != m_data->pen )
151 {
152 m_data->pen = pen;
153
155 itemChanged();
156 }
157}
158
163const QPen& QwtPlotHistogram::pen() const
164{
165 return m_data->pen;
166}
167
174void QwtPlotHistogram::setBrush( const QBrush& brush )
175{
176 if ( brush != m_data->brush )
177 {
178 m_data->brush = brush;
179
181 itemChanged();
182 }
183}
184
189const QBrush& QwtPlotHistogram::brush() const
190{
191 return m_data->brush;
192}
193
209{
210 if ( symbol != m_data->symbol )
211 {
212 delete m_data->symbol;
213 m_data->symbol = symbol;
214
216 itemChanged();
217 }
218}
219
225{
226 return m_data->symbol;
227}
228
241{
242 if ( m_data->baseline != value )
243 {
244 m_data->baseline = value;
245 itemChanged();
246 }
247}
248
254{
255 return m_data->baseline;
256}
257
263{
264 QRectF rect = data()->boundingRect();
265 if ( !rect.isValid() )
266 return rect;
267
268 if ( orientation() == Qt::Horizontal )
269 {
270 rect = QRectF( rect.y(), rect.x(),
271 rect.height(), rect.width() );
272
273 if ( rect.left() > m_data->baseline )
274 rect.setLeft( m_data->baseline );
275 else if ( rect.right() < m_data->baseline )
276 rect.setRight( m_data->baseline );
277 }
278 else
279 {
280 if ( rect.bottom() < m_data->baseline )
281 rect.setBottom( m_data->baseline );
282 else if ( rect.top() > m_data->baseline )
283 rect.setTop( m_data->baseline );
284 }
285
286 return rect;
287}
288
294
300 const QVector< QwtIntervalSample >& samples )
301{
302 setData( new QwtIntervalSeriesData( samples ) );
303}
304
320
334void QwtPlotHistogram::drawSeries( QPainter* painter,
335 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
336 const QRectF& canvasRect, int from, int to ) const
337{
338 Q_UNUSED( canvasRect )
339
340 if ( !painter || dataSize() <= 0 )
341 return;
342
343 if ( to < 0 )
344 to = dataSize() - 1;
345
346 switch ( m_data->style )
347 {
348 case Outline:
349 drawOutline( painter, xMap, yMap, from, to );
350 break;
351 case Lines:
352 drawLines( painter, xMap, yMap, from, to );
353 break;
354 case Columns:
355 drawColumns( painter, xMap, yMap, from, to );
356 break;
357 default:
358 break;
359 }
360}
361
376void QwtPlotHistogram::drawOutline( QPainter* painter,
377 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
378 int from, int to ) const
379{
380 const bool doAlign = QwtPainter::roundingAlignment( painter );
381
382 double v0 = ( orientation() == Qt::Horizontal ) ?
383 xMap.transform( baseline() ) : yMap.transform( baseline() );
384 if ( doAlign )
385 v0 = qRound( v0 );
386
387 QwtIntervalSample previous;
388
389 QPolygonF polygon;
390 for ( int i = from; i <= to; i++ )
391 {
392 const QwtIntervalSample sample = this->sample( i );
393
394 if ( !sample.interval.isValid() )
395 {
396 flushPolygon( painter, v0, polygon );
397 previous = sample;
398 continue;
399 }
400
401 if ( previous.interval.isValid() )
402 {
403 if ( !qwtIsCombinable( previous.interval, sample.interval ) )
404 flushPolygon( painter, v0, polygon );
405 }
406
407 if ( orientation() == Qt::Vertical )
408 {
409 double x1 = xMap.transform( sample.interval.minValue() );
410 double x2 = xMap.transform( sample.interval.maxValue() );
411 double y = yMap.transform( sample.value );
412 if ( doAlign )
413 {
414 x1 = qRound( x1 );
415 x2 = qRound( x2 );
416 y = qRound( y );
417 }
418
419 if ( polygon.size() == 0 )
420 polygon += QPointF( x1, v0 );
421
422 polygon += QPointF( x1, y );
423 polygon += QPointF( x2, y );
424 }
425 else
426 {
427 double y1 = yMap.transform( sample.interval.minValue() );
428 double y2 = yMap.transform( sample.interval.maxValue() );
429 double x = xMap.transform( sample.value );
430 if ( doAlign )
431 {
432 y1 = qRound( y1 );
433 y2 = qRound( y2 );
434 x = qRound( x );
435 }
436
437 if ( polygon.size() == 0 )
438 polygon += QPointF( v0, y1 );
439
440 polygon += QPointF( x, y1 );
441 polygon += QPointF( x, y2 );
442 }
443 previous = sample;
444 }
445
446 flushPolygon( painter, v0, polygon );
447}
448
461void QwtPlotHistogram::drawColumns( QPainter* painter,
462 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
463 int from, int to ) const
464{
465 painter->setPen( m_data->pen );
466 painter->setBrush( m_data->brush );
467
469
470 for ( int i = from; i <= to; i++ )
471 {
472 const QwtIntervalSample sample = series->sample( i );
473 if ( !sample.interval.isNull() )
474 {
475 const QwtColumnRect rect = columnRect( sample, xMap, yMap );
476 drawColumn( painter, rect, sample );
477 }
478 }
479}
480
493void QwtPlotHistogram::drawLines( QPainter* painter,
494 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
495 int from, int to ) const
496{
497 const bool doAlign = QwtPainter::roundingAlignment( painter );
498
499 painter->setPen( m_data->pen );
500 painter->setBrush( Qt::NoBrush );
501
503
504 for ( int i = from; i <= to; i++ )
505 {
506 const QwtIntervalSample sample = series->sample( i );
507 if ( !sample.interval.isNull() )
508 {
509 const QwtColumnRect rect = columnRect( sample, xMap, yMap );
510
511 QRectF r = rect.toRect();
512 if ( doAlign )
513 {
514 r.setLeft( qRound( r.left() ) );
515 r.setRight( qRound( r.right() ) );
516 r.setTop( qRound( r.top() ) );
517 r.setBottom( qRound( r.bottom() ) );
518 }
519
520 switch ( rect.direction )
521 {
523 {
524 QwtPainter::drawLine( painter,
525 r.topRight(), r.bottomRight() );
526 break;
527 }
529 {
530 QwtPainter::drawLine( painter,
531 r.topLeft(), r.bottomLeft() );
532 break;
533 }
535 {
536 QwtPainter::drawLine( painter,
537 r.bottomRight(), r.bottomLeft() );
538 break;
539 }
541 {
542 QwtPainter::drawLine( painter,
543 r.topRight(), r.topLeft() );
544 break;
545 }
546 }
547 }
548 }
549}
550
552void QwtPlotHistogram::flushPolygon( QPainter* painter,
553 double baseLine, QPolygonF& polygon ) const
554{
555 if ( polygon.size() == 0 )
556 return;
557
558 if ( orientation() == Qt::Horizontal )
559 polygon += QPointF( baseLine, polygon.last().y() );
560 else
561 polygon += QPointF( polygon.last().x(), baseLine );
562
563 if ( m_data->brush.style() != Qt::NoBrush )
564 {
565 painter->setPen( Qt::NoPen );
566 painter->setBrush( m_data->brush );
567
568 if ( orientation() == Qt::Horizontal )
569 {
570 polygon += QPointF( polygon.last().x(), baseLine );
571 polygon += QPointF( polygon.first().x(), baseLine );
572 }
573 else
574 {
575 polygon += QPointF( baseLine, polygon.last().y() );
576 polygon += QPointF( baseLine, polygon.first().y() );
577 }
578
579 QwtPainter::drawPolygon( painter, polygon );
580
581 polygon.pop_back();
582 polygon.pop_back();
583 }
584 if ( m_data->pen.style() != Qt::NoPen )
585 {
586 painter->setBrush( Qt::NoBrush );
587 painter->setPen( m_data->pen );
588 QwtPainter::drawPolyline( painter, polygon );
589 }
590 polygon.clear();
591}
592
603 const QwtScaleMap& xMap, const QwtScaleMap& yMap ) const
604{
605 QwtColumnRect rect;
606
607 const QwtInterval& iv = sample.interval;
608 if ( !iv.isValid() )
609 return rect;
610
611 if ( orientation() == Qt::Horizontal )
612 {
613 const double x0 = xMap.transform( baseline() );
614 const double x = xMap.transform( sample.value );
615 const double y1 = yMap.transform( iv.minValue() );
616 const double y2 = yMap.transform( iv.maxValue() );
617
618 rect.hInterval.setInterval( x0, x );
619 rect.vInterval.setInterval( y1, y2, iv.borderFlags() );
620 rect.direction = ( x < x0 ) ? QwtColumnRect::RightToLeft :
622 }
623 else
624 {
625 const double x1 = xMap.transform( iv.minValue() );
626 const double x2 = xMap.transform( iv.maxValue() );
627 const double y0 = yMap.transform( baseline() );
628 const double y = yMap.transform( sample.value );
629
630 rect.hInterval.setInterval( x1, x2, iv.borderFlags() );
631 rect.vInterval.setInterval( y0, y );
632 rect.direction = ( y < y0 ) ? QwtColumnRect::BottomToTop :
634 }
635
636 return rect;
637}
638
653void QwtPlotHistogram::drawColumn( QPainter* painter,
654 const QwtColumnRect& rect, const QwtIntervalSample& sample ) const
655{
656 Q_UNUSED( sample );
657
658 if ( m_data->symbol &&
659 ( m_data->symbol->style() != QwtColumnSymbol::NoStyle ) )
660 {
661 m_data->symbol->draw( painter, rect );
662 }
663 else
664 {
665 QRectF r = rect.toRect();
666 if ( QwtPainter::roundingAlignment( painter ) )
667 {
668 r.setLeft( qRound( r.left() ) );
669 r.setRight( qRound( r.right() ) );
670 r.setTop( qRound( r.top() ) );
671 r.setBottom( qRound( r.bottom() ) );
672 }
673
674 QwtPainter::drawRect( painter, r );
675 }
676}
677
688QwtGraphic QwtPlotHistogram::legendIcon( int index, const QSizeF& size ) const
689{
690 Q_UNUSED( index );
691 return defaultIcon( m_data->brush, size );
692}
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.
QRectF toRect() const
A drawing primitive for columns.
virtual void draw(QPainter *, const QwtColumnRect &) const
@ NoStyle
No Style, the symbol draws nothing.
A paint device for scalable graphics.
Definition qwt_graphic.h:76
A class representing an interval.
void setInterval(double minValue, double maxValue, BorderFlags=IncludeBorders)
double minValue() const
@ ExcludeMaximum
Max value is not included in the interval.
@ ExcludeMinimum
Min value is not included in the interval.
double maxValue() const
BorderFlags borderFlags() const
bool isNull() const
bool isValid() const
A sample of the types (x1-x2, y) or (x, y1-y2)
Definition qwt_samples.h:21
QwtInterval interval
Interval.
Definition qwt_samples.h:34
double value
Value.
Definition qwt_samples.h:31
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
static void drawPolyline(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolyline()
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
virtual QwtColumnRect columnRect(const QwtIntervalSample &, const QwtScaleMap &, const QwtScaleMap &) const
void setBrush(const QBrush &)
HistogramStyle style() const
QwtPlotHistogram(const QString &title=QString())
virtual QwtGraphic legendIcon(int index, const QSizeF &) const override
void setStyle(HistogramStyle style)
void setBaseline(double)
Set the value of the baseline.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
virtual ~QwtPlotHistogram()
Destructor.
void setSamples(const QVector< QwtIntervalSample > &)
void drawLines(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
void drawOutline(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
virtual int rtti() const override
const QPen & pen() const
virtual QRectF boundingRect() const override
void setSymbol(const QwtColumnSymbol *)
Assign a symbol.
const QwtColumnSymbol * symbol() const
void drawColumns(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
const QBrush & brush() const
virtual void drawColumn(QPainter *, const QwtColumnRect &, const QwtIntervalSample &) const
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const override
virtual void legendChanged()
void setZ(double z)
Set the z value.
void setItemAttribute(ItemAttribute, bool on=true)
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
@ Rtti_PlotHistogram
For QwtPlotHistogram.
virtual void itemChanged()
@ Legend
The item is represented on the legend.
Base class for plot items representing a series of samples.
Qt::Orientation orientation() const
A scale map.
double transform(double s) const
virtual QRectF boundingRect() const
virtual T sample(size_t i) const =0
QwtIntervalSample sample(int index) const
virtual size_t dataSize() const override
QwtSeriesData< QwtIntervalSample > * data()
void setData(QwtSeriesData< QwtIntervalSample > *series)
A class representing a text.
Definition qwt_text.h:52