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();
}
演示结果: