#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
const quint32 c_MaxSize=1000;//数据个数
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
,m_splineSerise1(NULL)
,m_splineSerise2(NULL)
{
ui->setupUi(this);
m_pChartView=new CChartView(ui->widget);
QGridLayout* gridLayout=new QGridLayout(ui->widget);
gridLayout->addWidget(m_pChartView,0,0);
ui->widget->setLayout(gridLayout);
//setWindowFlags(Qt::FramelessWindowHint);
//构建各个系列的画笔
m_penSeries1 =QPen(Qt::green,2.f);
m_penSeries2 =QPen(Qt::cyan,2.f);
//构建曲线系列
m_splineSerise1=new QSplineSeries(this);
m_splineSerise2=new QSplineSeries(this);
//为折线添加数据,曲线一
qint32 i=0;
qreal x=0.f;
for (i=0;i<c_MaxSize;i++)
{
x=i*1.f/c_MaxSize;
m_splineSerise1->append(i,getData_1(x));
}
//为折线添加数据,曲线二
for ( i=0;i<c_MaxSize;i++)
{
x=i*1.f/c_MaxSize;
m_splineSerise2->append(i,getData_2(x));
}
//构建图标对象
m_chart=new QChart();
//注意:先添加到图表再创建坐标轴,否则无效
//构建坐标轴
m_axisX = new QValueAxis();
m_axisX->setRange(0,c_MaxSize);
m_axisX->setTitleText(QString::fromLocal8Bit("Time"));//设置标题
m_axisX->setLabelFormat("%g");//设置格式
m_axisX->setTickCount(5);//设置刻度数
m_axisY= new QValueAxis();
m_axisY->setRange(-10,10);
m_axisY->setTitleText(QString::fromLocal8Bit("T"));
//将坐标轴绑定
m_chart->setAxisX(m_axisX,m_splineSerise1);
m_chart->setAxisY(m_axisY,m_splineSerise1);
m_chart->setAxisX(m_axisX,m_splineSerise2);
m_chart->setAxisY(m_axisY,m_splineSerise2);
// 隐藏图例
m_chart->legend()->hide();
// 设置图表的主题
m_chart->setTheme(QtCharts::QChart::ChartThemeBlueCerulean);
// 为图表添加标题
m_chart->setTitle(QString::fromLocal8Bit("Polyline Chart"));
// 将图表绑定到视图
m_pChartView->setChart(m_chart);
/* 感兴趣的读者,可以封掉下面3行代码进行对比 */
m_chart->layout()->setContentsMargins(0, 0, 0, 0);//设置外边界全部为0
m_chart->setMargins(QMargins(0, 0, 0, 0));//设置内边界全部为0
m_chart->setBackgroundRoundness(0);//设置背景区域无圆角
// 设置反走样
m_pChartView->setRenderHint(QPainter::Antialiasing, true);
m_pChartView->setBackgroundBrush(Qt::yellow);
// 设置样式
changeStyle();
//1.将折线系列添加到图表
m_chart->addSeries(m_splineSerise1);
m_chart->addSeries(m_splineSerise2);
// 绑定鼠标悬浮信号
connect(m_splineSerise1, &QSplineSeries::hovered, this, &Dialog::slot_serieshovered1);
connect(m_splineSerise2, &QSplineSeries::hovered, this, &Dialog::slot_serieshovered2);
connect(m_splineSerise1, &QSplineSeries::hovered, m_pChartView, &CChartView::slot_showTip);
connect(m_splineSerise2, &QSplineSeries::hovered, m_pChartView, &CChartView::slot_showTip);
connect(m_splineSerise1, &QSplineSeries::clicked, m_pChartView, &CChartView::slot_keepTip);
connect(m_splineSerise2, &QSplineSeries::clicked, m_pChartView, &CChartView::slot_keepTip);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::slot_serieshovered1(const QPointF &point, bool state)
{
QPen penHighlight(Qt::white,5.f);
if(state)//悬浮进入
{
m_splineSerise1->setPen(penHighlight);
}
else
{
m_splineSerise1->setPen(m_penSeries1);
}
}
void Dialog::slot_serieshovered2(const QPointF &point, bool state)
{
QPen penHighlight(Qt::white,5.f);
if(state)//悬浮进入
{
m_splineSerise2->setPen(penHighlight);
}
else
{
m_splineSerise2->setPen(m_penSeries2);
}
}
qreal Dialog::getData_1(qreal x)
{
return qSin(x*2*M_PI)*7;//正弦
}
qreal Dialog::getData_2(qreal x)
{
return qCos(x*2*M_PI)*7;//余弦
}
void Dialog::changeStyle()
{
/**
* 修改窗体
*
*/
//根据图表的主题设置调色板
setDialogPalette();
/**
* 修改图表
*
*/
m_chart->setBackgroundVisible(true);
//m_chart->setBackgroundBrush(Qt::transparent);//设置为透明
m_chart->setBackgroundBrush(Qt::lightGray);
QPen penBackground;
penBackground.setStyle(Qt::DotLine);
penBackground.setColor(Qt::green);
m_chart->setBackgroundPen(penBackground);
/**
* 修改绘图区
*
*/
m_chart->setPlotAreaBackgroundVisible(true);
m_chart->setPlotAreaBackgroundBrush(Qt::gray);
/**
* 修改标题
*
*/
QFont fontTitle;
fontTitle.setFamily(QString::fromLocal8Bit("华文琥珀"));
fontTitle.setPointSizeF(20.f);
m_chart->setTitleFont(fontTitle);
//设置字色
m_chart->setTitleBrush(Qt::black);
/**
* 修改刻度
*
*/
//设置刻度
QFont fontAxis;
fontAxis.setFamily(QString::fromLocal8Bit("微软雅黑"));
fontAxis.setPointSizeF(12.f);
m_axisX->setTitleFont(fontAxis);
m_axisY->setTitleFont(fontAxis);
//设置字色
m_axisX->setTitleBrush(Qt::darkMagenta);
m_axisY->setTitleBrush(Qt::darkMagenta);
//设否显示刻度线
m_axisX->setGridLineVisible(true);
m_axisY->setGridLineVisible(true);
//设置字体坐标轴
QFont fontLabel;
fontLabel.setFamily(QStringLiteral("微软雅黑"));
fontLabel.setPixelSize(12);
m_axisX->setLabelsFont(fontLabel);
m_axisY->setLabelsFont(fontLabel);
/**
* 修改图例
*
*/
//对齐方式
m_chart->legend()->setAlignment(Qt::AlignLeft);
/**
* 系列
*
*/
m_splineSerise1->setPen(m_penSeries1);
QPen pn2(Qt::cyan,2.f);
m_splineSerise2->setPen(m_penSeries2);
/**
* 开启动画
*
*/
QChart::AnimationOptions aniOptions=QChart::AllAnimations;
//m_chart->setAnimationOptions(aniOptions);
}
void Dialog::setDialogPalette()
{
QChart::ChartTheme theme=QChart::ChartThemeBlueIcy;
m_chart->setTheme(theme);
//根据选定的主题确定Dialog的调色板
QPalette pal=window()->palette();
switch (theme)
{
case QtCharts::QChart::ChartThemeLight:
pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
pal.setColor(QPalette::WindowText, QRgb(0x404040));
break;
case QtCharts::QChart::ChartThemeBlueCerulean:
pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
pal.setColor(QPalette::WindowText, QRgb(0x404040));
break;
case QtCharts::QChart::ChartThemeDark:
pal.setColor(QPalette::Window, QRgb(0x121218));
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
break;
case QtCharts::QChart::ChartThemeBrownSand:
pal.setColor(QPalette::Window, QRgb(0x9e8965));
pal.setColor(QPalette::WindowText, QRgb(0x404044));
break;
case QtCharts::QChart::ChartThemeBlueNcs:
pal.setColor(QPalette::Window, QRgb(0x18bba));
pal.setColor(QPalette::WindowText, QRgb(0x404044));
break;
case QtCharts::QChart::ChartThemeHighContrast:
pal.setColor(QPalette::Window, QRgb(0xffab03));
pal.setColor(QPalette::WindowText, QRgb(0x181818));
break;
case QtCharts::QChart::ChartThemeBlueIcy:
pal.setColor(QPalette::Window, QRgb(0xcee7f0));
pal.setColor(QPalette::WindowText, QRgb(0x404044));
break;
case QtCharts::QChart::ChartThemeQt:
default:
pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
pal.setColor(QPalette::WindowText, QRgb(0x404044));
没有合适的资源?快使用搜索试试~ 我知道了~
7、QtCharts 获取数值曲线内容
共38个文件
obj:12个
cpp:8个
h:6个
0 下载量 179 浏览量
2023-11-07
09:19:01
上传
评论
收藏 3.42MB ZIP 举报
温馨提示
7、QtCharts 获取数值曲线内容
资源推荐
资源详情
资源评论
收起资源包目录
QChartView.zip (38个子文件)
dialog.ui 623B
dialog.cpp 8KB
Makefile 30KB
ctip.h 2KB
ctip.cpp 5KB
main.cpp 175B
debug
moc_dialog.cpp 4KB
dialog.obj 401KB
moc_dialog.obj 278KB
cchartview.obj 338KB
QChartView.vc.pdb 4.86MB
ctip.obj 304KB
QChartView.pdb 4.95MB
moc_cchartview.obj 278KB
main.obj 273KB
moc_predefs.h 260B
QChartView.ilk 1.84MB
moc_cchartview.cpp 4KB
QChartView.exe 171KB
release
moc_dialog.cpp 4KB
dialog.obj 66KB
moc_dialog.obj 4KB
cchartview.obj 28KB
ctip.obj 19KB
moc_cchartview.obj 4KB
main.obj 2KB
moc_predefs.h 243B
moc_cchartview.cpp 4KB
QChartView.exe 57KB
ui_dialog.h 1KB
Makefile.Release 222KB
cchartview.cpp 2KB
QChartView.pro 1KB
Makefile.Debug 222KB
.qmake.stash 1KB
cchartview.h 1KB
QChartView.pro.user 22KB
dialog.h 2KB
共 38 条
- 1
资源评论
吃个糖糖
- 粉丝: 410
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功