qt中Qchart和qcustomplot二者的简单应用

//Qchart:
void MainWindow::Chart_Init()
{
   
    //初始化QChart的实例
    chart = new QChart();
    //初始化QSplineSeries的实例
    lineSeries  = new QSplineSeries();
    lineSeries1 = new QSplineSeries();
    lineSeries2 = new QSplineSeries();
    //设置曲线的名称
    lineSeries ->setName("------");
    lineSeries1->setName("------");
    lineSeries2->setName("------");
    //把曲线添加到QChart的实例chart中
    chart->addSeries(lineSeries);
    chart->addSeries(lineSeries1);
    chart->addSeries(lineSeries2);
    //声明并初始化X轴、两个Y轴
    QValueAxis *axisX  = new QValueAxis();
    QValueAxis *axisY  = new QValueAxis();
    QValueAxis *axisY1 = new QValueAxis();
    //设置坐标轴显示的范围
    axisX->setMin(0);
    axisX->setMax(MAX_X);
    axisY->setMin(0);
    axisY->setMax(100);
    axisY1->setMin(900);
    axisY1->setMax(1200);
    //设置坐标轴上的格点
    axisX->setTickCount(10);
    axisY->setTickCount(8);
    axisY1->setTickCount(8);
    //设置坐标轴显示的名称
    QFont font("Microsoft YaHei",8,QFont::Normal);//微软雅黑。字体大小8
    axisX->setTitleFont(font);
    axisY->setTitleFont(font);
    axisY1->setTitleFont(font);
    axisX->setTitleText("X-Time");
    axisY->setTitleText("T&H_number");
    axisY1->setTitleText("Pres_number");
    //设置网格不显示
    axisY->setGridLineVisible(false);
    axisY1->setGridLineVisible(false);
    //下方:Qt::AlignBottom,左边:Qt::AlignLeft
    //右边:Qt::AlignRight,上方:Qt::AlignTop
    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    chart->addAxis(axisY1,Qt::AlignRight);
    //把曲线关联到坐标轴
    lineSeries->attachAxis(axisX);
    lineSeries->attachAxis(axisY);
    lineSeries1->attachAxis(axisX);
    lineSeries1->attachAxis(axisY);
    lineSeries2->attachAxis(axisX);
    lineSeries2->attachAxis(axisY1);
    //把chart显示到窗口上
    ui->graphicsView->setChart(chart);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing); //设置渲染:抗锯齿,如果不设置那么曲线就显得不平滑
}
void MainWindow::DrawLine()
{
    //count表示x轴的坐标变化
    static int count = 0;
    if(count > MAX_X)
    {
      //当曲线上最早的点超出X轴的范围时,剔除最早的点,
      lineSeries->removePoints(0,lineSeries->count() - MAX_X);
      // 更新X轴的范围
      chart->axisX()->setMin(count - MAX_X);
      chart->axisX()->setMax(count);
    }
    //增加新的点到曲线末端(数据处理部分,count为x轴,其余为三条曲线的对应的y轴数据)
    lineSeries ->append(count, str3.toFloat());
    //qDebug()<<str3.toFloat();
    lineSeries1->append(count, str4.toFloat());
    //qDebug()<<str4.toFloat();
    lineSeries2->append(count, str5.toFloat());
    //qDebug()<<str5.toFloat();
    count ++;
}
void MainWindow::QPlot_init()
{
    //对坐标轴的颜色操作(注释,可自行修改):
    // ui->w_plot->xAxis->setBasePen(QPen(Qt::black, 2));
    // ui->w_plot->yAxis->setBasePen(QPen(Qt::black, 2));//图标网格线的外观颜色
    // ui->w_plot->xAxis->setTickPen(QPen(Qt::red, 2));
    // ui->w_plot->yAxis->setTickPen(QPen(Qt::red, 2));//图标主要刻度线的外观颜色
    // ui->w_plot->xAxis->setSubTickPen(QPen(Qt::black, 2));
    // ui->w_plot->yAxis->setSubTickPen(QPen(Qt::black, 2));//图标次要刻度线的外观颜色
    // ui->w_plot->xAxis->setTickLabelColor(Qt::black);
    // ui->w_plot->yAxis->setTickLabelColor(Qt::black);//图标主要刻度线标签文本的外观颜色

    // ui->w_plot->xAxis->grid()->setPen(QPen(Qt::red, 1, Qt::DotLine));
    // ui->w_plot->yAxis->grid()->setPen(QPen(Qt::red, 1, Qt::DotLine));
    // ui->w_plot->xAxis->grid()->setSubGridPen(QPen(Qt::blue, 1, Qt::DotLine));
    // ui->w_plot->yAxis->grid()->setSubGridPen(QPen(Qt::blue, 1, Qt::DotLine));
    // ui->w_plot->xAxis->grid()->setSubGridVisible(true);
    // ui->w_plot->yAxis->grid()->setSubGridVisible(true);
    // ui->w_plot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
    // ui->w_plot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);

    //坐标轴字体和注释(字体、颜色、标签):
    ui->w_plot->yAxis->setLabelFont(QFont("微软雅黑",12));
    ui->w_plot->xAxis->setLabelFont(QFont("微软雅黑",12));
    ui->w_plot->yAxis->setLabelColor(QColor (85,85,85));
    ui->w_plot->xAxis->setLabelColor(QColor (85,85,85));
    ui->w_plot->yAxis->setLabel("--------");
    ui->w_plot->xAxis->setLabel("Timer");
    ui->w_plot->xAxis2->setLabelFont(QFont("微软雅黑",12));
    ui->w_plot->yAxis2->setLabelColor(QColor (85,85,85));
    ui->w_plot->yAxis2->setLabel("--------");

    ui->w_plot->xAxis->setRange(0,100);
    //ui->w_plot->xAxis->rescale(true);
    ui->w_plot->yAxis->setRange(0,80);
    ui->w_plot->yAxis->rescale(true);
    // 图表添加曲线(关联坐标轴)
    xGraph = ui->w_plot->addGraph();
    yGraph = ui->w_plot->addGraph();
    zGraph = ui->w_plot->addGraph(ui->w_plot->xAxis,ui->w_plot->yAxis2);

    ui->w_plot->graph(0)->rescaleAxes(true);
    ui->w_plot->graph(1)->rescaleAxes(true);
    ui->w_plot->graph(2)->rescaleAxes(true);

    ui->w_plot->yAxis2->setVisible(true);
    ui->w_plot->yAxis2->setRange(800,1200);
    ui->w_plot->yAxis2->rescale(true);

    // 设置曲线颜色
    xGraph->setPen(QPen(Qt::red));
    yGraph->setPen(QPen(Qt::green));
    zGraph->setPen(QPen(Qt::black));

    //设置图例
    ui->w_plot->legend->setVisible(true);
    xGraph->setName("hdmi-left");
    yGraph->setName("temp-left");
    zGraph->setName("pree-right");

    ui->w_plot->xAxis->setTickLength(0, 5);
    ui->w_plot->xAxis->setSubTickLength(0, 3);
    ui->w_plot->yAxis2->setTickPen(QPen(Qt::blue, 2));

    ui->w_plot->yAxis->setTickLength(3, 3);
    ui->w_plot->yAxis->setSubTickLength(1, 1);
    ui->w_plot->yAxis->setTickPen(QPen(Qt::blue, 2));

    ui->w_plot->yAxis2->setTickLength(3, 3);
    ui->w_plot->yAxis2->setSubTickLength(1, 1);
    ui->w_plot->yAxis2->setTickPen(QPen(Qt::blue, 2));
    // 允许用户用鼠标拖动轴范围,用鼠标滚轮缩放,点击选择图形:
    //ui->w_plot->rescaleAxes();//坐标轴缩放
    ui->w_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes
                                | QCP::iSelectPlottables);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值