cpp文件

#include "mform.h"
#include "ui_mform.h"
#include <QBarSeries>
#include <QBarSet>
#include <QtCharts>
#include <QPushButton>
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCharts/QAbstractBarSeries>
#include <QtCharts/QPercentBarSeries>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QLegend>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtCharts/QBarCategoryAxis>
#include <QtWidgets/QApplication>
#include <QtCharts/QValueAxis>
#include <QVector>
#include <QDebug>

QVector<int>data0;
QVector<int>data1;
QVector<int>data2;
QVector<int>data3;

using namespace QtCharts;

MForm::MForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MForm)
{
    ui->setupUi(this);
    for (int i = 0; i < 6; ++i) {
        data0.push_front(0);
        data1.push_front(0);
        data2.push_front(0);
        data3.push_front(0);
    }
    connect(ui->pushButton,&amp;QPushButton::clicked,[=](){
        data0.push_front(ui->lineEdit_1->text().toInt());
        data1.push_front(ui->lineEdit_2->text().toInt());
        data2.push_front(ui->lineEdit_3->text().toInt());
        data3.push_front(ui->lineEdit_4->text().toInt());

        QBarSet *set0 = new QBarSet("日光(min)");
        QBarSet *set1 = new QBarSet("UV(min)");
        QBarSet *set2 = new QBarSet("日光(month)");
        QBarSet *set3 = new QBarSet("UV(month)");

        QVector<int>::iterator it1 = data0.begin();
        QVector<int>::iterator it2 = data1.begin();
        QVector<int>::iterator it3 = data2.begin();
        qDebug()<<it1<<"|"<<it2<<"|"<<it3;
        //if(i)
        for(QVector<int>::iterator it4 = data3.begin();it4<data3.begin()+5;it4++)
        {
            *set0 << *it1<< *(it1+1) << *(it1+2) << *(it1+3) << *(it1+4) << *(it1+5);
            *set1 << *it2<< *(it2+1) << *(it2+2) << *(it2+3) << *(it2+4) << *(it2+5);
            *set2 << *it3<< *(it3+1) << *(it3+2) << *(it3+3) << *(it3+4) << *(it3+5);
            *set3 << *it4<< *(it4+1) << *(it4+2) << *(it4+3) << *(it4+4) << *(it4+5);
            it1++;
            it2++;
            it3++;
        }
    //    *set0 << 5 << 2 << 3 << 4 << 5 << 6;
    //    *set1 << 7 << 0 << 0 << 4 << 0 << 7;
    //    *set2 << 9 << 5 << 8 << 19<< 8 << 5;
    //    *set3 << 5 << 6 << 7 << 3 << 4 << 5;
        QBarSeries *series = new QBarSeries();
        series->append(set0);
        series->append(set1);
        series->append(set2);
        series->append(set3);
        QChart *c = new QChart();
        c->setTitle("光照UV强度");
        c->addSeries(series);
        ui->graphicsView->setChart(c);
        c->legend()->setVisible(true);
        c->setAnimationOptions(QChart::SeriesAnimations);
        ui->graphicsView->setRenderHint(QPainter::Antialiasing);
        c->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值)
        QValueAxis *axisX = new QValueAxis();//轴变量数据系列变量,都不能声明局部临时变量
        QValueAxis *axisY = new QValueAxis();//创建X/Y轴
        axisX->setRange(0, 6);
        axisY->setRange(0, 10);//设置X/Y显示区间
        c->setAxisX(axisX);
        c->setAxisY(axisY);//设置chart坐标轴
        series->attachAxis(axisX);
        c->legend()->setVisible(true); //设置图例显示状态
        c->legend()->setAlignment(Qt::AlignBottom);//设置图例显示位置底部

    });
}

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

.h文件

#ifndef MFORM_H
#define MFORM_H

#include <QWidget>
#include <QtCharts>
namespace Ui {
class MForm;
}

class MForm : public QWidget
{
    Q_OBJECT

public:
    explicit MForm(QWidget *parent = 0);
    ~MForm();

private:
    Ui::MForm *ui;
};

#endif // MFORM_H

ui文件

原文地址:https://blog.csdn.net/aisheisadas/article/details/134767758

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37450.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注