Charts

做了一半不做了,保存下进度

.pro

QT       += core gui charts
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWidgets/QWidget>
#include<QMouseEvent>

#include <QChart>
#include <QChartView>
#include <QSplineSeries>
#include <qsplineseries.h>
#include <QStackedBarSeries>
#include <qstackedbarseries.h>
#include<QBarSet>
#include<QList>
#include<QPair>
#include<QPoint>
#include<QString>
#include<QGraphicsSimpleTextItem>
QT_BEGIN_NAMESPACE
QT_CHARTS_USE_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    QPoint m_lastPoint;
    bool m_isPress;
    bool m_ctrlPress;
    bool m_alreadySaveRange;
    double m_xMin, m_xMax, m_yMin, m_yMax;
    QGraphicsSimpleTextItem* m_coordItem;
    /*曲线图-start*/
public:
    QTimer* time_run;
    QChart* Chart_spline;
    QSplineSeries* series_spline;
    int spline_maxSize;
    void spline_init(void);
public slots:
    //void slot_spline_roll(void);
    /*曲线图-end*/


public:
    QStackedBarSeries* series_bar = nullptr;
    QChart* Chart_bar;
    QBarSet* barH;
    QBarSet* barL;
    void bar_init(void);
    QList<QPair<QString, int> > val;

public slots:
    void slot_bar_roll(void);

    /*内容换肤*/
public:
    struct colorSkin
    {
        QColor color_1;
        QColor color_2;
        QColor color_3;
        QString progress_color1;
        QString progress_color2;
    };
    colorSkin mySkin[4];
    int curr_skin;//当前皮肤
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBarCategoryAxis>
#include <QValueAxis>
#include <QBarSet>
#include<QChartView>
#include<QMouseEvent>
#include<QDebug>
#include<QPointF>
#include<QGraphicsSimpleTextItem>
QT_CHARTS_USE_NAMESPACE


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
         m_lastPoint = event->pos();
         m_isPress = true;
         qDebug()<<m_lastPoint.rx()<<"\t"<<m_lastPoint.ry();
    }
 }

 void MainWindow::mouseMoveEvent(QMouseEvent *event)
 {
     if (m_isPress)
     {

     }



 }

 void MainWindow::mouseReleaseEvent(QMouseEvent *event)
 {
     m_isPress = false;
     if (event->button() == Qt::RightButton)
     {
        if (m_alreadySaveRange)
        {

        }
     }
 }



void MainWindow::bar_init()
{
    barH = new QBarSet("High");         //设置上方数据
    barH->setColor(mySkin[curr_skin].color_1);   //设置颜色
    barL = new QBarSet("Low");          //设置下方数据
    barL->setColor(mySkin[curr_skin].color_2);  //设置颜色
    *barL << -22 << -10 << -9.3 << -17.0 << -15.6 << -8.0
        << -6.0 << -11.8 << -9.7 << -12.8 << -23.0 << -28.0;
    *barH << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8
        << 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8;

    series_bar = new QStackedBarSeries(this);
    //添加数据
    series_bar->append(barL);
    series_bar->append(barH);

    Chart_bar = new QChart();
    Chart_bar->addSeries(series_bar);
    QFont font_spline("Arial", 11);
    Chart_bar->setTitleFont(font_spline);
    Chart_bar->setTitle("QPieSeries");                         // 设置标题
    Chart_bar->setAnimationOptions(QChart::SeriesAnimations);  //开启动画效果
    Chart_bar->setAnimationDuration(1000);

    //设置X轴
    QBarCategoryAxis* axisX = new QBarCategoryAxis();
    QStringList strlist = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
    axisX->append(strlist);
    Chart_bar->addAxis(axisX, Qt::AlignBottom); //设置X坐标位置
    //设置Y轴
    QValueAxis* axisY = new QValueAxis();
    axisY->setTickCount(11);
    axisY->setLabelFormat("%d");
    axisY->setRange(0, 50);

    Chart_bar->addAxis(axisY, Qt::AlignLeft);   //设置Y坐标位置
    series_bar->attachAxis(axisX);
    series_bar->attachAxis(axisY);

    ui->graphicsView_bar->setRenderHint(QPainter::Antialiasing);// 设置抗锯齿
    ui->graphicsView_bar->setChart(Chart_bar);
}

