坐标图

#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

 

 

原文链接: https://www.cnblogs.com/ncepubye/p/17012752.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    坐标图

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/396409

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月6日 上午11:18
下一篇 2023年4月6日 上午11:18

相关推荐