座標圖

#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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章