一、建立QT GUI應用,基類選擇"QMainWindows",在.pro中添加:app
QT += charts
在源文件中添加:函數
#include <QtCharts>
二、在構造函數中添加:this
QChartView *chartView = new QChartView(this);
QChartView *chartView = new QChartView(this); QChart *chart = new QChart(); chart->setTitle("簡單函數曲線"); //圖標的名字 chartView->setChart(chart); setCentralWidget(chartView);
QChartView *chartView = new QChartView(this); QChart *chart = new QChart(); chart->setTitle("簡單函數曲線"); //圖標的名字 chartView->setChart(chart); setCentralWidget(chartView); //建立曲線序列 QLineSeries *series0 = new QLineSeries(); QLineSeries *series1 = new QLineSeries(); series0->setName("sin曲線"); //自動添加折線名字 series1->setName("cos曲線"); chart->addSeries(series0); chart->addSeries(series1);
QChartView *chartView = new QChartView(this); QChart *chart = new QChart(); chart->setTitle("簡單函數曲線"); //圖標的名字 chartView->setChart(chart); setCentralWidget(chartView); //建立曲線序列 QLineSeries *series0 = new QLineSeries(); QLineSeries *series1 = new QLineSeries(); series0->setName("sin曲線"); //自動添加折線名字 series1->setName("cos曲線"); chart->addSeries(series0); chart->addSeries(series1); //序列添加數值 qreal t = 0, y1, y2, intv = 0.1; int cnt = 200; for(int i = 0; i < cnt; ++i){ y1 = qSin(t); series0->append(t, y1); y2=qSin(t+20); series1->append(t,y2); t+=intv; //每次角度增長 } //建立座標 QValueAxis *axisX = new QValueAxis; axisX->setRange(0, 10);//設置座標軸範圍 axisX->setTitleText("time(secs)");//標題 axisX->setLabelFormat("%.1f"); //標籤格式:每一個單位保留幾位小數 axisX->setTickCount(20); //主分隔個數:0到10分紅20個單位 axisX->setMinorTickCount(4); //每一個單位之間繪製了多少虛網線 // axisX->setGridLineVisible(false); QValueAxis *axisY = new QValueAxis; //Y 軸 axisY->setRange(-2, 2); axisY->setTitleText("value"); axisY->setLabelFormat("%.2f"); //標籤格式 axisY->setTickCount(10); axisY->setMinorTickCount(4); // axisX->setGridLineVisible(false); //爲序列設置座標軸 chart->setAxisX(axisX, series0); chart->setAxisY(axisY, series0); chart->setAxisX(axisX, series1); chart->setAxisY(axisY, series1);