#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qwdialogpen.h"
// 若MSVC 编译版本错误,修改 msvc-version.conf 文件
// 解决MSVC编译时,界面汉字乱码的问题
#if _MSC_VER >= 1600 //MSVC2015>1899, MSVC_VER= 14.0
#pragma execution_character_set("utf-8")
#endif
void MainWindow::createChart()
{//创建图表的各个部件
QChart *chart = new QChart();
chart->setTitle(tr("简单函数曲线"));
// chart->setAcceptHoverEvents(true);
ui->chartView->setChart(chart);
ui->chartView->setRenderHint(QPainter::Antialiasing);
QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
series0->setName("Sin曲线");
series1->setName("Cos曲线");
curSeries=series0; //当前序列
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);//折线序列的线条设置
chart->addSeries(series0);
chart->addSeries(series1);
QValueAxis *axisX = new QValueAxis;
curAxis=axisX; //当前坐标轴
axisX->setRange(0, 10); //设置坐标轴范围
axisX->setLabelFormat("%.1f"); //标签格式
axisX->setTickCount(11); //主分隔个数
axisX->setMinorTickCount(4);
axisX->setTitleText("time(secs)"); //标题
// axisX->setGridLineVisible(false);
QValueAxis *axisY = new QValueAxis;
axisY->setRange(-2, 2);
axisY->setTitleText("value");
axisY->setTickCount(5);
axisY->setLabelFormat("%.2f"); //标签格式
// axisY->setGridLineVisible(false);
axisY->setMinorTickCount(4);
chart->setAxisX(axisX, series0); //添加X坐标轴
chart->setAxisX(axisX, series1); //添加X坐标轴
chart->setAxisY(axisY, series0); //添加Y坐标轴
chart->setAxisY(axisY, series1); //添加Y坐标轴
//另一种实现设置坐标轴的方法
// chart->addAxis(axisX,Qt::AlignBottom); //坐标轴添加到图表,并指定方向
// chart->addAxis(axisY,Qt::AlignLeft);
// series0->attachAxis(axisX); //序列 series0 附加坐标轴
// series0->attachAxis(axisY);
// series1->attachAxis(axisX);//序列 series1 附加坐标轴
// series1->attachAxis(axisY);
foreach (QLegendMarker* marker, chart->legend()->markers()) {
QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
QObject::connect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
}
}
void MainWindow::prepareData()
{//为序列生成数据
QLineSeries *series0=(QLineSeries *)ui->chartView->chart()->series().at(0);
QLineSeries *series1=(QLineSeries *)ui->chartView->chart()->series().at(1);
series0->clear(); //清除数据
series1->clear();
qsrand(QTime::currentTime().second());//随机数初始化
qreal t=0,y1,y2,intv=0.1;
qreal rd;
int cnt=100;
for(int i=0;i<cnt;i++)
{
rd=(qrand() % 10)-5; //随机数,-5~+5
y1=qSin(t)+rd/50;
*series0<<QPointF(t,y1); //序列添加数据点
// series0->append(t,y1); //序列添加数据点
rd=(qrand() % 10)-5; //随机数,-5~+5
y2=qCos(t)+rd/50;
// series1->append(t,y2); //序列添加数据点
*series1<<QPointF(t,y2); //序列添加数据点
t+=intv;
}
}
void MainWindow::updateFromChart()
{ //从图表上获取数据更新界面显示
QChart *aChart;
aChart=ui->chartView->chart(); //获取chart
ui->editTitle->setText(aChart->title()); //图表标题
QMargins mg=aChart->margins(); //边距
ui->spinMarginLeft->setValue(mg.left());
ui->spinMarginRight->setValue(mg.right());
ui->spinMarginTop->setValue(mg.top());
ui->spinMarginBottom->setValue(mg.bottom());
}
//void MainWindow::mouseMoveEvent(QMouseEvent *event)
//{
//// QPointF point;
//// point=ui->chartView->chart()->mapToValue(event->pos());
//// this->labXYValue->setText(QString::asprintf("X=%.2f,Y=%.2f",
//// point.x(), point.y()));
//// QChartView::mouseMoveEvent(event);
//// m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));
//// m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));
// // QGraphicsView::mouseMoveEvent(event);
//}
//bool MainWindow::eventFilter(QObject *watched, QEvent *event)
//{
// if (watched==ui->chartView)
// if (event->type()==QEvent::MouseMove)
// {
// QMouseEvent *msEvent;
// msEvent=(QMouseEvent *)event;
// QPointF point;
// point=ui->chartView->chart()->mapToValue(msEvent->pos());
// this->labXYValue->setText(QString::asprintf("X=%.2f,Y=%.2f",
// point.x(), point.y()));
// return true;
//// QChartView::mouseMoveEvent(event);
// }
// return QMainWindow::eventFilter(watched,event);
//}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// labXYValue = new QLabel("X=, Y= ");
// labXYValue->setMinimumWidth(200);
// ui->statusBar->addWidget(labXYValue);
createChart();//创建图表
prepareData();//生成数据
updateFromChart(); //从图表获得属性值,刷新界面显示
// ui->chartView->chart()->installEventFilter(this);//安装事件过滤器,由主窗口处理事件
// ui->chartView->setMouseTracking(true);
// this->setMouseTracking(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_LegendMarkerClicked()
{
QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
switch (marker->type())
{
case QLegendMarker::LegendMarkerTypeXY:
{
marker->series()->setVisible(!marker->series()->isVisible());
marker->setVisible(true);
qreal alpha = 1.0;
if (!marker->series()->isVisible())
alpha = 0.5;
QColor color;
QBrush brush = marker->labelBrush();
color = brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker->setLabelBrush(brush);
brush = marker->brush();
color = brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker->setBrush(brush);
QPen pen = marker->pen();
color = pen.color();
color.setAlphaF(alpha);
pen.setColor(color);
marker->setPen(pen);
break;
}
default:
break;
}
}
void MainWindow::on_actDraw_triggered()
{ //重新生成数据
prepareData();//重新生成数据
// ui->chartView->chart()->update();//更新绘图区
}
void MainWindow::on_btnSetTitle_clicked()
{ //设置图标标题文字
QString str=ui->editTitle->text();
ui->chartView->chart()->setTitle(str);
}
void MainWindow::on_btnSetTitleFont_clicked()
{ //设置图标标题文字的字体
QFont font=ui->chartView->chart()->titleFont();
bool ok=false;
font=QFontDialog::getFont(&ok,font);
if (ok)
ui->chartView->chart()->setTitleFont(font);
}
void MainWindow::on_btnSetMargin_clicked()
{//设置图标的4个边距
QMargins mgs;
mgs.setLeft(ui->spinMarginLeft->value());
mgs.setRight(ui->spinMarginRight->value());
mgs.setTop(ui->spinMarginTop->value());
mgs.setBottom(ui->spinMarginBottom->va

冷凝女子
- 粉丝: 701
最新资源
- STC89C52RC单片机手册.doc
- lowRISC-硬件开发资源
- 网络安全评估和安全法规.ppt
- 高质量C++编程学习笔记.doc
- 欧司朗普通照明产品网络营销年度方案.pptx
- 某网络系统有限公司商业计划书.docx
- 楼宇自动化论文(1).pdf
- 通信设备公司财务管理手册.doc
- 气象局网络视频监控系统方案.doc
- 2022年MATLAB复习知识点整理版.docx
- 中国网络广告效果营销发展趋势――效果网提供.ppt
- 建立卫生网络体系提升群众医疗保障水平调研思考.pdf
- 网络安全宣传周的活动总结2021年.doc
- 中铁工程项目管理标准化手册检查用表(30个).docx
- 基于AT89C51单片机的16x16LED点阵显示的课程设计.doc
- 中国人民银行招聘笔试计算机习题1.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


