qwt_plot_barchart.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_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 
20 {
21  public:
22  PrivateData()
23  : symbol( NULL )
25  {
26  }
27 
28  ~PrivateData()
29  {
30  delete symbol;
31  }
32 
35 };
36 
42  : QwtPlotAbstractBarChart( title )
43 {
44  init();
45 }
46 
51 QwtPlotBarChart::QwtPlotBarChart( const QString& title )
52  : QwtPlotAbstractBarChart( QwtText( title ) )
53 {
54  init();
55 }
56 
59 {
60  delete m_data;
61 }
62 
64 {
65  m_data = new PrivateData;
66  setData( new QwtPointSeriesData() );
67 }
68 
71 {
73 }
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 
120 {
121  setData( data );
122 }
123 
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 
151 {
152  return m_data->symbol;
153 }
154 
165 {
166  if ( mode != m_data->legendMode )
167  {
168  m_data->legendMode = mode;
169  legendChanged();
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 
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 
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 
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 
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 
417 {
419 
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;
479  barIndex = index;
480 
481  drawBar( &painter, barIndex, QPointF(), column );
482 
483  return icon;
484 }
qwt_graphic.h
QwtLegendData
Attributes of an entry on a legend.
Definition: qwt_legend_data.h:36
qwt_column_symbol.h
QwtPlotBarChart::QwtPlotBarChart
QwtPlotBarChart(const QString &title=QString())
Definition: qwt_plot_barchart.cpp:51
qwt_plot_barchart.h
QwtPlotBarChart::specialSymbol
virtual QwtColumnSymbol * specialSymbol(int sampleIndex, const QPointF &) const
Definition: qwt_plot_barchart.cpp:377
QwtColumnRect::vInterval
QwtInterval vInterval
Interval for the vertical coordinates.
Definition: qwt_column_symbol.h:67
QwtSeriesStore< QPointF >::sample
QPointF sample(int index) const
Definition: qwt_series_store.h:158
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
QwtPlotSeriesItem::orientation
Qt::Orientation orientation() const
Definition: qwt_plot_seriesitem.cpp:77
QwtPlotBarChart::drawSeries
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const QWT_OVERRIDE
Definition: qwt_plot_barchart.cpp:223
QwtPlotBarChart::~QwtPlotBarChart
virtual ~QwtPlotBarChart()
Destructor.
Definition: qwt_plot_barchart.cpp:58
QwtColumnRect::LeftToRight
@ LeftToRight
From left to right.
Definition: qwt_column_symbol.h:33
QwtPlotBarChart::symbol
const QwtColumnSymbol * symbol() const
Definition: qwt_plot_barchart.cpp:150
QwtPlotAbstractBarChart::baseline
double baseline() const
Definition: qwt_plot_abstract_barchart.cpp:208
QwtGraphic::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition: qwt_graphic.cpp:443
QVector
Definition: qwt_clipper.h:23
QwtPlotBarChart::PrivateData
Definition: qwt_plot_barchart.cpp:19
QwtSeriesStore< QPointF >::setData
void setData(QwtSeriesData< QPointF > *series)
Definition: qwt_series_store.h:164
QwtColumnSymbol
A drawing primitive for columns.
Definition: qwt_column_symbol.h:74
QwtPlotBarChart::LegendChartTitle
@ LegendChartTitle
Definition: qwt_plot_barchart.h:60
QwtLegendData::TitleRole
@ TitleRole
Definition: qwt_legend_data.h:59
QwtColumnSymbol::Plain
@ Plain
A plain frame style.
Definition: qwt_column_symbol.h:110
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QList
Definition: qwt_abstract_legend.h:17
qwt_legend_data.h
QwtColumnRect::direction
Direction direction
Direction.
Definition: qwt_column_symbol.h:70
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtPlotBarChart::LegendBarTitles
@ LegendBarTitles
Definition: qwt_plot_barchart.h:68
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
QwtPlotBarChart::setLegendMode
void setLegendMode(LegendMode)
Definition: qwt_plot_barchart.cpp:164
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
QwtGraphic::RenderPensUnscaled
@ RenderPensUnscaled
Definition: qwt_graphic.h:96
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:22
qwt_scale_map.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtInterval::width
double width() const
Return the width of an interval.
Definition: qwt_interval.h:227
QwtColumnRect::BottomToTop
@ BottomToTop
From bottom to top.
Definition: qwt_column_symbol.h:39
QwtPlotBarChart::legendIcon
QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
Definition: qwt_plot_barchart.cpp:462
QwtColumnRect::TopToBottom
@ TopToBottom
From top to bottom.
Definition: qwt_column_symbol.h:42
QwtSeriesData::boundingRect
virtual QRectF boundingRect() const
Definition: qwt_series_data.h:88
QwtSeriesData< QPointF >
QwtPlotBarChart::init
void init()
Definition: qwt_plot_barchart.cpp:63
QwtPlotBarChart::PrivateData::~PrivateData
~PrivateData()
Definition: qwt_plot_barchart.cpp:35
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
QwtColumnRect::hInterval
QwtInterval hInterval
Interval for the horizontal coordinates.
Definition: qwt_column_symbol.h:64
QwtPlotBarChart::m_data
PrivateData * m_data
Definition: qwt_plot_barchart.h:120
QwtSeriesStore< QPointF >::data
QwtSeriesData< QPointF > * data()
Definition: qwt_series_store.h:146
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtSeriesStore< QPointF >::dataSize
virtual size_t dataSize() const QWT_OVERRIDE
Definition: qwt_series_store.h:175
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtColumnSymbol::Box
@ Box
Definition: qwt_column_symbol.h:90
QwtColumnSymbol::setFrameStyle
void setFrameStyle(FrameStyle)
Definition: qwt_column_symbol.cpp:193
QwtPlotBarChart::setSamples
void setSamples(const QVector< QPointF > &)
Definition: qwt_plot_barchart.cpp:82
QwtPlotBarChart::setSymbol
void setSymbol(QwtColumnSymbol *)
Assign a symbol.
Definition: qwt_plot_barchart.cpp:134
QwtColumnRect
Directed rectangle representing bounding rectangle and orientation of a column.
Definition: qwt_column_symbol.h:26
QwtPlotBarChart::barTitle
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
Definition: qwt_plot_barchart.cpp:399
QwtPlotBarChart::boundingRect
virtual QRectF boundingRect() const QWT_OVERRIDE
Definition: qwt_plot_barchart.cpp:186
QwtPlotBarChart::PrivateData::symbol
QwtColumnSymbol * symbol
Definition: qwt_plot_barchart.cpp:40
QwtInterval::normalized
QwtInterval normalized() const
Normalize the limits of the interval.
Definition: qwt_interval.cpp:29
QwtPlotAbstractBarChart
Abstract base class for bar chart items.
Definition: qwt_plot_abstract_barchart.h:24
QwtPlotBarChart::LegendMode
LegendMode
Legend modes.
Definition: qwt_plot_barchart.h:52
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
QwtPlotSeriesItem::boundingRect
virtual QRectF boundingRect() const QWT_OVERRIDE
Definition: qwt_plot_seriesitem.cpp:97
QwtPlotBarChart::drawBar
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &sample, const QwtColumnRect &) const
Definition: qwt_plot_barchart.cpp:341
QwtPlotBarChart::legendData
QList< QwtLegendData > legendData() const QWT_OVERRIDE
Return all information, that is needed to represent the item on the legend.
Definition: qwt_plot_barchart.cpp:416
QwtPlotBarChart::PrivateData::legendMode
QwtPlotBarChart::LegendMode legendMode
Definition: qwt_plot_barchart.cpp:41
QwtColumnRect::RightToLeft
@ RightToLeft
From right to left.
Definition: qwt_column_symbol.h:36
QwtPlotAbstractBarChart::sampleWidth
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
Definition: qwt_plot_abstract_barchart.cpp:225
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
qwt_text.h
QwtPlotBarChart::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_barchart.cpp:29
QwtPointSeriesData
QwtArraySeriesData< QPointF > QwtPointSeriesData
Interface for iterating over an array of points.
Definition: qwt_series_data.h:214
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition: qwt_plot_item.h:206
QwtColumnSymbol::draw
virtual void draw(QPainter *, const QwtColumnRect &) const
Definition: qwt_column_symbol.cpp:238
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtColumnSymbol::setLineWidth
void setLineWidth(int width)
Definition: qwt_column_symbol.cpp:213
QwtLegendData::IconRole
@ IconRole
Definition: qwt_legend_data.h:62
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
QwtPlotItem::legendIconSize
QSize legendIconSize() const
Definition: qwt_plot_item.cpp:386
QwtPlotBarChart::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_barchart.cpp:70
QwtPlotItem::Rtti_PlotBarChart
@ Rtti_PlotBarChart
For QwtPlotBarChart.
Definition: qwt_plot_item.h:114
QwtPlotBarChart::legendMode
LegendMode legendMode() const
Definition: qwt_plot_barchart.cpp:177


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45