#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_chart(new QChart),
TempTime(new QTimer),
isStopping(false),
currentChangel(0)
{
ui->setupUi(this);
TempInferfaceInit();
TempTime->setInterval(50);
TempTime->start();
tip = 0;
connect(TempTime, SIGNAL(timeout()), this, SLOT(updateData()));
connect(ui->Btn_Stop, SIGNAL(clicked(bool)), this, SLOT(Btn_Stop()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
if (event->delta() > 0) {
m_chart->zoom(1.1);
} else {
m_chart->zoom(10.0/11);
}
isZooninOrOut = true;
QWidget::wheelEvent(event);
}
void MainWindow::TempInferfaceInit()
{
qint8 i;
// m_chart->createDefaultAxes();
axisX = new QValueAxis;
axisY = new QValueAxis;
axisY->setRange(0, 120);
axisX->setRange(0, 100);
axisY->setTickCount(12 + 1);
axisX->setTickCount(10 + 1);
axisY->setLabelFormat("%.2f");
axisX->setLabelFormat("%d");
axisX->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
axisY->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
// axisX->setTitleText("Time/sec");
axisY->setTitleText("Temp/°C");
axisX->setGridLineVisible(false);
axisY->setGridLineVisible(false);
for(i = 0;i < 8;i++)
{
SeriesInit(i);
}
m_chart->legend()->hide();
m_chart->setTitle("Temp demo");
chartView = new ChartView(m_chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->verticalLayout->addWidget(chartView);
}
void MainWindow::SeriesInit(qint8 ch)
{
m_series[ch] = new QLineSeries;
m_chart->addSeries(m_series[ch]);
switch(ch)
{
case 0:m_series[ch]->setPen(QPen(Qt::black,2,Qt::SolidLine));break;
case 1:m_series[ch]->setPen(QPen(Qt::red,2,Qt::SolidLine));break;
case 2:m_series[ch]->setPen(QPen(Qt::green,2,Qt::SolidLine));break;
case 3:m_series[ch]->setPen(QPen(Qt::blue,2,Qt::SolidLine));break;
case 4:m_series[ch]->setPen(QPen(Qt::cyan,2,Qt::SolidLine));break;
case 5:m_series[ch]->setPen(QPen(Qt::magenta,2,Qt::SolidLine));break;
case 6:m_series[ch]->setPen(QPen(Qt::yellow,2,Qt::SolidLine));break;
case 7:m_series[ch]->setPen(QPen(Qt::gray,2,Qt::SolidLine));break;
}
m_chart->setAxisX(axisX,m_series[ch]);
m_chart->setAxisY(axisY,m_series[ch]);
connect(m_series[ch], SIGNAL(hovered(QPointF, bool)), this, SLOT(showPointData(QPointF,bool)));
}
void MainWindow::updateData()
{
int i,j;
QVector<QPointF> oldData[8];
QVector<QPointF> data[8];
qint64 size[8];
if(isVisible())
{
if(currentChangel == 8)
{
for(j = 0;j < 8;j++)
{
oldData[j] = m_series[j]->pointsVector();
if (oldData[j].size() < 100)
{
data[j] = oldData[j];
}
else
{
/* 添加之前老的数据到新的vector中,不复制最前的数据,即每次替换前面的数据
* 由于这里每次只添加1个数据,所以为1,使用时根据实际情况修改
*/
for (i = 1; i < oldData[j].size(); ++i) {
data[j].append(QPointF(i - 1, oldData[j].at(i).y()));
}
}
size[j] = data[j].size();
}
/* 这里表示插入新的数据,因为每次只插入1个,这里为i < 1,
* 但为了后面方便插入多个数据,先这样写
*/
for(i = 0; i < 1; ++i){
for(j = 0;j < 8;j++)
{
data[j].append(QPointF(i + size[j], GetTempData(j) ));
}
}
for(j = 0;j < 8;j++)
{
m_series[j]->replace(data[j]);
}
}
else
{
lastChangel =currentChangel;
oldData[currentChangel] = m_series[currentChangel]->pointsVector();
if (oldData[currentChangel].size() < 100)
{
data[currentChangel] = oldData[currentChangel];
}
else
{
for (i = 1; i < oldData[currentChangel].size(); ++i) {
data[currentChangel].append(QPointF(i - 1, oldData[currentChangel].at(i).y()));
}
}
size[currentChangel] = data[currentChangel].size();
for(i = 0; i < 1; ++i)
{
data[currentChangel].append(QPointF(i + size[currentChangel], GetTempData(currentChangel) ));
}
m_series[currentChangel]->replace(data[currentChangel]);
}
}
}
void MainWindow::Btn_Stop()
{
if (QObject::sender() == ui->Btn_Stop) {
if (!isStopping) {
TempTime->stop();
ui->Btn_Stop->setText("继续");
} else {
TempTime->start();
ui->Btn_Stop->setText("暂停");
}
isStopping = !isStopping;
}
}
void MainWindow::showPointData(QPointF position, bool isHovering)
{
if (tip == 0)
tip = new Callout(m_chart);
if (isHovering) {
if(currentChangel != 8)
{
tip->setText(QString("ch: %1 \nY: %2 ").arg(currentChangel+1).arg(QString::number(position.y(),'f',2)));
}
else
{
tip->setText(QString("Temp: %1 ").arg(QString::number(position.y(),'f',2)));
}
tip->setAnchor(position);
tip->setZValue(11);
tip->updateGeometry();
tip->show();
} else {
tip->hide();
}
}
float MainWindow::GetTempData(qint8 ch)
{
ch++;
switch(ch)
{
case 1:return 10;break;
case 2:return 20;break;
case 3:return 30;break;
case 4:return 40;break;
case 5:return 50;break;
case 6:return 60;break;
case 7:return 70;break;
case 8:return 80;break;
}
return qrand() % 100;
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
currentChangel = index;
if(currentChangel != lastChangel)
{
TempTime->stop();
if(lastChangel == 8)
{
for(qint8 i = 0;i < 8;i++)
{
m_chart->removeSeries(m_series[i]);
SeriesInit(i);
}
}
else
{
m_chart->removeSeries(m_series[lastChangel]);
SeriesInit(lastChangel);
}
m_chart->legend()->update();
lastChangel = currentChangel;
TempTime->start();
}
else
{
qDebug() << "I can`t believe";
}
}