qwt绘图Demo示例

#include "readsonicpoint.h"
#include "marinebase.h"
#include "QMessageBox"
#include "qfiledialog.h"

ReadSonicPoint::ReadSonicPoint(QWidget *parent, QString filepath)
	: QDialog(parent)
{
	ui.setupUi(this);
	QList csv_data_list;
	QVector point_list;
	csv_data_list = readSonicPointFile(filepath);
	point_list = readStringListData(csv_data_list, 0);
	setEnableAllAxis(true, true, true, false);
	setQwtPlotTitle("速度(m/s)","深度(m)","温度(℃)");
	//setXScale();
	setInputData(point_list, "curve 1");
}

void ReadSonicPoint::openFilePath()
{
	QString filepath = QFileDialog::getOpenFileName(NULL, "请选择数据文件", "", "CSV(*.CSV)");
	if (filepath.isEmpty())
	{
		QMessageBox::warning(NULL, "提示", "未选择数据文件", "确定");
		return;
	}

}

QVector ReadSonicPoint::readStringListData(QList sonic_data_list, int flag)
{
	QVector sonic_point_list;
	QString sonic_data;
	QList sonicpointdata;
	//flag = 0:声速/深度  flag = 1:温度/深度
	for (int i = 0; i < sonic_data_list.size(); i++)
	{
		sonic_data = sonic_data_list[i];
		sonicpointdata = sonic_data.split(",");
		if (flag == 0)
		{
			QPointF point;
			point.setX(sonicpointdata[0].toDouble());
			point.setY(sonicpointdata[1].toDouble());
			sonic_point_list.append(point);
		}
		else if (flag == 1)
		{
			QPointF point;
			point.setX(sonic_data_list[0].toDouble());
			point.setY(sonic_data_list[1].toDouble());
			sonic_point_list.append(point);
		}
	}
	return sonic_point_list;
}

QList ReadSonicPoint::readSonicPointFile(QString csv_path)
{
	MSSetProperty(QString::fromLocal8Bit("TXTDSC"), csv_path); //设置属性字段
	QFile csvFile(csv_path);
	QList sonic_data_list;
	QString sonic_data;
	if (csvFile.open(QIODevice::ReadWrite))
	{
		QTextStream stream(&csvFile); 
		int i = 0;
		while (!stream.atEnd())
		{
			i = i + 1;
			sonic_data = stream.readLine();
			if (i > 2)
			{
				sonic_data_list.append(sonic_data);
			}
		}
		csvFile.close();
	}
	return sonic_data_list;
}

void ReadSonicPoint::setQwtPlotTitle(QString yLeftName, QString xBottomName, QString yRightName)
{
	ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::yLeft, yLeftName);
	ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::xBottom, xBottomName);
	ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::yRight, yRightName);
}

void ReadSonicPoint::setInputData(QVector vector , QString lineTitle)
{
	QwtPointSeriesData* series = new QwtPointSeriesData(vector);
	QwtPlotCurve* curve1 = new QwtPlotCurve(lineTitle);
	//设置数据
	curve1->setData(series);
	//把曲线附加到qwtPlot上
	curve1->attach(ui.qwtPlot_Sonic);
	curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);//设置画笔
	//添加格网
	QwtPlotGrid* grid = new QwtPlotGrid();
	grid->setPen(QColor(222, 222, 222), 1);
	grid->attach(ui.qwtPlot_Sonic);
	ui.qwtPlot_Sonic->replot();
	ui.qwtPlot_Sonic->show();
}

void ReadSonicPoint::setEnableAllAxis(bool yLeftEnable, bool yRightEnable, bool xBottomEnable, bool xTopEnable)
{
	ui.qwtPlot_Sonic->enableAxis(QwtPlot::yLeft, yLeftEnable);  
	ui.qwtPlot_Sonic->enableAxis(QwtPlot::yRight, yRightEnable);  
	ui.qwtPlot_Sonic->enableAxis(QwtPlot::xBottom, xBottomEnable); 
	ui.qwtPlot_Sonic->enableAxis(QwtPlot::xTop, xTopEnable);  
}

void ReadSonicPoint::setXScale()
{
	ui.qwtPlot_Sonic->setAxisScale(QwtPlot::xBottom,1,30);
	ui.qwtPlot_Sonic->setAxisScale(QwtPlot::yLeft, 1530,1550);
}

ReadSonicPoint::~ReadSonicPoint()
{
}

 

你可能感兴趣的:(C++,QTDevelop)