最近项目需要绘图功能,所以还是采用QCustomPlot来绘制,首先写了几个Demo.
void UserCustomPlot::plot2() {
//设置渐变色背景
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));
setBackground(QBrush(gradient));
//设置三个柱形图
QCPBars *first = new QCPBars(xAxis, yAxis);
QCPBars *second = new QCPBars(xAxis, yAxis);
QCPBars *thrid = new QCPBars(xAxis, yAxis);
//是否抗锯齿
first->setAntialiased(false);
second->setAntialiased(false);
thrid->setAntialiased(false);
//两个圆柱之间的距离,单位:像素
first->setStackingGap(1);
second->setStackingGap(1);
thrid->setStackingGap(1);
thrid->setName(QStringLiteral("男人"));
thrid->setPen(QPen(QColor(111, 9, 176).lighter(170)));
thrid->setBrush(QColor(111, 9, 176));
second->setName(QStringLiteral("女人"));
second->setPen(QPen(QColor(250, 170, 20).lighter(150)));
second->setBrush(QColor(250, 170, 20));
first->setName(QStringLiteral("其他"));
first->setPen(QPen(QColor(0, 168, 140).lighter(130)));
first->setBrush(QColor(0, 168, 140));
//设置位置
second->moveAbove(thrid);
first->moveAbove(second);
//文本坐标
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
labels << QStringLiteral("北京") << QStringLiteral("上海")
<< QStringLiteral("广州") << QStringLiteral("深圳")
<< QStringLiteral("石家庄") << QStringLiteral("衡水") << QStringLiteral("雄安");
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
xAxis->setTicker(textTicker);
xAxis->setRange(0, 8);
yAxis->setRange(0, 12.1);
yAxis->setLabel(QStringLiteral("人口数量"));
//设置数据
QVector<double> fossilData, nuclearData, regenData;
fossilData << 0.86*10.5 << 0.83*5.5 << 0.84*5.5 << 0.52*5.8 << 0.89*5.2 << 0.90*4.2 << 0.67*11.2;
nuclearData << 0.08*10.5 << 0.12*5.5 << 0.12*5.5 << 0.40*5.8 << 0.09*5.2 << 0.00*4.2 << 0.07*11.2;
regenData << 0.06*10.5 << 0.05*5.5 << 0.04*5.5 << 0.06*5.8 << 0.02*5.2 << 0.07*4.2 << 0.25*11.2;
first->setData(ticks, fossilData);
second->setData(ticks, nuclearData);
thrid->setData(ticks, regenData);
//比例尺
axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignHCenter);
legend->setBrush(QColor(255, 255, 255, 100));
legend->setBorderPen(Qt::NoPen);
}
void UserCustomPlot::plot3() {
QCPItemText *textLabel = new QCPItemText(this);
textLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
textLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
//矩形区域
textLabel->position->setCoords(0.5, 0.5);
textLabel->setText(QStringLiteral("显示文本"));
textLabel->setFont(QFont(font().family(), 16)); // make font a bit larger
textLabel->setPen(QPen(Qt::black)); // show black border around text
QCPItemLine *arrow = new QCPItemLine(this);
arrow->start->setParentAnchor(textLabel->bottom);
arrow->end->setCoords(0, 0);
arrow->setHead(QCPLineEnding::esSpikeArrow);
}
aaa