55 #if QWT_VERSION >= 0x060000
57 #include <qwt_plot_histogram.h>
58 #include <qwt_painter.h>
59 #include <qwt_scale_map.h>
67 #include "QwtHistogramm.hh"
69 class Histogram::PrivateData {
80 QwtIntervalSeriesData* data;
81 std::vector<QColor> colors_;
93 QwtPlotItem(QwtText(title))
103 void Histogram::init()
105 d_data =
new PrivateData();
107 setItemAttribute(QwtPlotItem::AutoScale,
true);
108 setItemAttribute(QwtPlotItem::Legend,
true);
113 void Histogram::setBaseline(
double reference)
115 if (d_data->reference != reference) {
116 d_data->reference = reference;
121 double Histogram::baseline()
const
123 return d_data->reference;
132 const QwtIntervalSeriesData* Histogram::data()
const
139 d_data->colors_ = _colors;
143 QColor Histogram::color(uint i)
const
145 if (i < d_data->colors_.size())
146 return d_data->colors_[i];
153 QRectF rect = d_data->data->boundingRect();
157 if (d_data->attributes & Xfy) {
158 rect = QRectF(rect.y(), rect.x(), rect.height(), rect.width());
160 if (rect.left() > d_data->reference)
161 rect.setLeft(d_data->reference);
162 else if (rect.right() < d_data->reference)
163 rect.setRight(d_data->reference);
165 if (rect.bottom() < d_data->reference)
166 rect.setBottom(d_data->reference);
167 else if (rect.top() > d_data->reference)
168 rect.setTop(d_data->reference);
175 int Histogram::rtti()
const
177 return QwtPlotItem::Rtti_PlotHistogram;
180 void Histogram::setHistogramAttribute(HistogramAttribute attribute,
bool on)
182 if (
bool(d_data->attributes & attribute) == on)
186 d_data->attributes |= attribute;
188 d_data->attributes &= ~attribute;
193 bool Histogram::testHistogramAttribute(HistogramAttribute attribute)
const
195 return d_data->attributes & attribute;
198 void Histogram::draw(QPainter *painter,
const QwtScaleMap &xMap,
const QwtScaleMap &yMap,
const QRectF &)
const
200 const QwtIntervalSeriesData &iData = *(d_data->data);
202 const int x0 = xMap.transform(baseline());
203 const int y0 = yMap.transform(baseline());
205 for (
int i = 0; i < (int) iData.size(); i++) {
206 if (d_data->attributes & Histogram::Xfy) {
207 const int x2 = xMap.transform(iData.sample(i).value);
211 int y1 = yMap.transform(iData.sample(i).interval.minValue());
212 int y2 = yMap.transform(iData.sample(i).interval.maxValue());
216 if (i < (
int) iData.size() - 2) {
217 const int yy1 = yMap.transform(iData.sample(i + 1).interval.minValue());
218 const int yy2 = yMap.transform(iData.sample(i + 1).interval.maxValue());
220 if (y2 == qMin(yy1, yy2)) {
221 const int xx2 = xMap.transform(iData.sample(i + 1).interval.minValue());
222 if (xx2 != x0 && ((xx2 < x0 && x2 < x0) || (xx2 > x0 && x2 > x0))) {
229 painter->setPen(QPen(color(i)));
231 drawBar(painter, Qt::Horizontal, QRect(x0, y1, x2 - x0, y2 - y1));
233 const int y2 = yMap.transform(iData.sample(i).value);
237 int x1 = xMap.transform(iData.sample(i).interval.minValue());
238 int x2 = xMap.transform(iData.sample(i).interval.maxValue());
242 if (i < (
int) iData.size() - 2) {
243 const int xx1 = xMap.transform(iData.sample(i + 1).interval.minValue());
244 const int xx2 = xMap.transform(iData.sample(i + 1).interval.maxValue());
246 if (x2 == qMin(xx1, xx2)) {
247 const int yy2 = yMap.transform(iData.sample(i + 1).value);
248 if (yy2 != y0 && ((yy2 < y0 && y2 < y0) || (yy2 > y0 && y2 > y0))) {
255 painter->setPen(QPen(color(i)));
257 drawBar(painter, Qt::Vertical, QRect(x1, y0, x2 - x1, y2 - y0));
266 const QColor color(painter->pen().color());
267 const QRect r = rect.normalized();
269 const int factor = 125;
270 const QColor light(color.light(factor));
271 const QColor dark(color.dark(factor));
273 painter->setBrush(color);
274 painter->setPen(Qt::NoPen);
275 QwtPainter::drawRect(painter, r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2);
276 painter->setBrush(Qt::NoBrush);
278 painter->setPen(QPen(light, 2));
280 QwtPainter::drawLine(painter, r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2);
282 painter->setPen(QPen(dark, 2));
284 painter->setPen(QPen(light, 1));
286 QwtPainter::drawLine(painter, r.left(), r.top() + 1, r.left(), r.bottom());
287 QwtPainter::drawLine(painter, r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1);
289 painter->setPen(QPen(dark, 1));
291 QwtPainter::drawLine(painter, r.right() + 1, r.top() + 1, r.right() + 1, r.bottom());
292 QwtPainter::drawLine(painter, r.right(), r.top() + 2, r.right(), r.bottom() - 1);
void setColors(std::vector< QColor > &_colors)
Set colors.
Histogram(const QString &title=QString::null)
Constructor.
virtual ~Histogram()
Destructor.
void setData(QwtIntervalSeriesData *data)
set data to render
virtual QRectF boundingRect() const
Function hat will return the datas bounding rectangle (for rendering)
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) 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.