座標図

3828 ワード

#include "plotdlg.h"
#include "ui_plotdlg.h"
#include 
#include 
#include 

plotDlg::plotDlg(QList &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 
#include 
#include 
#include 

namespace Ui { class plotDlg; };

class plotDlg : public QDialog
{
	Q_OBJECT

public:
	plotDlg(QList &data, QWidget *parent = Q_NULLPTR);
	~plotDlg();

	//       
	void drawLineChart();

private:
	Ui::plotDlg *ui;
	QList &data;

	QImage image;

protected:
	void paintEvent(QPaintEvent *event);    //   paintEvent
};

#endif