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();