qwt_plot_histogram.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 d_data;
84 }
85 
88 {
89  d_data = new PrivateData();
91 
94 
95  setZ( 20.0 );
96 }
97 
105 {
106  if ( style != d_data->style )
107  {
108  d_data->style = style;
109 
110  legendChanged();
111  itemChanged();
112  }
113 }
114 
120 {
121  return d_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 != d_data->pen )
151  {
152  d_data->pen = pen;
153 
154  legendChanged();
155  itemChanged();
156  }
157 }
158 
163 const QPen &QwtPlotHistogram::pen() const
164 {
165  return d_data->pen;
166 }
167 
174 void QwtPlotHistogram::setBrush( const QBrush &brush )
175 {
176  if ( brush != d_data->brush )
177  {
178  d_data->brush = brush;
179 
180  legendChanged();
181  itemChanged();
182  }
183 }
184 
189 const QBrush &QwtPlotHistogram::brush() const
190 {
191  return d_data->brush;
192 }
193 
209 {
210  if ( symbol != d_data->symbol )
211  {
212  delete d_data->symbol;
213  d_data->symbol = symbol;
214 
215  legendChanged();
216  itemChanged();
217  }
218 }
219 
225 {
226  return d_data->symbol;
227 }
228 
241 {
242  if ( d_data->baseline != value )
243  {
244  d_data->baseline = value;
245  itemChanged();
246  }
247 }
248 
254 {
255  return d_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() > d_data->baseline )
274  rect.setLeft( d_data->baseline );
275  else if ( rect.right() < d_data->baseline )
276  rect.setRight( d_data->baseline );
277  }
278  else
279  {
280  if ( rect.bottom() < d_data->baseline )
281  rect.setBottom( d_data->baseline );
282  else if ( rect.top() > d_data->baseline )
283  rect.setTop( d_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 ( d_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( d_data->pen );
466  painter->setBrush( d_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( d_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 ( d_data->brush.style() != Qt::NoBrush )
564  {
565  painter->setPen( Qt::NoPen );
566  painter->setBrush( d_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 ( d_data->pen.style() != Qt::NoPen )
585  {
586  painter->setBrush( Qt::NoBrush );
587  painter->setPen( d_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 ( d_data->symbol &&
660  {
661  d_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 
688 QwtGraphic QwtPlotHistogram::legendIcon( int index, const QSizeF &size ) const
689 {
690  Q_UNUSED( index );
691  return defaultIcon( d_data->brush, size );
692 }
static bool qwtIsCombinable(const QwtInterval &d1, const QwtInterval &d2)
virtual void legendChanged()
Qt::Orientation orientation() const
enum MQTTPropertyCodes value
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const QWT_OVERRIDE
Style style() const
QwtInterval vInterval
Interval for the vertical coordinates.
void setStyle(HistogramStyle style)
PrivateData * d_data
virtual QRectF boundingRect() const =0
From top to bottom.
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:152
A class representing an interval.
Definition: qwt_interval.h:22
virtual QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
void setBaseline(double)
Set the value of the baseline.
Max value is not included in the interval.
Definition: qwt_interval.h:38
virtual void drawColumn(QPainter *, const QwtColumnRect &, const QwtIntervalSample &) const
double minValue() const
Definition: qwt_interval.h:190
QRectF toRect() const
From left to right.
A drawing primitive for columns.
void drawLines(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
void setData(QwtSeriesData< QwtIntervalSample > *series)
const QwtColumnSymbol * symbol
Direction direction
Direction.
void setSamples(const QVector< QwtIntervalSample > &)
virtual ~QwtPlotHistogram()
Destructor.
QwtSeriesData< QwtIntervalSample > * data()
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
double maxValue() const
Definition: qwt_interval.h:196
No Style, the symbol draws nothing.
For QwtPlotHistogram.
void drawColumns(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
HistogramStyle style() const
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:141
A sample of the types (x1-x2, y) or (x, y1-y2)
Definition: qwt_samples.h:20
bool isValid() const
Definition: qwt_interval.h:208
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
bool isNull() const
Definition: qwt_interval.h:312
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
From bottom to top.
A scale map.
Definition: qwt_scale_map.h:26
BorderFlags borderFlags() const
Definition: qwt_interval.h:164
QwtIntervalSample sample(int index) const
virtual void itemChanged()
virtual QRectF boundingRect() const QWT_OVERRIDE
QwtPlotHistogram(const QString &title=QString())
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()
void drawOutline(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
Directed rectangle representing bounding rectangle and orientation of a column.
virtual void draw(QPainter *, const QwtColumnRect &) const
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
const QBrush & brush() const
double value
Value.
Definition: qwt_samples.h:31
virtual size_t dataSize() const QWT_OVERRIDE
virtual int rtti() const QWT_OVERRIDE
Base class for plot items representing a series of samples.
void setItemAttribute(ItemAttribute, bool on=true)
double transform(double s) const
void setSymbol(const QwtColumnSymbol *)
Assign a symbol.
From right to left.
const QPen & pen() const
void flushPolygon(QPainter *, double baseLine, QPolygonF &) const
Internal, used by the Outline style.
double baseline() const
static bool roundingAlignment()
Definition: qwt_painter.h:181
const QwtText & title() const
QwtInterval interval
Interval.
Definition: qwt_samples.h:34


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10