Qt 使用 qcustomplot 绘制柱状图

ClgBar::ClgBar(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::ClgBar)

{

    ui->setupUi(this);


    QCustomPlot *  m_rectPlot = new QCustomPlot;

    ui->verticalLayout->addWidget( m_rectPlot );


    m_rectPlot->xAxis->setLabel("参数");

    m_rectPlot->yAxis->setLabel("个数");

    QLinearGradient gradient(0, 0, 0, 400);

    gradient.setColorAt(0, QColor(218, 218, 218));

    m_rectPlot->setBackground(QBrush(gradient));


    QCPBars *bars = new QCPBars(m_rectPlot->xAxis, m_rectPlot->yAxis);

    bars->addData(1.0, 20.0);

    bars->setName(tr("测试"));

    bars->setPen(QPen(QColor(0,255,0)));

    bars->setBrush(QBrush(QColor(255,255,0)));

    m_rectPlot->rescaleAxes();

    m_rectPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    m_rectPlot->replot();

}

QCPBars *bars = new QCPBars(m_rectPlot->xAxis, m_rectPlot->yAxis);

    bars->setWidth(2);

    QVector xLineVector, yLineVector;

    xLineVector << 1 << 4 << 8 << 12 << 16 << 20 << 24;

    yLineVector << 4 << 1 << 2 << 4 << 5 << 6 << 7;

    //bars->setData(xLineVector, yLineVector);

    bars->addData(xLineVector, yLineVector);

    //bars->addData(10, 20.0);//添加一个

    bars->setName(tr("测试"));

    bars->setPen(QPen(QColor(255,0,0)));//边框夜色

    bars->setBrush(QBrush(QColor(255,255,0)));

    //m_rectPlot->rescaleAxes();//缩放坐标轴

    //设置为可拖拽、 可放大缩小、可选中

    m_rectPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    m_rectPlot->replot();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容