QCustomPlot的使用之三

16 篇文章 2 订阅

最近项目需要绘图功能,所以还是采用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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值