坐标图
#include "plotdlg.h" #include "ui_plotdlg.h" #include <QColor> #include <QPainter> #include <QPen> plotDlg::plotDlg(QList<QPointF> &data, QWidget *parent) : QDialog(parent), data(data) { ui = new Ui::plotDlg(); ui->setupUi(this); // 初始化image this->setFixedWidth(700); this->setFixedHeight(400); image = QImage(700, 400, QImage::Format_RGB32); QColor background(255, 255, 255); image.fill(background); } plotDlg::~plotDlg() { delete ui; } void plotDlg::drawLineChart() { QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing, true); // 抗锯齿 // 绘制坐标轴 float coord_x = 50; float coord_y = 350; painter.drawLine(QPointF(coord_x, coord_y), QPointF(50, 50)); painter.drawLine(QPointF(coord_x, coord_y), QPointF(650, 350)); float x_max = data.at(0).x(); float y_max = data.at(0).y(); foreach(QPointF point, data) { if (point.y() > y_max) { y_max = point.y(); } if (point.x() >x_max) { x_max = point.x(); } } int width = 600; int height = 300; int scale_cnt = 10; float x_step = (float)width / scale_cnt; float y_step = (float)height / scale_cnt; // x轴 float start_point_x = coord_x; float start_point_y = coord_y; float end_point_x = coord_x; float end_point_y = coord_y + 5; float x_val = 0; float y_val = 0; float x_val_step = (x_max - 0) / scale_cnt; float y_val_step = (y_max - 0) / scale_cnt; for (int cnt = 0; cnt < scale_cnt; cnt++) { start_point_x += x_step; end_point_x += x_step; painter.drawLine(QPointF(start_point_x, start_point_y), QPointF(end_point_x, end_point_y)); x_val += x_val_step; QFont font = painter.font(); font.setPixelSize(14); painter.setFont(font); painter.drawText(QPointF(start_point_x - 7, start_point_y + 18), QString::number((int)x_val)); } // y轴 start_point_x = coord_x; start_point_y = coord_y; end_point_x = coord_x+5; end_point_y = coord_y; for (int cnt = 0; cnt < scale_cnt; cnt++) { start_point_y -= y_step; end_point_y -= y_step; painter.drawLine(QPointF(start_point_x, start_point_y), QPointF(end_point_x, end_point_y)); y_val += y_val_step; QFont font = painter.font(); font.setPixelSize(14); painter.setFont(font); painter.drawText(QPointF(start_point_x - 18, start_point_y+5), QString::number((int)y_val)); } // 绘制数据点,此时需要做坐标转换 foreach (QPointF point, data) { float x_pos = 50 + width / x_max * point.x(); float y_pos = 350 - height / y_max * point.y(); QPen pen; pen.setColor(Qt::red); pen.setWidth(6); painter.setPen(pen); painter.drawPoint(QPointF(x_pos, y_pos)); } for (int index = 0; index < data.size() - 1; index++) { QPen pen; pen.setColor(Qt::black); pen.setWidth(5); painter.setPen(pen); float point1_x = 50 + width / x_max * data.at(index).x(); float point1_y = 350 - height / y_max * data.at(index).y(); float point2_x = 50 + width / x_max * data.at(index+1).x(); float point2_y = 350 - height / y_max * data.at(index+1).y(); painter.drawLine(QPointF(point1_x, point1_y), QPointF(point2_x, point2_y)); } } void plotDlg::paintEvent(QPaintEvent *event) { // 绘制折线图 QPainter painter(this); painter.drawImage(QPoint(0, 0), image); }
#ifndef PLOTDLG_H #define PLOTDLG_H #include <QDialog> #include <QList> #include <QPointF> #include <QImage> namespace Ui { class plotDlg; }; class plotDlg : public QDialog { Q_OBJECT public: plotDlg(QList<QPointF> &data, QWidget *parent = Q_NULLPTR); ~plotDlg(); // 定义绘图函数 void drawLineChart(); private: Ui::plotDlg *ui; QList<QPointF> &data; QImage image; protected: void paintEvent(QPaintEvent *event); // 重载paintEvent }; #endif
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)