ubuntu16在C++代码中实现plot函数

依靠qt的强大第三方库qwt实现plot(不好用来kuan我!!!)

先在qt ide中实现相关功能,然后再将其修改为可以在任意c++程序中运行的代码

需要的第三方库
  1. QT,可以使用命令行或者run文件直接安装
  2. QWT,QWT是专门用于在qt中plot数据的库,适用于 windows与ubuntu。
  3. QWT的安装方法可以依照这篇博客 https://blog.csdn.net/u013457167/article/details/80294580
    a. 下载qwt库
    b. 使用qt打开qwt中的pro文件
    c. 运行qmake
    d. 在qwt文件目录中执行
    make
    sudo make install
    
    e. 可以根据自己喜好将 so文件放入合适的位置,记得在.bashrc中添加
    export LD_LIBRARY_PATH=/usr/local/qwt-6.1.4:$LD_LIBRARY_PATH
    并在命令行中执行
    sudo ldconfig
使用QT IDE实现plot
  1. 依据这篇博客 https://blog.csdn.net/tengweitw/article/details/41911035
  2. 可以看到正确的结果
上面的部分需要依靠QT IDE来完成,现在使用cmake将其编成库,供其它代码使用

代码结构(简单粗暴!!!!):

 --- build
 --- CMakeLists.txt
 --- main.cpp
 --- plotlines.cpp
 --- plotlines.h

CMakeLists.txt写的内容如下:

cmake_minimum_required(VERSION 2.8)
project(testproject)

add_definitions(-std=c++11)
find_package(Qt5Widgets)
include_directories(/usr/local/qwt-6.1.4/include)

add_executable( main main.cpp plotlines.cpp)
target_link_libraries(main Qt5::Widgets /usr/local/lib/libqwt.so.6 )
#这里的qwt库要按照自己安装的路径书写

main.cpp内容

#include <QApplication>
#include "plotlines.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    PlotLines p;
    p.show();

    return a.exec();
}


plotlines.h

#ifndef PLOTLINES_H
#define PLOTLINES_H
#define QWT_DLL
#include <qwt_plot.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_histogram.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>

#include <qwt_legend.h>
#include <qwt_legend_label.h>
#include <qwt_column_symbol.h>
#include <qwt_series_data.h>
#include <qpen.h>
#include <qwt_symbol.h>
#include <qwt_picker_machine.h>
class PlotLines : public QwtPlot
{
    // Q_OBJECT
public:
    PlotLines(QWidget *parent = 0);




private Q_SLOTS:
    void showItem(const QVariant &itemInfo, bool on);//点击图例,显示相应的曲线
};

#endif // PLOTLINES_H


plotlines.cpp

#include "plotlines.h"

PlotLines::PlotLines(QWidget *parent) :
    QwtPlot(parent)
{
    setTitle("图的标题");
//---------设置画布---------//
    QwtPlotCanvas *canvas=new QwtPlotCanvas();
    canvas->setPalette(Qt::white);
    canvas->setBorderRadius(10);
    setCanvas( canvas );
    plotLayout()->setAlignCanvasToScales( true );

    //-----------设置x,y坐标和范围--------------//
    setAxisTitle( QwtPlot::yLeft, "ylabel" );
    setAxisTitle( QwtPlot::xBottom, "xlabel" );
    setAxisScale(QwtPlot::yLeft,0.0,10.0);
    setAxisScale(QwtPlot::xBottom,0.0,10.0);

    //----------------设置栅格线-------------------//
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableX( true );//设置网格线
    grid->enableY( true );
    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
    grid->attach( this );

    //-----------------开始画图----------------------//
    QwtPlotCurve *curve=new QwtPlotCurve("curve");
   // curve->setTitle( "信道"+QString( "%1 " ).arg( i+1));
    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
    QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
    curve->setSymbol( symbol );//添加样本点形状

    QPolygonF points1, points2;//输入节点数据QPointF(x,y)
    points1<<QPointF(1,1)<<QPointF(2,2)<<QPointF(3,3)<<QPointF(4,4)<<QPointF(5,5)<<QPointF(6,6)<<QPointF(7,7);
    points2<<QPointF(1,2)<<QPointF(2,3)<<QPointF(3,4)<<QPointF(4,5)<<QPointF(5,6)<<QPointF(6,7)<<QPointF(7,8);
    curve->setSamples(points1);
    curve->attach( this );
    curve->setLegendAttribute(curve->LegendShowLine);//显示图例的标志,这里显示线的颜色。

    //曲线2的形状采用默认,即不单独设置画笔的颜色、样本点的显示
    QwtPlotCurve *curve2=new QwtPlotCurve("curve2");
    curve2->setSamples(points2);
    curve2->attach( this );
    curve2->setLegendAttribute(curve->LegendShowLine);

//--------------设置图例可以被点击来确定是否显示曲线-----------------------//
    QwtLegend *legend = new QwtLegend;
    legend->setDefaultItemMode( QwtLegendData::Checkable );//图例可被点击
    insertLegend( legend, QwtPlot::RightLegend );
    connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
        SLOT( showItem( const QVariant &, bool ) ) );//点击图例操作

    QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状,注意改变参数
   //  qDebug()<<items;
    for ( int i = 0; i < items.size(); i++ )
    {

        if ( i == 0 )
        {
            const QVariant itemInfo = itemToInfo( items[i] );

            QwtLegendLabel *legendLabel =
                qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );
            if ( legendLabel )
                legendLabel->setChecked( true );//

            items[i]->setVisible( true );
        }
        else
        {
            items[i]->setVisible( false );
        }
    }

    this->resize(600,400);

    this->replot();

    setAutoReplot( true );//设置自动重画,相当于更新

}
//点击图例,显示相应的曲线
void PlotLines::showItem(const QVariant &itemInfo, bool on)
{
    QwtPlotItem *plotItem = infoToItem( itemInfo );
    if ( plotItem )
        plotItem->setVisible( on );
}

其它要修改的地方

  1. 需要将代码中出现Q_OBJECT的内容注释掉,否则报错
CMakeFiles/main.dir/main.cpp.o: In function `PlotLines::~PlotLines()':
main.cpp:(.text._ZN9PlotLinesD2Ev[_ZN9PlotLinesD5Ev]+0xf): undefined reference to `vtable for PlotLines'
main.cpp:(.text._ZN9PlotLinesD2Ev[_ZN9PlotLinesD5Ev]+0x21): undefined reference to `vtable for PlotLines'
main.cpp:(.text._ZN9PlotLinesD2Ev[_ZN9PlotLinesD5Ev]+0x37): undefined reference to `vtable for PlotLines'

我的效果

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值