基于QCustomplot实现帕累托图

一、什么是帕累托图?

二、相关代码

void MainWindow::setParetoDemo(QCustomPlot *customPlot)
{
    demoName = "Bar Chart Demo";
    // set dark background gradient:
    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(105, 105, 105));
    gradient.setColorAt(1, QColor(70, 70, 70));
    customPlot->setBackground(QBrush(gradient));

    // create empty bar chart objects:
    QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    //按比列设置柱状宽度,相当于100%
    regen->setWidth(1);
    regen->setAntialiased(false); // gives more crisp, pixel aligned bar borders
    regen->setStackingGap(1);

    regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
    regen->setBrush(QColor(0, 168, 140));

    // prepare x axis with country labels:
    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 << 4 << 5 << 6 ;
    labels << "1" << "2" << "3" << "4" << "5" << "6" ;
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(ticks, labels);
    customPlot->xAxis->setTicker(textTicker);
    customPlot->xAxis->setLabel("编号");
    customPlot->xAxis->setTickLabelRotation(60);
    customPlot->xAxis->setSubTicks(false);
    customPlot->xAxis->setTickLength(0, 4);
    customPlot->xAxis->setRange(0.5, 8);

    customPlot->xAxis->setBasePen(QPen(Qt::white));
    customPlot->xAxis->setTickPen(QPen(Qt::white));
    customPlot->xAxis->grid()->setVisible(true);
    customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    customPlot->xAxis->setTickLabelColor(Qt::white);
    customPlot->xAxis->setLabelColor(Qt::white);

    QVector<double> yticks2;
    QVector<QString> ylabels2;
    yticks2 << 0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
    ylabels2 <<  "0%" << "10%" << "20%" << "30%" << "40%" << "50%" << "60%" << "70%" << "80%" << "90%" << "100%";
    QSharedPointer<QCPAxisTickerText> ytextTicker2(new QCPAxisTickerText);
    ytextTicker2->addTicks(yticks2, ylabels2);
    customPlot->yAxis2->setTicker(ytextTicker2);
    customPlot->yAxis2->setRange(0, 10);
    customPlot->yAxis2->setTickLength(0, 4);
    customPlot->yAxis2->setSubTicks(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setBasePen(QPen(Qt::white));
    customPlot->yAxis2->setTickPen(QPen(Qt::white));
    customPlot->yAxis2->setTickLabelColor(Qt::white);
    customPlot->yAxis2->setLabelColor(Qt::white);

    // prepare y axis:
    customPlot->yAxis->setRange(0, 20);
    customPlot->yAxis->setPadding(5); // a bit more space to the left border
    customPlot->yAxis->setLabel("次数");
    customPlot->yAxis->setBasePen(QPen(Qt::white));
    customPlot->yAxis->setTickPen(QPen(Qt::white));
    customPlot->yAxis->setSubTickPen(QPen(Qt::white));
    customPlot->yAxis->grid()->setSubGridVisible(true);
    customPlot->yAxis->setTickLabelColor(Qt::white);
    customPlot->yAxis->setLabelColor(Qt::white);
    customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
    customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

    // Add data:
    QVector<double> fossilData;
    fossilData  << 18 << 12 << 6 << 4 << 0.89*5.2 << 0.90*4.2 << 0.67*11.2;

    QVector<double> ticks2;
    ticks2 << 1 << 2 << 3 << 4;
    regen->setData(ticks2, fossilData);
    //添加折线
    auto graph = customPlot->addGraph(customPlot->xAxis, customPlot->yAxis2);
    QVector<double> percent;
    QVector<double> ticks3;
    ticks3 << 0.5 << 1 << 2 << 3 << 4 << 5 << 6 << 7 ;
    percent << 0 << 4.5 << 7.5 << 9 << 10 << 10 << 10 << 10;
    graph->setData(ticks3, percent);
    QPen p = graph->pen();
    p.setWidth(3);
    p.setColor(QColor(240,210, 177));
    graph->setPen(p);

    // setup legend:
    customPlot->legend->setVisible(false);
    customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    customPlot->legend->setBrush(QColor(255, 255, 255, 100));
    customPlot->legend->setBorderPen(Qt::NoPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    customPlot->legend->setFont(legendFont);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

三、效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值