QCustomPlot - 柱状堆积图

embedded/2024/10/18 8:33:35/

参考链接
显示柱状图的值

QCustomPlot下载

下载地址:https://www.qcustomplot.com/index.php/download选择版本2.1.0 QCustomPlot.tar.gz

QCustomPlot 的使用

解压下载的文件
把qcustomplot.h和qcustomplot.cpp放到自己的项目工程(复制文件并qt 的目录树添加存在的头文件源文件)。在使用QCustomPlot类的地址包含头文件 #include "qcustomplot.h"
添加printsupport
如果Qt版本在5.0以上,需要在.pro文件中的QT变量加上printsupport:
QT += widgets printsupport
添加帮助文档
在下载的documentation文件夹下有个qcustomplot.qch文件,将它拷贝Qt的安装文档目录下(一般为qt5.9\Docs\Qt-5.9,会根据你的Qt版本号而做相应变动),然后在QtCreator ——>工具——>选项——>帮助——>文档——>添加,选择qcustomplot.qch文件,确定,以后按F1就能跳转到QCustomPlot的帮助

实例Demo

void MainWindow::setupBarChartDemo(QCustomPlot *customPlot)
{demoName = "Bar Chart Demo";// set dark background gradient: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));customPlot->setBackground(QBrush(gradient));// create empty bar chart objects:QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);QCPBars *nuclear = new QCPBars(customPlot->xAxis, customPlot->yAxis);QCPBars *fossil = new QCPBars(customPlot->xAxis, customPlot->yAxis);regen->setAntialiased(false); // gives more crisp, pixel aligned bar bordersnuclear->setAntialiased(false);fossil->setAntialiased(false);regen->setStackingGap(1);nuclear->setStackingGap(1);fossil->setStackingGap(1);// set names and colors:fossil->setName("Fossil fuels");fossil->setPen(QPen(QColor(111, 9, 176).lighter(170)));fossil->setBrush(QColor(111, 9, 176));nuclear->setName("Nuclear");nuclear->setPen(QPen(QColor(250, 170, 20).lighter(150)));nuclear->setBrush(QColor(250, 170, 20));regen->setName("Regenerative");regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));regen->setBrush(QColor(0, 168, 140));// stack bars on top of each other:nuclear->moveAbove(fossil);regen->moveAbove(nuclear);// prepare x axis with country labels:QVector<double> ticks;QVector<QString> labels;ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);textTicker->addTicks(ticks, labels);customPlot->xAxis->setTicker(textTicker);customPlot->xAxis->setTickLabelRotation(60);customPlot->xAxis->setSubTicks(false);customPlot->xAxis->setTickLength(0, 4);customPlot->xAxis->setRange(0, 8);customPlot->xAxis->setBasePen(QPen(Qt::white));customPlot->xAxis->setTickPen(QPen(Qt::white));customPlot->xAxis->grid()->setVisible(true);customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));customPlot->xAxis->setTickLabelColor(Qt::white);customPlot->xAxis->setLabelColor(Qt::white);// prepare y axis:customPlot->yAxis->setRange(0, 12.1);customPlot->yAxis->setPadding(5); // a bit more space to the left bordercustomPlot->yAxis->setLabel("Power Consumption in\nKilowatts per Capita (2007)");customPlot->yAxis->setBasePen(QPen(Qt::white));customPlot->yAxis->setTickPen(QPen(Qt::white));customPlot->yAxis->setSubTickPen(QPen(Qt::white));customPlot->yAxis->grid()->setSubGridVisible(true);customPlot->yAxis->setTickLabelColor(Qt::white);customPlot->yAxis->setLabelColor(Qt::white);customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));// Add data: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;fossil->setData(ticks, fossilData);nuclear->setData(ticks, nuclearData);regen->setData(ticks, regenData);// setup legend:customPlot->legend->setVisible(true);customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);customPlot->legend->setBrush(QColor(255, 255, 255, 100));customPlot->legend->setBorderPen(Qt::NoPen);QFont legendFont = font();legendFont.setPointSize(10);customPlot->legend->setFont(legendFont);customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);//这里可以显示文本(思路)
//  for (int i = 0; i < fossil->dataCount(); ++i) {
//      QCPItemText *text = new QCPItemText(customPlot);
//      text->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);//      double key = regen->data()->at(i)->key;
//      double value = regen->getStackedBaseValue(regen->data()->at(i)->key, true)+ regen->data()->at(i)->value;
//      text->position->setCoords(key, value); // 将文本位置设置在 bar 的正上方
//      QString valueStr = QString::number(regen->data()->at(i)->value); // 将数值转换为字符串
//      text->setText(valueStr); // 设置文本内容
//      text->setFont(QFont(font().family(), 10)); // 设置文本字体大小
//      text->setPen(QPen(Qt::black)); // 设置文本颜色
//  }
}

