QCustomPlot笔记——图例设置
///标题和图例设置
ui->customplot_bar->plotLayout()->insertRow(0);
QCPTextElement *title = new QCPTextElement(ui->customplot_bar, "bar", QFont("sans", 12, QFont::Bold));
ui->customplot_bar->plotLayout()->addElement(0, 0, title);
title->setTextColor(QColor(138,180,219)); //设置标题的颜色
QCPBars *bars1 = new QCPBars(ui->customplot_bar->xAxis, ui->customplot_bar->yAxis);
bars1->setPen(QPen(QColor(138,180,219)));
bars1->setName("RVP900");
QCPBars *bars2 = new QCPBars(ui->customplot_bar->xAxis, ui->customplot_bar->yAxis);
bars2->setPen(QPen(QColor(138,180,219)));
bars2->setName("敏视达");
// ui->customplot_bar->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignCenter|Qt::AlignTop); //图例居中显示
ui->customplot_bar->legend->setVisible(true);
ui->customplot_bar->legend->setFont(QFont("Helvetica", 8));
ui->customplot_bar->legend->setTextColor(QColor(138,180,219));
ui->customplot_bar->legend->setBrush(QBrush(Qt::transparent)); //设置图例透明无边框
ui->customplot_bar->legend->setBorderPen(Qt::NoPen);
//将图例设置成横排显示
ui->customplot_bar->legend->addElement(0,1,ui->customplot_bar->legend->item(1));
ui->customplot_bar->legend->addElement(0,2,ui->customplot_bar->legend->item(2));
图例置于底部
ui->customplot_bar->legend->setMargins(QMargins(320,1,1,1));
int count = ui->customplot_bar->plotLayout()->rowCount();
ui->customplot_bar->plotLayout()->addElement(count,0, ui->customplot_bar->legend);
ui->customplot_bar->plotLayout()->setRowStretchFactor(count, 0.0001); //设置行的伸展因子
ui->customplot_bar->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignRight|Qt::AlignTop);
参考文章:https://blog.****.net/chyuanrufeng/article/details/83056661