/*更新数据*/
void MainWindow::slot_bar_roll(void)
{
    barH->setColor(mySkin[curr_skin].color_1);   //设置颜色
    barL->setColor(mySkin[curr_skin].color_2);  //设置颜色

    for (int i = 0; i < 12; i++)
    {
        barH->replace(i, rand() % 40);
        barL->replace(i, -rand() % 30);
    }
}



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



    mySkin[0] = { QColor(252,77,62),QColor(21,23,49),QColor(200,29,14),"252,77,62","21,23,49" };              //商务
    mySkin[1] = { QColor(49,177,72),QColor(26,142,199),QColor(61,222,85),"49,177,72","26,142,199" };          //夏日
    mySkin[2] = { QColor(138,103,205),QColor(137,204,208),QColor(122,64,232),"138,103,205","137,204,208" };   //清晰
    mySkin[3] = { QColor(252,53,152),QColor(83,207,199),QColor(200,30,114),"252,53,152","83,207,199" };       //猛男
    curr_skin = 0;



    //spline_init();      //曲线初始化
    bar_init();         //柱状图初始化



    //读取数据并在坐标系下绘制坐标点
    connect(ui->down,&QPushButton::clicked,[=]()
    {

        //获取鼠标位置,

        //获取按下时位置

        //获取移动时实时位置

        //获取松开时位置




        for (int i = 0; i < 12; i++)
        {
            //void QBarSet::append(const QList<qreal> &values)
            barH->replace(i, 40);
            //barL->replace(i, -rand() % 30);
        }



    });
}

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

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

ui_mainwindow.h

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 5.15.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFrame>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
#include <qchartview.h>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QFrame *frame;
    QFrame *frame_4;
    QChartView *graphicsView_bar;
    QFrame *frame_5;
    QPushButton *pushButton_2;
    QPushButton *up;
    QPushButton *down;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(910, 566);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        frame = new QFrame(centralwidget);
        frame->setObjectName(QString::fromUtf8("frame"));
        frame->setGeometry(QRect(10, 10, 161, 541));
        frame->setFrameShape(QFrame::StyledPanel);
        frame->setFrameShadow(QFrame::Raised);
        frame_4 = new QFrame(frame);
        frame_4->setObjectName(QString::fromUtf8("frame_4"));
        frame_4->setGeometry(QRect(0, 330, 161, 80));
        frame_4->setFrameShape(QFrame::StyledPanel);
        frame_4->setFrameShadow(QFrame::Raised);
        graphicsView_bar = new QChartView(centralwidget);
        graphicsView_bar->setObjectName(QString::fromUtf8("graphicsView_bar"));
        graphicsView_bar->setGeometry(QRect(170, 10, 721, 461));
        graphicsView_bar->setStyleSheet(QString::fromUtf8(""));
        frame_5 = new QFrame(centralwidget);
        frame_5->setObjectName(QString::fromUtf8("frame_5"));
        frame_5->setGeometry(QRect(190, 470, 701, 81));
        frame_5->setFrameShape(QFrame::StyledPanel);
        frame_5->setFrameShadow(QFrame::Raised);
        pushButton_2 = new QPushButton(frame_5);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(510, 20, 151, 41));
        up = new QPushButton(frame_5);
        up->setObjectName(QString::fromUtf8("up"));
        up->setGeometry(QRect(50, 20, 151, 41));
        down = new QPushButton(frame_5);
        down->setObjectName(QString::fromUtf8("down"));
        down->setGeometry(QRect(270, 20, 151, 41));
        MainWindow->setCentralWidget(centralwidget);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
        pushButton_2->setText(QCoreApplication::translate("MainWindow", "\344\277\235\345\255\230\351\205\215\347\275\256", nullptr));
        up->setText(QCoreApplication::translate("MainWindow", "\344\270\212\351\241\265", nullptr));
        down->setText(QCoreApplication::translate("MainWindow", "\344\270\213\351\241\265", nullptr));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天生_13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值