添加模块
添加头文件
需要在窗体上放置 QChartView控件,工具箱没找到,需要在别的项目复制过来一个这个控件。
代码
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // QChartView *chartView=new QChartView(this); //创建 ChartView QChart *chart = new QChart(); //创建 Chart chart->setTitle("温湿度数据曲线"); // chartView->setChart(chart); //Chart添加到ChartView // this->setCentralWidget(chartView); ui->chartViewBar->setChart(chart); //创建曲线序列 QLineSeries *series0 = new QLineSeries(); QLineSeries *series1 = new QLineSeries(); series0->setName("温度曲线"); series1->setName("湿度曲线"); chart->addSeries(series0); //序列添加到图表 chart->addSeries(series1); // QPen pen; // pen.setStyle(Qt::DotLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine // pen.setWidth(2); // pen.setColor(Qt::red); // series0->setPen(pen); // pen.setStyle(Qt::SolidLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine // pen.setColor(Qt::blue); // series1->setPen(pen); //序列添加数值 // qreal t=0,y1,y2,intv=0.1; // int cnt=100; // for(int i=0;i<cnt;i++) // { // y1=qSin(t);//+qrand(); // series0->append(t,y1); // y2=qSin(t+20); // series1->append(t,y2); // t+=intv; // } series0->append(2.5,1); series0->append(5,1.3); series1->append(2,1.2); series1->append(5,1.5); // chart->createDefaultAxes(); // chart->axisX()->setTitleText("time(secs)"); // chart->axisY()->setTitleText("value"); //创建坐标轴 QValueAxis *axisX = new QValueAxis; //X 轴 axisX->setRange(0, 24); //设置坐标轴范围 // axisX->setLabelFormat("%.1f"); //标签格式 // axisX->setTickCount(11); //主分隔个数 // axisX->setMinorTickCount(4); axisX->setTitleText("time(secs)"); //标题 // axisX->setGridLineVisible(false); QValueAxis *axisY = new QValueAxis; //Y 轴 axisY->setRange(0, 60); axisY->setTitleText("value"); // axisY->setTickCount(5); // axisY->setMinorTickCount(4); // axisY->setLabelFormat("%.2f"); //标签格式 // axisY->setGridLineVisible(false); chart->setAxisX(axisX, series0); //为序列设置坐标轴 chart->setAxisY(axisY, series0); // chart->setAxisX(axisX, series1); //为序列设置坐标轴 chart->setAxisY(axisY, series1); // }
标签:Qt,axisX,axisY,chart,图表,pen,LINUX,series1 From: https://www.cnblogs.com/yzdz2/p/17067815.html