214_QT_最初级的曲线图

214_QT_最初级的曲线图

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QChartView>
QT_CHARTS_USE_NAMESPACE
#include <QLineSeries>
#include <QtMath>
#include <QValueAxis>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建图标
    QChartView *chartView = new QChartView(this);
    QChart *chart = new QChart();
    chart->setTitle(QString::fromLocal8Bit("简单函数曲线"));

    chartView->setChart(chart);
    setCentralWidget(chartView);

    //创建折现序列
    QLineSeries *series0 = new  QLineSeries;
    QLineSeries *series1 = new  QLineSeries;
    series0->setName(QString::fromLocal8Bit("sin曲线"));
    series1->setName(QString::fromLocal8Bit("cos曲线"));
    chart->addSeries(series0);
    chart->addSeries(series1);

    //添加数据列
    qreal y0, y1, t = 0, intv = 0.1;
    int cnt = 100;
    for(int i = 0; i < cnt; i++)
    {
        y0 = qSin(t);
        series0->append(t, y0);
        y1 = qCos(t);
        series1->append(t, y1);
        t+=intv;
    }

    //创建坐标轴,给定坐标点
    QValueAxis * xAxis = new  QValueAxis;
    xAxis->setRange(0, 10);
    chart->setAxisX(xAxis, series0);
    chart->setAxisX(xAxis, series1);

    QValueAxis * yAxis = new  QValueAxis;
    yAxis->setRange(-1, 1);
    chart->setAxisY(yAxis, series0);
    chart->setAxisY(yAxis, series1);
}

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


上一篇:-(四)Set、Map、(Weak)、Symbol和BigInt


下一篇:k8s的应用包管理工具helm的部署和使用