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_text.h"
14 #include "qwt_graphic.h"
15 #include "qwt_legend_data.h"
16 
17 #include <qpainter.h>
18 
20 {
21 public:
23  symbol( NULL ),
25  {
26  }
27 
29  {
30  delete symbol;
31  }
32 
35 };
36 
43 {
44  init();
45 }
46 
53 {
54  init();
55 }
56 
59 {
60  delete d_data;
61 }
62 
64 {
65  d_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 != d_data->symbol )
137  {
138  delete d_data->symbol;
139  d_data->symbol = symbol;
140 
141  legendChanged();
142  itemChanged();
143  }
144 }
145 
151 {
152  return d_data->symbol;
153 }
154 
165 {
166  if ( mode != d_data->legendMode )
167  {
168  d_data->legendMode = mode;
169  legendChanged();
170  }
171 }
172 
178 {
179  return d_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 = d_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 
430  QVariant::fromValue( barTitle( i ) ) );
431 
432  if ( !legendIconSize().isEmpty() )
433  {
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 );
471  icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
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 }
virtual void legendChanged()
const QwtColumnSymbol * symbol() 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.
virtual QRectF boundingRect() const =0
From top to bottom.
A class representing an interval.
Definition: qwt_interval.h:22
From left to right.
A drawing primitive for columns.
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const QWT_OVERRIDE
QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
void setData(QwtSeriesData< QPointF > *series)
void setLegendMode(LegendMode)
Direction direction
Direction.
virtual int rtti() const QWT_OVERRIDE
Abstract base class for bar chart items.
QwtSeriesData< QPointF > * data()
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
virtual QRectF boundingRect() const QWT_OVERRIDE
bool testRenderHint(RenderHint) const
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
PrivateData * d_data
A class representing a text.
Definition: qwt_text.h:51
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
void setFrameStyle(FrameStyle)
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
From bottom to top.
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &sample, const QwtColumnRect &) const
void setLineWidth(int width)
A scale map.
Definition: qwt_scale_map.h:26
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
QwtPlotBarChart::LegendMode legendMode
Directed rectangle representing bounding rectangle and orientation of a column.
void setValue(int role, const QVariant &)
QList< QwtLegendData > legendData() const QWT_OVERRIDE
Return all information, that is needed to represent the item on the legend.
virtual void draw(QPainter *, const QwtColumnRect &) const
LegendMode legendMode() const
QwtInterval hInterval
Interval for the horizontal coordinates.
virtual size_t dataSize() const QWT_OVERRIDE
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
From right to left.
double width() const
Return the width of an interval.
Definition: qwt_interval.h:225
LegendMode
Legend modes.
Interface for iterating over an array of points.
Attributes of an entry on a legend.
virtual QRectF boundingRect() const QWT_OVERRIDE
QwtPlotBarChart(const QString &title=QString())
const QwtText & title() const
For QwtPlotBarChart.


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