55 #if QWT_VERSION < 0x060000
59 #include <qwt_interval_data.h>
60 #include <qwt_painter.h>
61 #include <qwt_scale_map.h>
62 #include "HistogramItem.hh"
64 class HistogramItem::PrivateData
69 std::vector< QColor > colors_;
80 QwtPlotItem(QwtText(title))
90 void HistogramItem::init()
92 d_data =
new PrivateData();
93 d_data->reference = 0.0;
94 d_data->attributes = HistogramItem::Auto;
96 setItemAttribute(QwtPlotItem::AutoScale,
true);
97 setItemAttribute(QwtPlotItem::Legend,
true);
102 void HistogramItem::setBaseline(
double reference)
104 if ( d_data->reference != reference )
106 d_data->reference = reference;
111 double HistogramItem::baseline()
const
113 return d_data->reference;
122 const QwtIntervalData &HistogramItem::data()
const
129 d_data->colors_ = _colors;
133 QColor HistogramItem::color(uint i)
const
135 if ( i < d_data->colors_.size() )
136 return d_data->colors_[i];
143 QwtDoubleRect rect = d_data->data.boundingRect();
144 if ( !rect.isValid() )
147 if ( d_data->attributes & Xfy )
149 rect = QwtDoubleRect( rect.y(), rect.x(), rect.height(), rect.width() );
151 if ( rect.left() > d_data->reference )
152 rect.setLeft( d_data->reference );
153 else if ( rect.right() < d_data->reference )
154 rect.setRight( d_data->reference );
158 if ( rect.bottom() < d_data->reference )
159 rect.setBottom( d_data->reference );
160 else if ( rect.top() > d_data->reference )
161 rect.setTop( d_data->reference );
168 int HistogramItem::rtti()
const
170 return QwtPlotItem::Rtti_PlotHistogram;
173 void HistogramItem::setHistogramAttribute(HistogramAttribute attribute,
bool on)
175 if (
bool(d_data->attributes & attribute) == on )
179 d_data->attributes |= attribute;
181 d_data->attributes &= ~attribute;
186 bool HistogramItem::testHistogramAttribute(HistogramAttribute attribute)
const
188 return d_data->attributes & attribute;
192 const QwtScaleMap &yMap,
const QRect &)
const
194 const QwtIntervalData &iData = d_data->data;
196 const int x0 = xMap.transform(baseline());
197 const int y0 = yMap.transform(baseline());
199 for (
int i = 0; i < (int)iData.size(); i++ )
201 if ( d_data->attributes & HistogramItem::Xfy )
203 const int x2 = xMap.transform(iData.value(i));
207 int y1 = yMap.transform( iData.interval(i).minValue());
208 int y2 = yMap.transform( iData.interval(i).maxValue());
212 if ( i < (
int)iData.size() - 2 )
214 const int yy1 = yMap.transform(iData.interval(i+1).minValue());
215 const int yy2 = yMap.transform(iData.interval(i+1).maxValue());
217 if ( y2 == qwtMin(yy1, yy2) )
219 const int xx2 = xMap.transform(
220 iData.interval(i+1).minValue());
221 if ( xx2 != x0 && ( (xx2 < x0 && x2 < x0) ||
222 (xx2 > x0 && x2 > x0) ) )
230 painter->setPen( QPen( color(i) ) );
232 drawBar(painter, Qt::Horizontal,
233 QRect(x0, y1, x2 - x0, y2 - y1));
237 const int y2 = yMap.transform(iData.value(i));
241 int x1 = xMap.transform(iData.interval(i).minValue());
242 int x2 = xMap.transform(iData.interval(i).maxValue());
246 if ( i < (
int)iData.size() - 2 )
248 const int xx1 = xMap.transform(iData.interval(i+1).minValue());
249 const int xx2 = xMap.transform(iData.interval(i+1).maxValue());
251 if ( x2 == qwtMin(xx1, xx2) )
253 const int yy2 = yMap.transform(iData.value(i+1));
254 if ( yy2 != y0 && ( (yy2 < y0 && y2 < y0) || (yy2 > y0 && y2 > y0) ) )
262 painter->setPen( QPen( color(i) ) );
265 QRect(x1, y0, x2 - x1, y2 - y0) );
271 Qt::Orientation,
const QRect& rect)
const
275 const QColor color(painter->pen().color());
276 const QRect r = rect.normalized();
279 const int factor = 125;
280 const QColor light(color.light(factor));
281 const QColor dark(color.dark(factor));
283 painter->setBrush(color);
284 painter->setPen(Qt::NoPen);
285 QwtPainter::drawRect(painter, r.x() + 1, r.y() + 1,
286 r.width() - 2, r.height() - 2);
287 painter->setBrush(Qt::NoBrush);
289 painter->setPen(QPen(light, 2));
291 QwtPainter::drawLine(painter,
292 r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2);
294 painter->setPen(QPen(dark, 2));
296 QwtPainter::drawLine(painter,
297 r.left() + 1, r.bottom(), r.right() + 1, r.bottom());
299 painter->setPen(QPen(light, 1));
301 QwtPainter::drawLine(painter,
302 r.left(), r.top() + 1, r.left(), r.bottom());
303 QwtPainter::drawLine(painter,
304 r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1);
307 painter->setPen(QPen(dark, 1));
309 QwtPainter::drawLine(painter,
310 r.right() + 1, r.top() + 1, r.right() + 1, r.bottom());
311 QwtPainter::drawLine(painter,
312 r.right(), r.top() + 2, r.right(), r.bottom() - 1);
HistogramItem(const QString &title=QString::null)
Constructor.
virtual ~HistogramItem()
Destructor.
void setColors(std::vector< QColor > &_colors)
Set colors.
virtual QwtDoubleRect boundingRect() const
Function hat will return the datas bounding rectangle (for rendering)
void setData(const QwtIntervalData &data)
set data to render
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRect &) const
The actual draw function, drawing the bars inside the plot widget.
virtual void drawBar(QPainter *, Qt::Orientation o, const QRect &) const
Draws a single bar.