QCustomPlot-绘制X轴为日期的折线图

2024-03-19 23:36

本文主要是介绍QCustomPlot-绘制X轴为日期的折线图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要代码如下:

void Widget::InitQLineXDateAddData()
{customPlot = new QCustomPlot(this);// 创建日期时间类型的刻度生成器QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);dateTimeTicker->setDateTimeFormat("yyyy-MM-dd"); // 设置日期时间格式//dateTimeTicker->setTickCount(8);// 将刻度生成器应用到 X 轴customPlot->xAxis->setTicker(dateTimeTicker);customPlot->xAxis->setTickLabelRotation(45); // 设置刻度标签旋转角度,以便更好地显示// 设置 X 轴的范围(使用 QDateTime 转换为键)QDateTime startDateTime = QDateTime(QDate(2024, 3, 13), QTime(0, 0, 0), Qt::UTC);QDateTime endDateTime = QDateTime(QDate(2024, 3, 20), QTime(0, 0, 0), Qt::UTC); // 计算日期范围内的天数,并设置为刻度数量int numDays = startDateTime.daysTo(endDateTime) + 1; // 包括开始和结束日期dateTimeTicker->setTickCount(numDays);// 将 QDateTime 转换为自 Unix 纪元以来的秒数double startKey = startDateTime.toMSecsSinceEpoch() / 1000.0;double endKey = endDateTime.toMSecsSinceEpoch() / 1000.0;customPlot->xAxis->setRange(QCPRange(startKey, endKey));// 添加并设置两个图形的penQCPGraph *graph1 = customPlot->addGraph();graph1->setPen(QPen(Qt::blue));QCPGraph *graph2 = customPlot->addGraph();graph2->setPen(QPen(Qt::red));// 设置图例customPlot->legend->setVisible(true);graph1->setName("A产品");graph2->setName("B产品");customPlot->axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignBottom|Qt::AlignRight);graph1->setLineStyle(QCPGraph::lsLine); // 实线graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, QColor(Qt::blue), 6)); // 圆形散点graph2->setLineStyle(QCPGraph::lsLine); // 实线graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 6)); // 圆形散点QVector<double> keys1;int numValues = 6; // 要生成的值的数量for (int i = 0; i < numValues; ++i ) {keys1.push_back(startKey + i * 86400);}QVector<double> values1 = {100, 300, 500, 800, 900, 800};QVector<double> keys2 = {startKey, startKey + 86400, startKey + 2 * 86400, startKey + 3 * 86400, startKey + 4 * 86400, startKey + 5 * 86400};QVector<double> values2 = {100, 200, 400, 900, 1100, 600};// 设置数据并显示图表graph1->setData(keys1, values1);graph2->setData(keys2, values2);// 设置数据点标签显示for (int i = 0; i < keys1.size(); ++i) {QCPItemText *textItem = new QCPItemText(customPlot);//textItem->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);textItem->setPositionAlignment( Qt::AlignBottom | Qt::AlignHCenter);textItem->position->setType(QCPItemPosition::ptPlotCoords);textItem->position->setCoords(keys1[i], values1[i]);textItem->setText(QString::number(values1[i]));textItem->setFont(QFont(font().family(), 10)); // Set font size as needed//textItem->setPen(QPen(Qt::blue)); // Set text colortextItem->setPen(Qt::NoPen); // Set no pen (remove border)textItem->setBrush(QBrush(Qt::transparent)); // Set transparent brush to hide the background}// 设置数据点标签显示for (int i = 0; i < keys1.size(); ++i) {QCPItemText *textItem = new QCPItemText(customPlot);//textItem->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);textItem->setPositionAlignment( Qt::AlignBottom | Qt::AlignHCenter);textItem->position->setType(QCPItemPosition::ptPlotCoords);textItem->position->setCoords(keys1[i], values2[i]);textItem->setText(QString::number(values2[i]));textItem->setFont(QFont(font().family(), 10)); // Set font size as needed//textItem->setPen(QPen(Qt::blue)); // Set text colortextItem->setPen(Qt::NoPen); // Set no pen (remove border)textItem->setBrush(QBrush(Qt::transparent)); // Set transparent brush to hide the background}// 整个折线图标题QCPTextElement* PlotTitle = new QCPTextElement(customPlot, "折线图统计分析-近5天对比");//PlotTitle->setPositionAlignment(Qt::AlignCenter);PlotTitle->setFont(QFont("宋体", 12, QFont::Bold)); // 设置标题的字体customPlot->plotLayout()->insertRow(0); // 在图表布局中插入一行customPlot->plotLayout()->addElement(0, 0, PlotTitle); // 将标题添加到插入的行// 设置Y轴范围为0-2000customPlot->yAxis->setRange(0, 2000);customPlot->yAxis->ticker()->setTickCount(10);customPlot->setGeometry(QRect(10,20,700,600));// 自动调整坐标轴范围以适应数据,并重新绘制图表//customPlot->rescaleAxes();customPlot->replot();QVBoxLayout* pVBoxLayout = new QVBoxLayout(this);pVBoxLayout->addWidget(customPlot);}

这篇关于QCustomPlot-绘制X轴为日期的折线图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/827585

相关文章

【WebGPU Unleashed】1.1 绘制三角形

一部2024新的WebGPU教程,作者Shi Yan。内容很好,翻译过来与大家共享,内容上会有改动,加上自己的理解。更多精彩内容尽在 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信号:digital_twin123 在 3D 渲染领域,三角形是最基本的绘制元素。在这里,我们将学习如何绘制单个三角形。接下来我们将制作一个简单的着色器来定义三角形内的像素

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

linux下修改系统日期与时间

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

YOLOv8/v10+DeepSORT多目标车辆跟踪(车辆检测/跟踪/车辆计数/测速/禁停区域/绘制进出线/绘制禁停区域/车道车辆统计)

01:YOLOv8 + DeepSort 车辆跟踪 该项目利用YOLOv8作为目标检测模型,DeepSort用于多目标跟踪。YOLOv8负责从视频帧中检测出车辆的位置,而DeepSort则负责关联这些检测结果,从而实现车辆的持续跟踪。这种组合使得系统能够在视频流中准确地识别并跟随特定车辆。 02:YOLOv8 + DeepSort 车辆跟踪 + 任意绘制进出线 在此基础上增加了用户

时间日期与时间戳转换(Linux C)

本文主要学习三个知识点,第一是UTC时间、GMT时间的概念;第二是在Unix环境下UTC时间与时间戳的转换;第三是在C语言中如何修改时区。 本文参考了《UNP》以及 http://blog.csdn.net/foxir/article/details/43916601 http://blog.csdn.net/ljafl9988/article/details/16847935 一、

Java中计算两个日期间隔多少天

String dbtime1 = "2017-02-23";  //第二个日期 String dbtime2 = "2017-02-22";  //第一个日期 //算两个日期间隔多少天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = format.parse(dbtime1); Date dat

实现日期往前或往后或跳转到指定月份或天数

//月份跳转 //初始日期 String yearMonth = "201702"; String yearMonthStr = ""; //往前(负数)或往后(正数) int add = -2; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date source = sdf.parse(yearMonth); Cal

HighCharts 折线图的实现

Java和HighCharts结合,实现折线图; 代码下载:Java和Highcharts实现折线图