Qt绘制曲线图(基于qt画图QPainter)

在没有QCharst模块时,可以使用QPainter自定义绘制曲线折线图

Qt绘制曲线图(基于qt画图QPainter)_第1张图片
下面提供完整代码供参考:
直接在qt创建一个QMainWindow类的app的工程,不自动生成ui文件,然后把下面代码复制到mainwindow.cpp编译运行即可。
mainwindow.cpp:

#include "mainwindow.h"

#include 
#include 
#include 
#include 

//使用QPainter将数据到曲线画在图片中
QImage imageCurve(const QVector<int> &data1, const QVector<int> &data2);

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(800,600);

    int n=10;//n为数据个数
    QVector<int> data1;
    QVector<int> data2;

    for(int i=0;i<n;i++)//随机产生两个数据集
        data1.append(rand()%20+10);
    for(int i=0;i<n;i++)
        data2.append(rand()%30+10);

    QLabel *label = new QLabel(this);

    label->setGeometry(50,50,600,500);//将画出来到曲线显示到label上面
    label->setPixmap(QPixmap::fromImage(imageCurve(data1,data2)));
    label->show();
}

MainWindow::~MainWindow()
{

}


QImage imageCurve(const QVector<int> &data1, const QVector<int> &data2)
{
    QString x_title = "x_title";//x坐标名称
    QString y_title = "y_title";//y坐标名称
    QString title = "xx title charst";//曲线图xx名称
    QString overView = "charst: test,csdn: boonion";//图内容说明

    QString data1_name = "first";//第一条曲线名称
    QString data2_name = "second";


    QImage mimage = QImage(600,500,QImage::Format_RGB32);  //画布的初始化大小设为600*500,使用32位颜色
    QColor backColor = qRgb(255,255,255);    //画布初始化背景色使用白色
    mimage.fill(backColor);//对画布进行填充

    QPainter painter(&mimage);

    painter.setRenderHint(QPainter::Antialiasing, true);//设置反锯齿模式,好看一点


    int pointx=30,pointy=420;//确定坐标轴起点坐标,这里定义(35,280)
    int width=560-pointx,height=280;//确定坐标轴宽度跟高度


    painter.drawLine(pointx,pointy,width+pointx,pointy);//坐标轴x宽度为width
    painter.drawLine(pointx,pointy-height,pointx,pointy);//坐标轴y高度为height


    int n=data1.size();//n为第一个数据集个数
    int m = data2.size();

    int max=0;//数组里的最大值

    for(int i = 0 ; i < n; i++)
    {
        if(data1[i] > max)
        {
            max = data1[i];
        }
    }

    for(int i = 0 ; i < m; i++)
    {
        if(data2[i] > max)
        {
            max = data2[i];
        }
    }


    double kx=(double)width/(n-1); //x轴的系数
    double ky=(double)height/max;//y方向的比例系数

    //设置两条的画笔
    QPen pen,penPoint;
    pen.setColor(Qt::green);
    pen.setWidth(2);

    penPoint.setColor(Qt::blue);
    penPoint.setWidth(5);

    QPen penOther,penPointOther;
    penOther.setColor(Qt::yellow);
    penOther.setWidth(2);

    penPointOther.setColor(Qt::red);
    penPointOther.setWidth(5);

    for(int i=0;i<n-1;i++)//画第一条线
    {
        if(i+1 > n-1)
            break;
        //由于y轴是倒着的,所以y轴坐标要pointy-a[i]*ky 其中ky为比例系数
        painter.setPen(pen);//用于连线
        painter.drawLine(pointx+kx*i,pointy-data1[i]*ky,pointx+kx*(i+1),pointy-data1[i+1]*ky);
        painter.setPen(penPoint);//蓝色的笔,用于标记各个点
        painter.drawPoint(pointx+kx*i,pointy-data1[i]*ky);
        painter.drawText(pointx+kx*i,pointy-data1[i]*ky-4,
                         QString::number(data1[i]));
    }
    if(!data1.isEmpty())
        painter.drawPoint(pointx+kx*(n-1),pointy-data1[n-1]*ky);//绘制最后一个点

    //画线图示
    painter.setPen(pen);
    painter.drawLine(pointx+width-50,pointy-height-50,pointx+width-25,pointy-height-50);
    painter.setPen(penPoint);
    painter.drawPoint(pointx+width-25,pointy-height-50);
    painter.setPen(pen);
    painter.drawLine(pointx+width-20,pointy-height-50,pointx+width,pointy-height-50);
    painter.setPen(Qt::black);
    painter.drawText(pointx+width-40,pointy-height-30,QString("%1").arg(data1_name));


    for(int i=0;i<m-1;i++)
    {
        if(i+1 > m-1)
            break;

        painter.setPen(penOther);
        painter.drawLine(pointx+kx*i,pointy-data2[i]*ky,pointx+kx*(i+1),pointy-data2[i+1]*ky);
        painter.setPen(penPointOther);
        painter.drawPoint(pointx+kx*i,pointy-data2[i]*ky);
        painter.drawText(pointx+kx*i,pointy-data2[i]*ky-5,
                         QString::number(data2[i]));
    }
    if(!data2.isEmpty())
         painter.drawPoint(pointx+kx*(m-1),pointy-data2[m-1]*ky);//绘制最后一个点

    //画线图示
    painter.setPen(penOther);
    painter.drawLine(pointx+width-50,pointy-height-100,pointx+width-25,pointy-height-100);
    painter.setPen(penPointOther);
    painter.drawPoint(pointx+width-25,pointy-height-100);
    painter.setPen(penOther);
    painter.drawLine(pointx+width-20,pointy-height-100,pointx+width,pointy-height-100);
    painter.setPen(Qt::black);
    painter.drawText(pointx+width-40,pointy-height-80,QString("%1").arg(data2_name));


    //绘制刻度线
    int tmp = n > m ? n : m; //取刻度最大的分度值
    int xn = tmp > 16 ? 16 : tmp;

    int yn = max > 20 ? 20 : max;


    QPen penDegree;
    penDegree.setColor(Qt::black);
    penDegree.setWidth(2);
    painter.setPen(penDegree);

    //画上x轴刻度线
    for(int i=0;i<xn;i++)//分成dn份
    {
        //选取合适的坐标,绘制一段长度为4的直线,用于表示刻度

        painter.drawLine(pointx+(i+1)*width/xn,pointy,pointx+(i+1)*width/xn,pointy+4);
        painter.drawText(pointx+(i)*width/xn,
                         pointy+15,QString::number((int)((i)*((double)tmp/xn))));
    }

    //画xy刻度名称
    painter.setPen(Qt::black);
    painter.drawText(pointx+width-40,pointy+30,QString("%1").arg(x_title));
    painter.setPen(Qt::black);
    painter.drawText(pointx-10,pointy-height-30,QString("%1").arg(y_title));


    //y轴刻度线
    double _maStep=(double)max/yn;//y轴刻度间隔需根据最大值来表示
    for(int i=0;i<yn;i++)
    {

        //主要就是确定一个位置,然后画一条短短的直线表示刻度。
        painter.drawLine(pointx,pointy-(i+1)*height/yn,
                         pointx-4,pointy-(i+1)*height/yn);
        painter.drawText(pointx-20,pointy-(i+0.85)*height/yn,
                         QString::number((int)(_maStep*(i+1))));
    }


    //画图内容说明
    QStringList strlist = overView.split(',');//分割字符串
    painter.setPen(Qt::gray);

    int t_h = 100;

    foreach(QString str,strlist)
    {
      painter.drawText(pointx+40,pointy-height-t_h,QString("%1").arg(str));
      t_h -= 20;

    }


    //画标题
    painter.setPen(Qt::black);
    QFont font;
    font.setPixelSize(20);
    painter.setFont(font);
    painter.drawText(pointx+width/2-80,pointy-height-60,QString("%1").arg(title));

    return mimage;
}
//博客:booinon
//https://blog.csdn.net/boonion?spm=1011.2415.3001.5343

//博客:booinon
//https://blog.csdn.net/boonion?spm=1011.2415.3001.5343

你可能感兴趣的:(Qt,C++,qt,ui)