记录一下,以备以后查阅
效果图:
#include "mychart.h"
#include <QLineSeries>
#include <QChart>
#include <QChartView>
#include <QBoxLayout>
#include <QtMath>
QT_CHARTS_USE_NAMESPACE
MyChart::MyChart(QWidget *parent)
{
resize(800,600);
QLineSeries* series = new QLineSeries();//用于绘制二维数据点的折线图
for(int i = 0; i < 97; ++i){
series->append(QPointF(i, 10 * sin(i*M_PI*2/97.00)));
}
QChart *chart = new QChart;//管理和控制图表
chart->addSeries(series);
chart->createDefaultAxes();//创建坐标轴
chart->axisY()->setRange(-10, 10);
chart->axisX()->setRange(0, 96);
chart->axisX()->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
chart->axisY()->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
chart->axisX()->setTitleText("Time/sec");
chart->axisY()->setTitleText("Speed/m");
chart->axisX()->setGridLineVisible(false);
chart->axisY()->setGridLineVisible(false);
/* hide legend */
chart->legend()->hide();
//视图组件(画布),无法单独进行显示,需要依附其他组件进行显示
QChartView *chartView = new QChartView(chart);
QHBoxLayout *mainHorLayout = new QHBoxLayout(this);
mainHorLayout->addWidget(chartView);
}