效果

在这里插入图片描述


http://www.ppmy.cn/embedded/42056.html

相关文章

如何看待云计算的第三次浪潮?

如何看待云计算的第三次浪潮&#xff1f; 来自云栖大会的演讲你如何看待云计算的第三次浪潮&#xff1f;云计算的第三次浪潮将会给社会带来怎样的变革&#xff1f;开发者在云计算的第三次浪潮中将会有哪些机遇和挑战&#xff1f;机遇挑战 来自云栖大会的演讲 在2023云栖大会上…

C++并发:构建线程安全的队列

正文 线程安全队列的完整的类定义&#xff0c;其中采用了条件变量&#xff1a; #include <condition_variable> #include <memory> #include <mutex> #include <queue> template <typename T> class threadsafe_queue {private:mutable std::m…

PY32F403系列单片机,32位M4内核MCU,主频最高144MHZ

PY32F403系列单片机是基于Arm Cortex-M4核的32位通用微控制器产品。内置的FPU和DSP功能支持浮点运算和全部DSP指令。通过平衡成本&#xff0c;性能&#xff0c;功耗来获得更好的用户体验。 PY32F403单片机典型工作频率可达144MHZ&#xff0c;内置高速存储器&#xff0c;丰富的…

逻辑图这么搞, 哪个老板不拿你当个宝,有图有真相。

逻辑图作用在于把复杂的文字描述&#xff0c;抽象出来&#xff0c;形成可视化的图表&#xff0c;便于用户快速阅读&#xff0c;这种图在o B和to G的项目中经常用到&#xff0c;这次给大家分享一批。

Mac SourceTree配置ssh git仓库

一、准备条件 1、Mac系统电脑 2、安装好SourceTree 3、获取ssh git仓库地址 二、配置步骤 1、打开终端命令行 ssh -t rsa -C "xxx""xxx"代表注册git仓库时&#xff0c;使用的用户名&#xff0c;可以是字符串也可以是邮箱地址。 如果遇到输入密码&#xf…

HTML常见标签-段落标签

段落标签 段落标签一般用于定义一些在页面上要显示的大段文字,多个段落标签之间实现自动分段的效果 代码 <p>记者从工信部了解到&#xff0c;近年来我国算力产业规模快速增长&#xff0c;年增长率近30%&#xff0c;算力规模排名全球第二。</p><p>工信部统计…

【Leetcode每日一题】 综合练习 - 括号生成(难度⭐⭐)(76)

1. 题目解析 题目链接&#xff1a;22. 括号生成 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 问题描述 我们需要找出所有可能的、有效的括号序列。一个有效的括号序列指的是一个仅由(和)组成的字符串&#xff0c;…

【C】求Sn=a+aa+aaa+aaaa+aaaaa的前n项之和

问题 求Snaaaaaaaaaaaaaaa的前n项之和&#xff0c;其中a是一个数字。 例如&#xff1a;当a为2&#xff0c;n为5&#xff1a;Sn222222222222222 整体分析 像之前的水仙花数一样&#xff08;如果你看过这篇的话&#xff09;&#xff0c;我们可以先把这个问题拆分为一个个小的问…