/*! \brief Draw dots \param painter Painter \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted \sa QwtCurve::drawPolyline, QwtCurve::drawLine, QwtCurve::drawLines, QwtCurve::drawSpline, QwtCurve::drawSteps QwtCurve::drawPolyline, QwtCurve::drawPolygon */ void QwtCurve::drawDots(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to) { const bool doFill = painter->brush().style() != Qt::NoBrush; QPointArray polyline; if ( doFill ) polyline.resize(to - from + 1); for (int i = from; i <= to; i++) { int xi = xMap.transform(x(i)); int yi = yMap.transform(y(i)); QwtPainter::drawPoint(painter, xi, yi); if ( doFill ) polyline.setPoint(i - from, xi, yi); } if ( doFill ) { closePolyline(xMap, yMap, polyline); painter->setPen(QPen(Qt::NoPen)); QwtPainter::drawPolygon(painter, polyline); } }
/*! \brief Draw step function \param painter Painter \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted \sa QwtCurve::draw, QwtCurve::drawCurve, QwtCurve::drawDots, QwtCurve::drawLines, QwtCurve::drawSpline, QwtCurve::drawSticks */ void QwtCurve::drawSteps(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to) { QPointArray polyline(2 * (to - from) + 1); bool inverted = d_options & Yfx; if ( d_options & Inverted ) inverted = !inverted; int i,ip; for (i = from, ip = 0; i <= to; i++, ip += 2) { int xi = xMap.transform(x(i)); int yi = yMap.transform(y(i)); if ( ip > 0 ) { if (inverted) polyline.setPoint(ip - 1, polyline[ip-2].x(), yi); else polyline.setPoint(ip - 1, xi, polyline[ip-2].y()); } polyline.setPoint(ip, xi, yi); } QwtPainter::drawPolyline(painter, polyline); if ( painter->brush().style() != Qt::NoBrush ) { closePolyline(xMap, yMap, polyline); painter->setPen(QPen(Qt::NoPen)); QwtPainter::drawPolygon(painter, polyline); } }
/*! Translate a point from plot into pixel coordinates \return Point in pixel coordinates \sa QwtPlotPicker::invTransform() */ QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const { QwtDiMap xMap = plot()->canvasMap(d_xAxis); QwtDiMap yMap = plot()->canvasMap(d_yAxis); return QPoint( xMap.transform(pos.x()), yMap.transform(pos.y()) ); }
/*! Translate a rectangle from plot into pixel coordinates \return Rectangle in pixel coordinates \sa QwtPlotPicker::invTransform() */ QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const { QwtDiMap xMap = plot()->canvasMap(d_xAxis); QwtDiMap yMap = plot()->canvasMap(d_yAxis); const int x1 = xMap.transform(rect.x1()); const int x2 = xMap.transform(rect.x2()); const int y1 = yMap.transform(rect.y1()); const int y2 = yMap.transform(rect.y2()); return QRect(x1, y1, x2 - x1, y2 - y1); }
/*! \brief Draw sticks \param painter Painter \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted \sa QwtCurve::draw, QwtCurve::drawCurve, QwtCurve::drawDots, QwtCurve::drawLines, QwtCurve::drawSpline, QwtCurve::drawSteps */ void QwtCurve::drawSticks(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to) { int x0 = xMap.transform(d_ref); int y0 = yMap.transform(d_ref); for (int i = from; i <= to; i++) { int xi = xMap.transform(x(i)); int yi = yMap.transform(y(i)); if (d_options & Xfy) QwtPainter::drawLine(painter, x0, yi, xi, yi); else QwtPainter::drawLine(painter, xi, y0, xi, yi); } }
/*! \brief Draw symbols \param painter Painter \param symbol Curve symbol \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted */ void QwtCurve::drawSymbols(QPainter *painter, QwtSymbol &symbol, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to) { painter->setBrush(symbol.brush()); painter->setPen(symbol.pen()); QRect rect; rect.setSize(QwtPainter::metricsMap().screenToLayout(symbol.size())); for (int i = from; i <= to; i++) { const int xi = xMap.transform(x(i)); const int yi = yMap.transform(y(i)); rect.moveCenter(QPoint(xi, yi)); symbol.draw(painter, rect); } }
/*! \param axis Axis \return Map for the axis on the canvas. With this map pixel coordinates can translated to plot coordinates and vice versa. \sa QwtDiMap, QwtPlot::transform, QwtPlot::invTransform */ QwtDiMap QwtPlot::canvasMap(int axis) const { QwtDiMap map; if ( d_canvas && d_scale[axis] ) { const QwtScale *s = d_scale[axis]; map = *s->scaleDraw(); if ( axis == yLeft || axis == yRight ) { int y = s->y() + s->startBorderDist() - d_canvas->y(); int h = s->height() - s->startBorderDist() - s->endBorderDist(); map.setIntRange(y + h, y); } else { int x = s->x() + s->startBorderDist() - d_canvas->x(); int w = s->width() - s->startBorderDist() - s->endBorderDist(); map.setIntRange(x, x + w); } } return map; }
void QwtCurve::closePolyline(const QwtDiMap &xMap, const QwtDiMap &yMap, QPointArray &pa) const { const int sz = pa.size(); if ( sz < 2 ) return; pa.resize(sz + 2); if ( d_options & QwtCurve::Xfy ) { pa.setPoint(sz, xMap.transform(d_ref), pa.point(sz - 1).y()); pa.setPoint(sz + 1, xMap.transform(d_ref), pa.point(0).y()); } else { pa.setPoint(sz, pa.point(sz - 1).x(), yMap.transform(d_ref)); pa.setPoint(pa.size() - 1, pa.point(0).x(), yMap.transform(d_ref)); } }
/*! Translate a rectangle from pixel into plot coordinates \return Rectangle in plot coordinates \sa QwtPlotPicker::transform() */ QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const { QwtDiMap xMap = plot()->canvasMap(d_xAxis); QwtDiMap yMap = plot()->canvasMap(d_yAxis); return QwtDoubleRect( xMap.invTransform(rect.left()), xMap.invTransform(rect.right()), yMap.invTransform(rect.top()), yMap.invTransform(rect.bottom()) ); }
/*! \param axis Axis \return Map for the axis on the canvas. With this map pixel coordinates can translated to plot coordinates and vice versa. \sa QwtDiMap, QwtPlot::transform, QwtPlot::invTransform */ QwtDiMap QwtPlot::canvasMap(int axis) const { QwtDiMap map; if ( !d_canvas ) return map; const QwtScaleDiv &sd = d_as[axis].scaleDiv(); map.setDblRange(sd.lBound(), sd.hBound(), sd.logScale()); if ( axisEnabled(axis) ) { const QwtScale *s = d_scale[axis]; if ( axis == yLeft || axis == yRight ) { int y = s->y() + s->startBorderDist() - d_canvas->y(); int h = s->height() - s->startBorderDist() - s->endBorderDist(); map.setIntRange(y + h - 1, y); } else { int x = s->x() + s->startBorderDist() - d_canvas->x(); int w = s->width() - s->startBorderDist() - s->endBorderDist(); map.setIntRange(x, x + w - 1); } } else { const int margin = plotLayout()->canvasMargin(axis); const QRect &canvasRect = d_canvas->contentsRect(); if ( axis == yLeft || axis == yRight ) { map.setIntRange(canvasRect.bottom() - margin, canvasRect.top() + margin); } else { map.setIntRange(canvasRect.left() + margin, canvasRect.right() - margin); } } return map; }
void BoxCurve::drawSymbols(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, double *dat, int size) { const int px = xMap.transform(x(0)); QwtSymbol s = this->symbol(); if (min_style != QwtSymbol::None) { const int py_min = yMap.transform(y(0)); s.setStyle(min_style); s.draw(painter, px, py_min); } if (max_style != QwtSymbol::None) { const int py_max = yMap.transform(y(size - 1)); s.setStyle(max_style); s.draw(painter, px, py_max); } if (p1_style != QwtSymbol::None) { const int p1 = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01)); s.setStyle(p1_style); s.draw(painter, px, p1); } if (p99_style != QwtSymbol::None) { const int p99 = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.99)); s.setStyle(p99_style); s.draw(painter, px, p99); } if (mean_style != QwtSymbol::None) { const int mean = yMap.transform(gsl_stats_mean(dat, 1, size)); s.setStyle(mean_style); s.draw(painter, px, mean); } }
void BoxCurve::drawBox(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, double *dat, int size) { const int px = xMap.transform(x(0)); const int px_min = xMap.transform(x(0) - 0.5); const int px_max = xMap.transform(x(0) + 0.5); const int box_width = 1+(px_max - px_min)*b_width/100; const int hbw = box_width/2; const int median = yMap.transform(gsl_stats_median_from_sorted_data (dat, 1, size)); int b_lowerq, b_upperq; double sd, se, mean; if(w_range == SD || w_range == SE || b_range == SD || b_range == SE) { sd = gsl_stats_sd(dat, 1, size); se = sd/sqrt((double)size); mean = gsl_stats_mean(dat, 1, size); } if(b_range == SD) { b_lowerq = yMap.transform(mean - sd*b_coeff); b_upperq = yMap.transform(mean + sd*b_coeff); } else if(b_range == SE) { b_lowerq = yMap.transform(mean - se*b_coeff); b_upperq = yMap.transform(mean + se*b_coeff); } else { b_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*b_coeff)); b_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*b_coeff)); } //draw box if (b_style == Rect) { const QRect r = QRect(px - hbw, b_upperq, box_width, b_lowerq - b_upperq + 1); painter->fillRect(r, QwtPlotCurve::brush()); painter->drawRect(r); } else if (b_style == Diamond) { const QPointArray pa(4); pa[0] = QPoint(px, b_upperq); pa[1] = QPoint(px + hbw, median); pa[2] = QPoint(px, b_lowerq); pa[3] = QPoint(px - hbw, median); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == WindBox) { const int lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.25)); const int upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.75)); const QPointArray pa(8); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(int(px + 0.4*box_width), upperq); pa[2] = QPoint(int(px + 0.4*box_width), lowerq); pa[3] = QPoint(px + hbw, b_lowerq); pa[4] = QPoint(px - hbw, b_lowerq); pa[5] = QPoint(int(px - 0.4*box_width), lowerq); pa[6] = QPoint(int(px - 0.4*box_width), upperq); pa[7] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == Notch) { int j = (int)ceil(0.5*(size - 1.96*sqrt((double)size))); int k = (int)ceil(0.5*(size + 1.96*sqrt((double)size))); const int lowerCI = yMap.transform(dat[j]); const int upperCI = yMap.transform(dat[k]); const QPointArray pa(10); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(px + hbw, upperCI); pa[2] = QPoint(int(px + 0.25*hbw), median); pa[3] = QPoint(px + hbw, lowerCI); pa[4] = QPoint(px + hbw, b_lowerq); pa[5] = QPoint(px - hbw, b_lowerq); pa[6] = QPoint(px - hbw, lowerCI); pa[7] = QPoint(int(px - 0.25*hbw), median); pa[8] = QPoint(px - hbw, upperCI); pa[9] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } if (w_range) {//draw whiskers const int l = int(0.1*box_width); int w_upperq, w_lowerq; if(w_range == SD) { w_lowerq = yMap.transform(mean - sd*w_coeff); w_upperq = yMap.transform(mean + sd*w_coeff); } else if(w_range == SE) { w_lowerq = yMap.transform(mean - se*w_coeff); w_upperq = yMap.transform(mean + se*w_coeff); } else { w_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*w_coeff)); w_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*w_coeff)); } painter->drawLine(px - l, w_lowerq, px + l, w_lowerq); painter->drawLine(px - l, w_upperq, px + l, w_upperq); if (b_style) { if (w_upperq != b_upperq) painter->drawLine(px, w_upperq, px, b_upperq); if (w_lowerq != b_lowerq) painter->drawLine(px, w_lowerq, px, b_lowerq); } else painter->drawLine(px, w_upperq, px, w_lowerq); } //draw median line if (b_style == Notch || b_style == NoBox) return; if (b_style == WindBox) painter->drawLine(int(px - 0.4*box_width), median, int(px + 0.4*box_width), median); else painter->drawLine(px - hbw, median, px + hbw, median); }
/*! \brief Draw a spline \param painter Painter \param xMap x map \param yMap y map \sa QwtCurve::draw, QwtCurve::drawCurve, QwtCurve::drawDots, QwtCurve::drawLines, QwtCurve::drawSteps, QwtCurve::drawSticks */ void QwtCurve::drawSpline(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap) { register int i; int size = dataSize(); double *txval = new double[size]; double *tyval = new double[size]; if ( !txval || !tyval ) { if (txval) delete[] txval; if (tyval) delete[] tyval; return; } QPointArray polyline(d_splineSize); // // Transform x and y values to window coordinates // to avoid a distinction between linear and // logarithmic scales. // for (i=0;i<size;i++) { txval[i] = xMap.xTransform(x(i)); tyval[i] = yMap.xTransform(y(i)); } int stype; if (! (d_options & (Yfx|Xfy|Parametric))) { if (qwtChkMono(txval, size)) { stype = Yfx; } else { if(qwtChkMono(tyval, size)) { stype = Xfy; } else { stype = Parametric; if ( (d_options & Periodic) || ( (x(0) == x(size-1)) && (y(0) == y(size-1)))) { stype |= Periodic; } } } } else { stype = d_options; } if (stype & Parametric) { double *param = new double[size]; if (param) { // // setup parameter vector // param[0] = 0.0; for (i=1; i<size; i++) { double delta = sqrt( qwtSqr(txval[i] - txval[i-1]) + qwtSqr( tyval[i] - tyval[i-1])); param[i] = param[i-1] + qwtMax(delta, 1.0); } // // setup splines int rc = d_spx.recalc(param, txval, size, stype & Periodic); if (!rc) rc = d_spy.recalc(param, tyval, size, stype & Periodic); if (rc) { drawLines(painter, xMap, yMap, 0, size - 1); } else { // fill point array double delta = param[size - 1] / double(d_splineSize-1); for (i=0;i<d_splineSize;i++) { double dtmp = delta * double(i); polyline.setPoint(i, int(floor (d_spx.value(dtmp) + 0.5)), int(floor (d_spy.value(dtmp) + 0.5))); } } delete[] param; } } else if (stype & Xfy) { if (tyval[size-1] < tyval[0]) { qwtTwistArray(txval, size); qwtTwistArray(tyval, size); } // 1. Calculate spline coefficients int rc = d_spx.recalc(tyval, txval, size, stype & Periodic); if (rc) // an error occurred { drawLines(painter, xMap, yMap, 0, size - 1); } else // Spline OK { double ymin = qwtGetMin(tyval, size); double ymax = qwtGetMax(tyval, size); double delta = (ymax - ymin) / double(d_splineSize - 1); for (i=0;i<d_splineSize;i++) { double dtmp = ymin + delta * double(i); polyline.setPoint(i, int(floor(d_spx.value(dtmp) + 0.5)), int(floor(dtmp + 0.5))); } } } else { if (txval[size-1] < txval[0]) { qwtTwistArray(tyval, size); qwtTwistArray(txval, size); } // 1. Calculate spline coefficients int rc = d_spy.recalc(txval, tyval, size, stype & Periodic); if (rc) // error { drawLines(painter, xMap, yMap, 0, size - 1); } else // Spline OK { double xmin = qwtGetMin(txval, size); double xmax = qwtGetMax(txval, size); double delta = (xmax - xmin) / double(d_splineSize - 1); for (i=0;i<d_splineSize;i++) { double dtmp = xmin + delta * double(i); polyline.setPoint(i, int(floor (dtmp + 0.5)), int(floor(d_spy.value(dtmp) + 0.5))); } } } delete[] txval; delete[] tyval; QwtPainter::drawPolyline(painter, polyline); if ( painter->brush().style() != Qt::NoBrush ) { closePolyline(xMap, yMap, polyline); painter->setPen(QPen(Qt::NoPen)); QwtPainter::drawPolygon(painter, polyline); } }
void QwtBarCurve::draw(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to) { if ( !painter || dataSize() <= 0 ) return; if (to < 0) to = dataSize() - 1; if ( verifyRange(from, to) > 0 ) { painter->save(); painter->setPen(QwtPlotCurve::pen()); painter->setBrush(QwtPlotCurve::brush()); int dx,dy,ref,bar_width; if (bar_style == Vertical) ref= yMap.transform(1e-100); //smalest positive value for log scales else ref= xMap.transform(1e-100); int i; if (bar_style == Vertical) { dx = abs(xMap.transform(x(from+1))-xMap.transform(x(from))); for (i=from+2; i<to; i++) { int min = abs(xMap.transform(x(i+1))-xMap.transform(x(i))); if (min <= dx) dx=min; } bar_width=int(dx*(1-bar_gap*0.01)); } else { dy = abs(yMap.transform(y(from+1))-yMap.transform(y(from))); for (i=from+2; i<to; i++) { int min = abs(yMap.transform(y(i+1))-yMap.transform(y(i))); if (min <= dy) dy=min; } bar_width=int(dy*(1-bar_gap*0.01)); } const int half_width = int((0.5-bar_offset*0.01)*bar_width); const int bw1 = bar_width+1; for (i=from; i<=to; i++) { const int px = xMap.transform(x(i)); const int py = yMap.transform(y(i)); if (bar_style == Vertical) { if (y(i) < 0) painter->drawRect(px-half_width, ref, bw1, (py-ref)); else painter->drawRect(px-half_width, py, bw1, (ref-py+1)); } else { if (x(i) < 0) painter->drawRect(px, py-half_width, (ref-px), bw1); else painter->drawRect(ref, py-half_width,(px-ref), bw1); } } painter->restore(); } }
void Plot::drawInwardMinorTicks(QPainter *painter, const QRect &rect, const QwtDiMap &map, int axis) const { QwtScale *scale=(QwtScale *) QwtPlot::axis (axis); if (!scale) return; int x1=rect.left(); int x2=rect.right(); int y1=rect.top(); int y2=rect.bottom(); QPalette pal=scale->palette(); QColor color=pal.color(QPalette::Active, QColorGroup::Foreground); painter->save(); painter->setPen(QPen(color, d_lineWidth, QPainter::SolidLine)); QwtScaleDiv *scDiv=(QwtScaleDiv *)axisScale(axis); int minTicks=scDiv->minCnt(); int j, x, y, low,high; switch (axis) { case QwtPlot::yLeft: x=x1; low=y1+majTickLength; high=y2-majTickLength; for (j = 0; j < minTicks; j++) { y = map.transform(scDiv->minMark(j)); if (y>low && y< high) QwtPainter::drawLine(painter, x, y, x+minTickLength, y); } break; case QwtPlot::yRight: { x=x2; low=y1+majTickLength; high=y2-majTickLength; for (j = 0; j < minTicks; j++) { y = map.transform(scDiv->minMark(j)); if (y>low && y< high) QwtPainter::drawLine(painter, x+1, y, x-minTickLength, y); } } break; case QwtPlot::xBottom: y=y2; low=x1+majTickLength; high=x2-majTickLength; for (j = 0; j < minTicks; j++) { x = map.transform(scDiv->minMark(j)); if (x>low && x<high) QwtPainter::drawLine(painter, x, y+1, x, y-minTickLength); } break; case QwtPlot::xTop: y=y1; low=x1+majTickLength; high=x2-majTickLength; for (j = 0; j < minTicks; j++) { x = map.transform(scDiv->minMark(j)); if (x>low && x<high) QwtPainter::drawLine(painter, x, y, x, y + minTickLength); } break; } painter->restore(); }