qwt_plot_histogram.cpp
Go to the documentation of this file.
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 
19 static 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 
38 {
39  public:
41  : baseline( 0.0 )
42  , style( Columns )
43  , symbol( NULL )
44  {
45  }
46 
48  {
49  delete symbol;
50  }
51 
52  double baseline;
53 
54  QPen pen;
55  QBrush brush;
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 
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 
110  legendChanged();
111  itemChanged();
112  }
113 }
114 
120 {
121  return m_data->style;
122 }
123 
137 void QwtPlotHistogram::setPen( const QColor& color, qreal width, Qt::PenStyle style )
138 {
139  setPen( QPen( color, width, style ) );
140 }
141 
148 void QwtPlotHistogram::setPen( const QPen& pen )
149 {
150  if ( pen != m_data->pen )
151  {
152  m_data->pen = pen;
153 
154  legendChanged();
155  itemChanged();
156  }
157 }
158 
163 const QPen& QwtPlotHistogram::pen() const
164 {
165  return m_data->pen;
166 }
167 
174 void QwtPlotHistogram::setBrush( const QBrush& brush )
175 {
176  if ( brush != m_data->brush )
177  {
178  m_data->brush = brush;
179 
180  legendChanged();
181  itemChanged();
182  }
183 }
184 
189 const 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 
215  legendChanged();
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 
291 {
293 }
294 
300  const QVector< QwtIntervalSample >& samples )
301 {
302  setData( new QwtIntervalSeriesData( samples ) );
303 }
304 
317 {
318  setData( data );
319 }
320 
334 void 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 
376 void 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 
461 void 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 
468  const QwtSeriesData< QwtIntervalSample >* series = data();
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 
493 void 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 
502  const QwtSeriesData< QwtIntervalSample >* series = data();
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 
552 void 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 
653 void QwtPlotHistogram::drawColumn( QPainter* painter,
654  const QwtColumnRect& rect, const QwtIntervalSample& sample ) const
655 {
656  Q_UNUSED( sample );
657 
658  if ( m_data->symbol &&
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 
689 {
690  Q_UNUSED( index );
691  return defaultIcon( m_data->brush, size );
692 }
static bool qwtIsCombinable(const QwtInterval &d1, const QwtInterval &d2)
virtual void legendChanged()
double baseline() const
void drawLines(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const QWT_OVERRIDE
bool isValid() const
Definition: qwt_interval.h:210
QwtInterval vInterval
Interval for the vertical coordinates.
void setStyle(HistogramStyle style)
virtual QRectF boundingRect() const =0
const QwtText & title() const
From top to bottom.
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
A class representing an interval.
Definition: qwt_interval.h:22
virtual QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
const QPen & pen() const
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
void setBaseline(double)
Set the value of the baseline.
double minValue() const
Definition: qwt_interval.h:192
Max value is not included in the interval.
Definition: qwt_interval.h:38
PrivateData * m_data
HistogramStyle style() const
From left to right.
A drawing primitive for columns.
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
void setData(QwtSeriesData< QwtIntervalSample > *series)
QwtIntervalSample sample(int index) const
BorderFlags borderFlags() const
Definition: qwt_interval.h:166
void drawColumns(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
const QwtColumnSymbol * symbol
Direction direction
Direction.
void setSamples(const QVector< QwtIntervalSample > &)
virtual ~QwtPlotHistogram()
Destructor.
Qt::Orientation orientation() const
bool isNull() const
Definition: qwt_interval.h:314
QwtSeriesData< QwtIntervalSample > * data()
double transform(double s) const
No Style, the symbol draws nothing.
void drawOutline(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
For QwtPlotHistogram.
void init()
Initialize data members.
The item is represented on the legend.
const QwtColumnSymbol * symbol() const
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
void setInterval(double minValue, double maxValue, BorderFlags=IncludeBorders)
Definition: qwt_interval.h:143
A sample of the types (x1-x2, y) or (x, y1-y2)
Definition: qwt_samples.h:20
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
Min value is not included in the interval.
Definition: qwt_interval.h:35
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
QwtPlotHistogram::HistogramStyle style
void flushPolygon(QPainter *, double baseLine, QPolygonF &) const
Internal, used by the Outline style.
From bottom to top.
virtual void drawColumn(QPainter *, const QwtColumnRect &, const QwtIntervalSample &) const
A scale map.
Definition: qwt_scale_map.h:26
virtual void itemChanged()
const QBrush & brush() const
virtual QRectF boundingRect() const QWT_OVERRIDE
QwtPlotHistogram(const QString &title=QString())
color
Definition: color.h:23
void setBrush(const QBrush &)
Interface for iterating over an array of intervals.
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
static void drawPolyline(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolyline()
Directed rectangle representing bounding rectangle and orientation of a column.
virtual QwtColumnRect columnRect(const QwtIntervalSample &, const QwtScaleMap &, const QwtScaleMap &) const
QwtInterval hInterval
Interval for the horizontal coordinates.
virtual T sample(size_t i) const =0
double value
Value.
Definition: qwt_samples.h:31
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1485
virtual size_t dataSize() const QWT_OVERRIDE
virtual int rtti() const QWT_OVERRIDE
Definition: core.h:1131
Base class for plot items representing a series of samples.
void setItemAttribute(ItemAttribute, bool on=true)
void setSymbol(const QwtColumnSymbol *)
Assign a symbol.
From right to left.
QRectF toRect() const
static bool roundingAlignment()
Definition: qwt_painter.h:183
Definition: format.h:895
double maxValue() const
Definition: qwt_interval.h:198
QwtInterval interval
Interval.
Definition: qwt_samples.h:34
virtual void draw(QPainter *, const QwtColumnRect &) const


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38