Qt之qcustomplot绘柱图(柱图重叠问题)

    //避免柱图重叠,每次绘制之前柱图清除一次
    int plottableCount = ui->qcustomPlot1->plottableCount();
    if (plottableCount != 0)
          ui->qcustomPlot1->clearPlottables();

 
    QVector<QString> labels(5);
    QVector<double> values(5);
 
    labels[0]=QString("Co");
    labels[1]=QString("H2s");
    labels[2]=QString("O2");
    labels[3]=QString("Gas");
    labels[4]=QString("电量");
 
    values[0]=d1;
    values[1]=d2;
    values[2]=d3;
    values[3]=d4;
    values[4]=d5;
 
    //QCustomPlot提供的QCPBars来表示柱状图
    QCPBars* bars=new QCPBars(ui->qcustomPlot1->xAxis,ui->qcustomPlot1->yAxis);
    bars->setAntialiased(false);
    bars->setPen(QPen(QColor(0, 168, 140).lighter(130)));
    bars->setBrush(QColor(0, 168, 140));
 
    QVector<double> index(5);
    for(int i=0;i<5;++i)
       index[i]=i;
    //QCPBars的setData()的两个参数也是两个向量,只不过第一个向量index的每个元素表示“第几个柱子”,然后后面对应的values表示对应“柱子的值”
    bars->setData(index,values);
 
    ui->qcustomPlot1->xAxis->setAutoTicks(false);
    ui->qcustomPlot1->xAxis->setAutoTickLabels(false);
    ui->qcustomPlot1->xAxis->setAutoTickStep(false);
    ui->qcustomPlot1->addPlottable(bars);
    ui->qcustomPlot1->rescaleAxes();
 
    double wid=ui->qcustomPlot1->xAxis->range().upper-ui->qcustomPlot1->xAxis->range().lower;
    double cl=bars->width()+(1.0*wid-bars->width()*5)/4;
 
    QVector<double> coor;
    for(int i=0;i<5;++i)
            coor.append(ui->qcustomPlot1->xAxis->range().lower+i*cl+bars->width()/2);
    ui->qcustomPlot1->xAxis->setTickVector(coor);
    ui->qcustomPlot1->xAxis->setTickVectorLabels(labels);
    ui->qcustomPlot1->replot();

                
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值