Qt 读取csv文件并且绘制折线图


 1 void MainWindow::readcsvfile() //读取csv
 2 {  3  QFile csvFile("C:/Users/Administrator/Desktop/Demo/0702.CSV");  4  QStringList csvList;  5  csvList.clear();  6  if (csvFile.open(QIODevice::ReadWrite)) //对csv文件进行读写操作
 7  {  8   QTextStream stream(&csvFile);  9   while (!stream.atEnd()) 10  { 11  csvList.push_back(stream.readLine()); //保存到List当中
12  } 13  csvFile.close(); 14  } 15  else
16  { 17      QMessageBox::about(NULL, "csv文件", "未打开该文件!"); 18  } 19    int i = 0; 20    Q_FOREACH(QString str, csvList)   //遍历List
21  { 22    i = i + 1; 23    QStringList valsplit = str.split(","); //分隔字符串
24    if(i > 2) 25  { 26     //得到深度、声速、温度
27     QString depth = valsplit[0]; 28     QString sonicvelocity = valsplit[1]; 29     QString temperature = valsplit[2]; 30     double depthvalue = depth.toDouble(); 31     double sonicvalue = sonicvelocity.toDouble(); 32     double tempvalue = temperature.toDouble(); 33     //Q//MessageBox::warning(NULL, "dd", QString::number(tempvalue));
34  QPointF point; 35  point.setX(depthvalue); 36  point.setY(sonicvalue); 37  QPointF point2; 38  point2.setX(depthvalue); 39     point2.setY(tempvalue + 1510); 40  vectors.append(point); 41  vector2.append(point2); 42  } 43  } 44 } 45  
46 void MainWindow::lineChart() //绘制图
47 { 48     //设置X,Y标题
49     ui->qwtPlot->setAxisTitle(QwtPlot::xBottom, QString::fromLocal8Bit("深度(m)")); 50     ui->qwtPlot->setAxisTitle(QwtPlot::yLeft, QString::fromLocal8Bit("声速(m/s)")); 51     ui->qwtPlot->setAxisTitle(QwtPlot::yRight, QString::fromLocal8Bit("温度(°C)")); 52     ui->qwtPlot->enableAxis(QwtPlot::yRight,true); 53     ui->qwtPlot->setAxisScale(QwtPlot::yLeft,1538,1540,0.2); 54     ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,30,2); 55     ui->qwtPlot->setAxisScale(QwtPlot::yRight,28,30,0.2); 56  
57     //ui->qwtPlot->set 58     //构造曲线数据
59      QwtPointSeriesData* series = new QwtPointSeriesData(vectors); 60      //设置网格
61      QwtPlotGrid* grid = new QwtPlotGrid(); 62      grid->setPen(QColor(222, 222, 222), 1); 63      grid->attach(ui->qwtPlot); 64      //create plot item
65      QwtPlotCurve* curve1 = new QwtPlotCurve(QString::fromLocal8Bit("声速")); 66      //设置数据
67      curve1->setData(series); 68      //设置画笔颜色==就是图像颜色
69      curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine); 70      //使曲线更光滑
71      curve1->setCurveAttribute(QwtPlotCurve::Fitted, true); 72      //把曲线附加到qwtPlot上
73      curve1->attach(ui->qwtPlot); 74      //添加温度-深度曲线 75      //构造曲线数据
76      QwtPointSeriesData* series2 = new QwtPointSeriesData(vector2); 77      //create plot item
78      QwtPlotCurve* curve2 = new QwtPlotCurve(QString::fromLocal8Bit("温度")); 79      //设置数据
80      curve2->setData(series2); 81      //设置画笔颜色=图像颜色
82      curve2->setPen(QColor(127, 222, 335), 2, Qt::SolidLine); 83      //使曲线更光滑
84      curve2->setCurveAttribute(QwtPlotCurve::Fitted, true); 85      //把曲线附加到qwtPlot上
86      curve2->attach(ui->qwtPlot); 87  
88      //设置图例
89      QwtLegend *legend = new QwtLegend; 90      legend->setDefaultItemMode(QwtLegendData::ReadOnly); 91      ui->qwtPlot->insertLegend(legend,QwtPlot::BottomLegend);//插入图例
92      ui->qwtPlot->replot(); 93      ui->qwtPlot->show(); 94 }

需要第三方库QWT

运行结果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM