Qwt实现对称ErrorBar图

用QWT画曲线图很方便,qwt画errorBar不知道是怎么实现的,故自己实现一种对称的ErrorBar,

方法:重新实现QwtPlotCurve的drawCurve函数;

头文件实现:

#ifndef QwtPlotErrorBarCurve_LJ_H
#define QwtPlotErrorBarCurve_LJ_H
#include <QtQwt.h>
 
class QwtPlotErrorBarCurve_LJ:public QwtPlotCurve
{
public:
    QwtPlotErrorBarCurve_LJ();
     explicit QwtPlotErrorBarCurve_LJ( const QString &title = QString::null );
    void setCurveValue(QVector<double>,QVector<double>,QVector<double>,int num);
protected:
    void drawCurve(QPainter *painter, int style, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const;
 
private:
    QVector<QwtPointArrayData *> m_errorData;/*误差数据*/
    QVector<QwtPointArrayData *> m_shortHorizontalLine;/*短横线数据*/
};
 
#endif // QwtPlotErrorBarCurve_LJ_H
 

函数实现:

#include "qwt_plot_errorBar_curve_lj.h"
 
QwtPlotErrorBarCurve_LJ::QwtPlotErrorBarCurve_LJ()
{
 
}
 
QwtPlotErrorBarCurve_LJ::QwtPlotErrorBarCurve_LJ(const QString &title)
{
    setTitle(title);
}
 
void QwtPlotErrorBarCurve_LJ::setCurveValue(QVector<double>x, QVector<double>y, QVector<double>var, int num)
{
    QwtPointArrayData *data = new QwtPointArrayData(x, y);
    setData(data);
 
   for(int i=0;i<num;i++)
    {
        double y1 = y[i] + var[i];
        double y2 = y[i] - var[i];
        QVector<double> xc;
        QVector<double> yc;
        yc.append(y1);
        yc.append(y2);
        xc.append(x[i]);
        xc.append(x[i]);
       QwtPointArrayData *data= new QwtPointArrayData(xc,yc);
        m_errorData.append(data);
 
        QVector<double> xs;
        QVector<double> ys;
        ys.append(y1);
        ys.append(y1);
        xs.append(x[i]+1);
        xs.append(x[i]-1);
        QwtPointArrayData *data1= new QwtPointArrayData(xs,ys);
        m_shortHorizontalLine.append(data1);
        QVector<double> ys2;
        ys2.append(y2);
        ys2.append(y2);
        QwtPointArrayData *data2= new QwtPointArrayData(xs,ys2);
        m_shortHorizontalLine.append(data2);
    }
}
#include <QDebug>
void QwtPlotErrorBarCurve_LJ::drawCurve( QPainter *painter, int style,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, int from, int to ) const
{
   QwtPlotCurve::drawCurve(painter,style,xMap,yMap,canvasRect,from,to);
    const bool doAlign = QwtPainter::roundingAlignment( painter );
    const bool noDuplicates = testPaintAttribute(FilterPoints) ;
    QwtPointMapper mapper;
    mapper.setFlag( QwtPointMapper::RoundPoints, doAlign );
    mapper.setFlag( QwtPointMapper::WeedOutPoints, noDuplicates );
    mapper.setBoundingRect( canvasRect );
    foreach (QwtPointArrayData *data, m_errorData) {
 
        QPolygonF polyline = mapper.toPolygonF( xMap, yMap, data, 0, 1 );
          QwtPainter::drawPolyline( painter, polyline );
    }
    foreach (QwtPointArrayData *data, m_shortHorizontalLine) {
 
        QPolygonF polyline = mapper.toPolygonF( xMap, yMap, data, 0, 1 );
          QwtPainter::drawPolyline( painter, polyline );
    }
 
 
 
}
 
 
 

调用方法实现:

#include "qwt_plot_errorBar_curve_lj.h"
void electronicCamDiagram::initErrorBar()
{
 
    QGridLayout *gridLayout = new QGridLayout(this);
    m_plot= new QwtPlot(this);
    m_plot->setAxisScale(QwtPlot::xBottom,0,100,10);
    m_plot->setAxisScale(QwtPlot::yLeft,20,40,5);
    m_plot->insertLegend(new QwtLegend(),QwtPlot::RightLegend);
    QVector<double> xs;
    QVector<double> ys;
    QVector<double> vars;
    for (int x = 0; x < 10 ; x++)
    {
        xs.append(x*10);
        ys.append(dataValue[x][0]);
        vars.append(dataValue[x][1]);
    }
 
    //构造曲线数据
    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
                                       QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
 
 
    QwtPlotErrorBarCurve_LJ *curveLine = new QwtPlotErrorBarCurve_LJ("ErrorBar");
    curveLine->setCurveValue(xs,ys,vars,10);
    //curveLine->setData(data);//设置数据
    curveLine->setStyle(QwtPlotCurve::Dots);//直线形式
    //curveLine->setCurveAttribute(QwtPlotCurve::Fitted, true);//是曲线更光滑
    curveLine->setSymbol( symbol );//添加样本点形状
    curveLine->setPen(QPen(Qt::blue));//设置画笔
    curveLine->attach(m_plot);//把曲线附加到plot上
    gridLayout->addWidget(m_plot);
    exportPlot();
}

数据源:

double  dataValue[10][2] =
{
    { 31.82, 2.05 },
    { 30.82, 2.10 },
    { 31.82, 2.05 },
    { 31.82, 3.05 },
    { 33.82, 2.05 },
    { 31.82, 4.05 },
    { 31.82, 2.05 },
    { 30.82, 3.05 },
    { 27.82, 2.05 },
    { 34.82, 2.05 },
};

效果:


以上是全部实现过程;

第一次写博客有什么不好的地方,请多多指教;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值