qwt_plot_barchart.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_barchart.h"
11 #include "qwt_scale_map.h"
12 #include "qwt_column_symbol.h"
13 #include "qwt_painter.h"
14 #include <qpainter.h>
15 
17 {
18 public:
20  symbol( NULL ),
22  {
23  }
24 
26  {
27  delete symbol;
28  }
29 
32 };
33 
40 {
41  init();
42 }
43 
50 {
51  init();
52 }
53 
56 {
57  delete d_data;
58 }
59 
61 {
62  d_data = new PrivateData;
63  setData( new QwtPointSeriesData() );
64 }
65 
68 {
70 }
71 
80  const QVector<QPointF> &samples )
81 {
82  setData( new QwtPointSeriesData( samples ) );
83 }
84 
95  const QVector<double> &samples )
96 {
97  QVector<QPointF> points;
98  points.reserve( samples.size() );
99 
100  for ( int i = 0; i < samples.size(); i++ )
101  points += QPointF( i, samples[ i ] );
102 
103  setData( new QwtPointSeriesData( points ) );
104 }
105 
117 {
118  setData( data );
119 }
120 
132 {
133  if ( symbol != d_data->symbol )
134  {
135  delete d_data->symbol;
136  d_data->symbol = symbol;
137 
138  legendChanged();
139  itemChanged();
140  }
141 }
142 
148 {
149  return d_data->symbol;
150 }
151 
162 {
163  if ( mode != d_data->legendMode )
164  {
165  d_data->legendMode = mode;
166  legendChanged();
167  }
168 }
169 
175 {
176  return d_data->legendMode;
177 }
178 
184 {
185  const size_t numSamples = dataSize();
186  if ( numSamples == 0 )
188 
189  QRectF rect = QwtPlotSeriesItem::boundingRect();
190  if ( rect.height() >= 0 )
191  {
192  const double baseLine = baseline();
193 
194  if ( rect.bottom() < baseLine )
195  rect.setBottom( baseLine );
196 
197  if ( rect.top() > baseLine )
198  rect.setTop( baseLine );
199  }
200 
201  if ( orientation() == Qt::Horizontal )
202  rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
203 
204  return rect;
205 }
206 
220 void QwtPlotBarChart::drawSeries( QPainter *painter,
221  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
222  const QRectF &canvasRect, int from, int to ) const
223 {
224  if ( to < 0 )
225  to = dataSize() - 1;
226 
227  if ( from < 0 )
228  from = 0;
229 
230  if ( from > to )
231  return;
232 
233 
234  const QRectF br = data()->boundingRect();
235  const QwtInterval interval( br.left(), br.right() );
236 
237  painter->save();
238 
239  for ( int i = from; i <= to; i++ )
240  {
241  drawSample( painter, xMap, yMap,
242  canvasRect, interval, i, sample( i ) );
243  }
244 
245  painter->restore();
246 }
247 
260  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
261  const QRectF &canvasRect, const QwtInterval &boundingInterval,
262  const QPointF &sample ) const
263 {
264  QwtColumnRect barRect;
265 
266  if ( orientation() == Qt::Horizontal )
267  {
268  const double barHeight = sampleWidth( yMap, canvasRect.height(),
269  boundingInterval.width(), sample.y() );
270 
271  const double x1 = xMap.transform( baseline() );
272  const double x2 = xMap.transform( sample.y() );
273 
274  const double y = yMap.transform( sample.x() );
275  const double y1 = y - 0.5 * barHeight;
276  const double y2 = y + 0.5 * barHeight;
277 
278  barRect.direction = ( x1 < x2 ) ?
280 
281  barRect.hInterval = QwtInterval( x1, x2 ).normalized();
282  barRect.vInterval = QwtInterval( y1, y2 );
283  }
284  else
285  {
286  const double barWidth = sampleWidth( xMap, canvasRect.width(),
287  boundingInterval.width(), sample.y() );
288 
289  const double x = xMap.transform( sample.x() );
290  const double x1 = x - 0.5 * barWidth;
291  const double x2 = x + 0.5 * barWidth;
292 
293  const double y1 = yMap.transform( baseline() );
294  const double y2 = yMap.transform( sample.y() );
295 
296  barRect.direction = ( y1 < y2 ) ?
298 
299  barRect.hInterval = QwtInterval( x1, x2 );
300  barRect.vInterval = QwtInterval( y1, y2 ).normalized();
301  }
302 
303  return barRect;
304 }
305 
319 void QwtPlotBarChart::drawSample( QPainter *painter,
320  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
321  const QRectF &canvasRect, const QwtInterval &boundingInterval,
322  int index, const QPointF &sample ) const
323 {
324  const QwtColumnRect barRect = columnRect( xMap, yMap,
325  canvasRect, boundingInterval, sample );
326 
327  drawBar( painter, index, sample, barRect );
328 }
329 
338 void QwtPlotBarChart::drawBar( QPainter *painter,
339  int sampleIndex, const QPointF &sample,
340  const QwtColumnRect &rect ) const
341 {
342  const QwtColumnSymbol *specialSym =
343  specialSymbol( sampleIndex, sample );
344 
345  const QwtColumnSymbol *sym = specialSym;
346  if ( sym == NULL )
347  sym = d_data->symbol;
348 
349  if ( sym )
350  {
351  sym->draw( painter, rect );
352  }
353  else
354  {
355  // we build a temporary default symbol
357  sym.setLineWidth( 1 );
359  sym.draw( painter, rect );
360  }
361 
362  delete specialSym;
363 }
364 
375  int sampleIndex, const QPointF &sample ) const
376 {
377  Q_UNUSED( sampleIndex );
378  Q_UNUSED( sample );
379 
380  return NULL;
381 }
382 
396 QwtText QwtPlotBarChart::barTitle( int sampleIndex ) const
397 {
398  Q_UNUSED( sampleIndex );
399  return QwtText();
400 }
401 
413 QList<QwtLegendData> QwtPlotBarChart::legendData() const
414 {
415  QList<QwtLegendData> list;
416 
418  {
419  const size_t numSamples = dataSize();
420 #if QT_VERSION >= 0x040700
421  list.reserve( numSamples );
422 #endif
423 
424  for ( size_t i = 0; i < numSamples; i++ )
425  {
427 
428  QVariant titleValue;
429  qVariantSetValue( titleValue, barTitle( i ) );
430  data.setValue( QwtLegendData::TitleRole, titleValue );
431 
432  if ( !legendIconSize().isEmpty() )
433  {
434  QVariant iconValue;
435  qVariantSetValue( iconValue,
436  legendIcon( i, legendIconSize() ) );
437 
438  data.setValue( QwtLegendData::IconRole, iconValue );
439  }
440 
441  list += data;
442  }
443  }
444  else
445  {
447  }
448 
449  return list;
450 }
451 
466  int index, const QSizeF &size ) const
467 {
468  QwtColumnRect column;
469  column.hInterval = QwtInterval( 0.0, size.width() - 1.0 );
470  column.vInterval = QwtInterval( 0.0, size.height() - 1.0 );
471 
472  QwtGraphic icon;
473  icon.setDefaultSize( size );
474  icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
475 
476  QPainter painter( &icon );
477  painter.setRenderHint( QPainter::Antialiasing,
479 
480  int barIndex = -1;
482  barIndex = index;
483 
484  drawBar( &painter, barIndex, QPointF(), column );
485 
486  return icon;
487 }
virtual void legendChanged()
const QwtColumnSymbol * symbol() const
virtual size_t dataSize() const
Qt::Orientation orientation() const
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
QwtInterval vInterval
Interval for the vertical coordinates.
QwtInterval normalized() const
Normalize the limits of the interval.
Enable antialiasing.
virtual ~QwtPlotBarChart()
Destructor.
void setSamples(const QVector< QPointF > &)
A plain frame style.
QwtGraphic legendIcon(int index, const QSizeF &) const
virtual QRectF boundingRect() const =0
From top to bottom.
A class representing an interval.
Definition: qwt_interval.h:26
From left to right.
A drawing primitive for columns.
virtual void drawSeries(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
void setData(QwtSeriesData< QPointF > *series)
void setLegendMode(LegendMode)
Direction direction
Direction.
Abstract base class for bar chart items.
QwtSeriesData< QPointF > * data()
TFSIMD_FORCE_INLINE const tfScalar & y() const
QSize legendIconSize() const
void setDefaultSize(const QSizeF &)
Set a default size.
QwtPlotBarChart displays a series of a values as bars.
void setSymbol(QwtColumnSymbol *)
Assign a symbol.
virtual QwtColumnSymbol * specialSymbol(int sampleIndex, const QPointF &) const
QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
virtual int rtti() const
bool testRenderHint(RenderHint) const
PrivateData * d_data
void setFrameStyle(FrameStyle style)
A class representing a text.
Definition: qwt_text.h:51
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
A paint device for scalable graphics.
Definition: qwt_graphic.h:74
TFSIMD_FORCE_INLINE const tfScalar & x() const
From bottom to top.
virtual QRectF boundingRect() const
void setLineWidth(int width)
A scale map.
Definition: qwt_scale_map.h:30
QPointF sample(int index) const
virtual void itemChanged()
QwtColumnRect columnRect(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, const QPointF &sample) const
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &point, const QwtColumnRect &) const
QwtPlotBarChart::LegendMode legendMode
Directed rectangle representing bounding rectangle and orientation of a column.
void setValue(int role, const QVariant &)
double sampleWidth(const QwtScaleMap &map, double canvasSize, double dataSize, double value) const
virtual void draw(QPainter *, const QwtColumnRect &) const
LegendMode legendMode() const
QwtInterval hInterval
Interval for the horizontal coordinates.
virtual QRectF boundingRect() const
double transform(double s) const
virtual void drawSample(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, int index, const QPointF &sample) const
int i
From right to left.
double width() const
Return the width of an interval.
Definition: qwt_interval.h:228
LegendMode
Legend modes.
Interface for iterating over an array of points.
Attributes of an entry on a legend.
QwtPlotBarChart(const QString &title=QString())
const QwtText & title() const
For QwtPlotBarChart.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17