qwtplot读取tid数据并绘图

void MainWindow::readTidFile()
{
	//读取tid文件
	QFile f("C:/Users/Administrator/Desktop/Demo/sealevel.tid");
	if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QMessageBox::warning(NULL, "文件打开", "文件打开失败!"); 
	}

	QTextStream txtInput(&f);
	QString lineStr;
	QStringList tidList;
	QString tidsealevel;
	QString timestr;
	QStringList valsplit;
	double sealevelval;   //潮位点数值
	tidList.clear();
	int i = 0;
	while (!txtInput.atEnd())
	{
		i = i + 1;
		lineStr = txtInput.readLine();
		tidList.push_back(lineStr);
	}
	Q_FOREACH(QString str, tidList) //遍历tidList
	{ 
			valsplit = str.split(" "); 
			if (valsplit.size() > 2)
			{
				tidsealevel = valsplit[2];               //得到潮位点
				sealevelval = tidsealevel.toDouble();    //转成double类型
				tidsealevelvals.push_back(sealevelval);  //添加到潮位点集合
				timestr = valsplit[0] + " " + valsplit[1];  //x轴坐标
				timestr.replace("/", "-");
				curtime = QDateTime::fromString(timestr, "yyyy-M-d h:mm:ss");
				datevals.push_back(curtime.toTime_t());    //添加到时间点集合
			}
	}
	f.close();
}

void MainWindow::drawTidChart()
{
	//重新绘制x轴scale
	ui->qwtPlot->setAxisTitle(QwtPlot::xBottom, QString::fromLocal8Bit("时间"));
	ui->qwtPlot->setAxisTitle(QwtPlot::yLeft, QString::fromLocal8Bit("潮位点(m)"));
	QPointF pointdemo;
	for (int i = 0; i < datevals.size(); i++)
	{
		if (i == 0)
		{
			ui->qwtPlot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());
		}
		pointdemo.setX(datevals[i]);
		pointdemo.setY(tidsealevelvals[i]);
		vectorSeaLevel.push_back(pointdemo);
    }
	ui->qwtPlot->setAxisScale(QwtPlot::yLeft, 0, 5, 0.2);
	ui->qwtPlot->setAxisScale(QwtPlot::xBottom, datevals[0], datevals[datevals.size()-1],101800); //最后一个参数是以秒为单位的
	//ui->qwtPlot->setAxisScale(QwtPlot::xBottom, GetTime("2018-5-15 2:00:00"), GetTime("2018-6-16 0:00:00"), 204400);
	QwtPointSeriesData* series = new QwtPointSeriesData(vectorSeaLevel);
	//设置网格
	QwtPlotGrid* grid = new QwtPlotGrid();
	grid->setPen(QColor(222, 222, 222), 1);
	grid->attach(ui->qwtPlot);
	//create plot item
	QwtPlotCurve* curve1 = new QwtPlotCurve(QString::fromLocal8Bit("tid"));
	//设置数据
	curve1->setData(series);
	//设置画笔颜色==就是图像颜色
	curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);
	//使曲线更光滑
	curve1->setCurveAttribute(QwtPlotCurve::Fitted, true);
	//把曲线附加到qwtPlot上
	curve1->attach(ui->qwtPlot);

	//TimeScakeDrawR.label("dd");
	ui->qwtPlot->replot();
	ui->qwtPlot->show();
}

 演示结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Music 爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值