曲线

1.h

#ifndef CURVECHART_H
#define CURVECHART_H

#include <QWidget>
#include <QTimer>
namespace Ui {
class curvechart;
}

class curvechart : public QWidget
{
    Q_OBJECT

public:
    explicit curvechart(QWidget *parent = 0);
    ~curvechart();
    void setShowPoint(bool showPoint);
    void setShowHLine(bool showHLine);
    void setShowPointBg(bool showPointBg);
    void setpointColor(QColor pointColor);
protected:
    void paintEvent(QPaintEvent *event);
    void drawBg(QPainter *painter);
    void drawBox(QPainter *painter);
    void drawText(QPainter *painter);
    void drawPoint(QPainter *painter);
private:
    Ui::curvechart *ui;
    QColor bgColorStart;            //背景渐变开始颜色
    QColor bgColorEnd;              //背景渐变结束颜色
    QColor textColor;               //文字颜色
    QColor pointColor;              //坐标点颜色

    QString text;

    double space;
    QVector<double> listData;
    int right_num;//记录哪个数在最右边
    int sum;//记录左边有几个数
    QTimer timer;

    bool showPoint;
    bool showHLine;
    bool ShowPointBg;

    int num;//一张图,有几个点
};

#endif // CURVECHART_H

2.cpp

#include "curvechart.h"
#include "ui_curvechart.h"
#include <QPainter>
#include <QDebug>

curvechart::curvechart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::curvechart)
{
    ui->setupUi(this);

    space =40;
    bgColorStart = QColor(79, 79, 79);
    bgColorEnd = QColor(51, 51, 51);
    textColor = QColor(250, 250, 250);
    pointColor = QColor(38, 114, 179);

    showPoint=true;
    showHLine=true;
    ShowPointBg=true;
    num=50;

    text="曲线图";
    for(int i=0;i<100;i++)
    {
//        qDebug()<<qrand()%100;
        listData.append(qrand()%100);
    }
    sum=1;
    right_num=0;

    timer.setInterval(200);
    connect(&timer,&QTimer::timeout,[=](){
        if(sum!=num)sum++;
        if(right_num==listData.count()-1)right_num=0;
        else right_num++;
        update();
    });
    timer.start();
}

curvechart::~curvechart()
{
    delete ui;
}

void curvechart::setShowPoint(bool s)
{
    showPoint=s;
}

void curvechart::setShowHLine(bool s)
{
    showHLine=s;
}

void curvechart::setShowPointBg(bool s)
{
    ShowPointBg=s;
}

void curvechart::setpointColor(QColor p)
{
    pointColor=p;
}

void curvechart::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    drawBg(&painter);
    drawBox(&painter);
    drawText(&painter);
    drawPoint(&painter);
}

void curvechart::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient topGradient(rect().topLeft(), rect().bottomLeft());
    topGradient.setColorAt(0.0, bgColorStart);
    topGradient.setColorAt(1.0, bgColorEnd);
    painter->setBrush(topGradient);
    painter->drawRect(rect());
    painter->restore();
}

void curvechart::drawBox(QPainter *painter)
{
    painter->save();

    painter->translate(space,space);
    QPointF topLeftPot(0,0);
    qreal w=width()-space*3/2,h=height()-space*3/2;
    QPointF bottomRightPot(w,h);

    painter->setPen(QColor(250,250,250));
    painter->setBrush(Qt::NoBrush);
    //画盒子
    painter->drawRect(QRectF(topLeftPot,bottomRightPot));

    qreal sp=h/10;
    //画数字
    for(int i=0;i<11;i++)
    {
        int num=100-i*10;
        QRect rect(-space-5,-20+i*sp,space,40);

        painter->drawText(rect,Qt::AlignVCenter|Qt::AlignRight,QString::number(num));
    }
    //画虚线
    if(showHLine){
        QPen pen(QColor(250,250,250),1,Qt::DotLine);
        painter->setPen(pen);
        for(int i=1;i<10;i++)
        {
            painter->drawLine(QPointF(0,sp*i),QPointF(w,sp*i));
        }
    }
    painter->restore();
}

void curvechart::drawText(QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setColor(QColor(250,250,250));
    QFont font;
    font.setPixelSize(25);
    font.setBold(true);
    painter->setFont(font);
    painter->setPen(pen);
    painter->drawText(QRect(0,0,width(),space),Qt::AlignCenter,text);
    painter->restore();
}

void curvechart::drawPoint(QPainter *painter)
{
    painter->save();
    painter->translate(space,height()-space/2);

    qreal w=width()-space*3/2;
    qreal sp=w/num;
    QVector<QPointF> points;
    //right_num:最右边的点:(sp*30,listData[right_num])
    int s=sum;
    qreal right_w=w;
    int m=right_num;
    qreal scale=(height()-space*3/2)/100;//高度比例

    points.append(QPointF(right_w,0));//最右边的点
    while(s!=1)
    {
        if(s==2){
            points.append(QPointF(right_w,0));
            qDebug()<<right_w;
            break;
        }
        QPointF p(right_w,-listData[m]*scale);//在listData中的第m+1个数
        points.append(p);
//      qDebug()<<s<<":"<<p;
        s--;
        right_w-=sp;
        if(m>0)m=m-1;
        else m=listData.count()-1;
    }
    //需要在首尾添加点
    QColor color(pointColor.red(),pointColor.green(),pointColor.blue(),150);
    QPen pen;
    pen.setColor(pointColor);
    pen.setWidth(3);
    painter->setPen(pen);
    if(ShowPointBg){
        painter->setBrush(color);
    }
    else{
        painter->setBrush(Qt::NoBrush);
    }
    //绘制图
    painter->drawPolygon(points);
    //绘制坐标点
    if(showPoint){
        for(int i=0;i<points.count();++i)
        {
            QPointF dataPot=points.at(i);
            painter->setBrush(QBrush(color));
            painter->drawEllipse(dataPot,3,3);
        }
    }
    painter->restore();
}

 

3.效果

 

posted @ 2024-12-03 14:59  wuyuan2011woaini  阅读(1)  评论(0编辑  收藏  举报