9 #include <qwt_global.h> 11 #if defined( QWT_VERSION ) && QWT_VERSION<0x060000 18 HistogramItem::HistogramItem(
const QwtText &title ):
24 HistogramItem::HistogramItem(
const QString &title ):
25 QwtPlotItem( QwtText( title ) )
30 HistogramItem::~HistogramItem()
35 void HistogramItem::init()
37 d_data =
new PrivateData();
38 d_data->reference = 0.0;
39 d_data->attributes = HistogramItem::Auto;
42 d_data->pen = Qt::NoPen;
44 setItemAttribute( QwtPlotItem::AutoScale,
true );
45 setItemAttribute( QwtPlotItem::Legend,
true );
50 void HistogramItem::setBaseline(
double reference )
52 if ( d_data->reference != reference )
54 d_data->reference = reference;
59 double HistogramItem::baseline()
const 61 return d_data->reference;
64 void HistogramItem::setData(
const QwtIntervalData &
data )
70 const QwtIntervalData &HistogramItem::data()
const 75 void HistogramItem::setColor(
const QColor &color )
77 if ( d_data->color != color )
79 d_data->color = color;
84 QColor HistogramItem::color()
const 89 void HistogramItem::setFlat(
bool flat )
91 if ( d_data->flat != flat )
98 bool HistogramItem::flat()
const 103 void HistogramItem::setSpacing(
int spacing )
105 if ( d_data->spacing != spacing )
107 d_data->spacing = spacing;
112 int HistogramItem::spacing()
const 114 return d_data->spacing;
117 void HistogramItem::setPen(
const QPen &pen )
119 if ( d_data->pen != pen )
126 QPen HistogramItem::pen()
const 131 QwtDoubleRect HistogramItem::boundingRect()
const 133 QwtDoubleRect rect = d_data->data.boundingRect();
134 if ( !rect.isValid() )
137 if ( d_data->attributes & Xfy )
139 rect = QwtDoubleRect( rect.y(), rect.x(),
140 rect.height(), rect.width() );
142 if ( rect.left() > d_data->reference )
143 rect.setLeft( d_data->reference );
144 else if ( rect.right() < d_data->reference )
145 rect.setRight( d_data->reference );
149 if ( rect.bottom() < d_data->reference )
150 rect.setBottom( d_data->reference );
151 else if ( rect.top() > d_data->reference )
152 rect.setTop( d_data->reference );
159 int HistogramItem::rtti()
const 161 return QwtPlotItem::Rtti_PlotHistogram;
164 void HistogramItem::setHistogramAttribute( HistogramAttribute attribute,
bool on )
166 if (
bool( d_data->attributes & attribute ) == on )
170 d_data->attributes |= attribute;
172 d_data->attributes &= ~attribute;
177 bool HistogramItem::testHistogramAttribute( HistogramAttribute attribute )
const 179 return d_data->attributes & attribute;
182 void HistogramItem::draw(
QPainter *painter,
const QwtScaleMap &xMap,
183 const QwtScaleMap &yMap,
const QRect & )
const 185 const QwtIntervalData &iData = d_data->data;
186 const int x0 = xMap.transform( baseline() );
187 const int y0 = yMap.transform( baseline() );
189 for (
int i = 0; i < ( int )iData.size(); i++ )
191 if ( d_data->attributes & HistogramItem::Xfy )
193 const int x2 = xMap.transform( iData.value( i ) );
197 int y1 = yMap.transform( iData.interval( i ).minValue() );
198 int y2 = yMap.transform( iData.interval( i ).maxValue() );
202 if ( i < (
int )iData.size() - 2 )
204 const int yy1 = yMap.transform( iData.interval( i + 1 ).minValue() );
205 const int yy2 = yMap.transform( iData.interval( i + 1 ).maxValue() );
207 if ( y2 == qwtMin( yy1, yy2 ) )
209 const int xx2 = xMap.transform(
210 iData.interval( i + 1 ).minValue() );
211 if ( xx2 != x0 && (( xx2 < x0 && x2 < x0 ) ||
212 ( xx2 > x0 && x2 > x0 ) ) )
215 y2 += d_data->spacing;
220 drawBar( painter, Qt::Horizontal,
221 QRect( x0, y1, x2 - x0, y2 - y1 ) );
225 const int y2 = yMap.transform( iData.value( i ) );
229 int x1 = xMap.transform( iData.interval( i ).minValue() );
230 int x2 = xMap.transform( iData.interval( i ).maxValue() );
234 if ( i < (
int )iData.size() - 2 )
236 const int xx1 = xMap.transform( iData.interval( i + 1 ).minValue() );
237 const int xx2 = xMap.transform( iData.interval( i + 1 ).maxValue() );
239 if ( x2 == qwtMin( xx1, xx2 ) )
241 const int yy2 = yMap.transform( iData.value( i + 1 ) );
242 if ( yy2 != y0 && (( yy2 < y0 && y2 < y0 ) ||
243 ( yy2 > y0 && y2 > y0 ) ) )
246 x2 -= d_data->spacing;
250 drawBar( painter, Qt::Vertical,
251 QRect( x1, y0, x2 - x1, y2 - y0 ) );
256 void HistogramItem::drawBar(
QPainter *painter,
257 Qt::Orientation,
QRect rect )
const 266 painter->
setPen( d_data->pen );
267 int penWidth = d_data->pen == Qt::NoPen ? 0 :
268 ( d_data->pen.isCosmetic() ? 1 : d_data->pen.width() );
269 QwtPainter::drawRect( painter, r.
x(), r.
y(),
274 const int factor = 125;
275 const QColor light( d_data->color.light( factor ) );
276 const QColor dark( d_data->color.dark( factor ) );
278 QwtPainter::drawRect( painter, r.
x() + 1, r.
y() + 1,
284 QwtPainter::drawLine( painter,
288 QwtPainter::drawLine( painter,
293 QwtPainter::drawLine( painter,
295 QwtPainter::drawLine( painter,
300 QwtPainter::drawLine( painter,
302 QwtPainter::drawLine( painter,
311 void HistogramItem::updateLegend( QwtLegend *legend )
const 316 QwtPlotItem::updateLegend( legend );
319 if ( !widget || !widget->
inherits(
"QwtLegendItem" ) )
322 QwtLegendItem *legendItem = ( QwtLegendItem * )widget;
325 legendItem->setUpdatesEnabled(
false );
327 const int policy = legend->displayPolicy();
329 if ( policy == QwtLegend::FixedIdentifier )
331 int mode = legend->identifierMode();
333 legendItem->setCurvePen(
QPen( color() ) );
335 if ( mode & QwtLegendItem::ShowText )
336 legendItem->setText( title() );
338 legendItem->setText( QwtText() );
340 legendItem->setIdentifierMode( mode );
342 else if ( policy == QwtLegend::AutoIdentifier )
346 legendItem->setCurvePen(
QPen( color() ) );
347 mode |= QwtLegendItem::ShowLine;
348 if ( !title().isEmpty() )
350 legendItem->setText( title() );
351 mode |= QwtLegendItem::ShowText;
355 legendItem->setText( QwtText() );
357 legendItem->setIdentifierMode( mode );
360 legendItem->setUpdatesEnabled( doUpdate );
361 legendItem->update();
bool inherits(const char *className) const
void setPen(const QColor &color)
void setBrush(const QBrush &brush)