曲线可以显示数据
This commit is contained in:
+318
-53
@@ -1,65 +1,46 @@
|
||||
#include "Scope.h"
|
||||
#include "ui_Scope.h"
|
||||
|
||||
int Scope::ChannelIndex = 0;
|
||||
|
||||
Scope::Scope(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::Scope)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
timeseries = 0;
|
||||
|
||||
chartView = new QChartView(this);
|
||||
series = new QLineSeries(this);
|
||||
|
||||
chartView->setRenderHint(QPainter::Antialiasing);
|
||||
chartView->chart()->legend()->setAlignment(Qt::AlignRight);
|
||||
chartView->chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
|
||||
chartView->chart()->legend()->setColor(Qt::yellow);
|
||||
chartView = new QChartView(this);
|
||||
chartView->setRenderHint(QPainter::Antialiasing);
|
||||
chartView->chart()->legend()->setAlignment(Qt::AlignRight|Qt::AlignTop);
|
||||
chartView->chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
|
||||
chartView->chart()->legend()->setColor(Qt::yellow);
|
||||
chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean);
|
||||
//chartView->setRubberBand(QChartView::RectangleRubberBand);
|
||||
|
||||
|
||||
series->setName("123");
|
||||
QValueAxis *axisX = new QValueAxis();
|
||||
|
||||
chartView->chart()->createDefaultAxes();
|
||||
|
||||
QPen pen(Qt::yellow);
|
||||
pen.setWidth(2);
|
||||
series->setPen(pen);
|
||||
axisX->setRange(0, 10);
|
||||
axisX->setTickCount(11);
|
||||
axisX->setLabelFormat("%.2f");
|
||||
chartView->chart()->addAxis(axisX, Qt::AlignBottom);
|
||||
|
||||
series->append(0, 0);
|
||||
series->append(1, -4);
|
||||
series->append(2, 5);
|
||||
series->append(3, 6);
|
||||
series->append(4, 2);
|
||||
series->append(5, 9);
|
||||
series->append(6, -0);
|
||||
series->append(7, 4);
|
||||
series->append(8, 4);
|
||||
series->append(9, -5);
|
||||
series->append(10, 6);
|
||||
series->append(11, 2);
|
||||
series->append(12, 9);
|
||||
series->append(13, 0);
|
||||
series->append(14, -4);
|
||||
series->append(16, 4);
|
||||
series->append(18, 5);
|
||||
series->append(19, -6);
|
||||
series->append(20, 2);
|
||||
series->append(21, 9);
|
||||
series->append(22, 0);
|
||||
series->append(23, 4);
|
||||
|
||||
chartView->chart()->addSeries(series);
|
||||
|
||||
QValueAxis *axisX = new QValueAxis(this);
|
||||
axisX->setRange(0, 1);
|
||||
axisX->setTickCount(11);
|
||||
axisX->setLabelFormat("%.2f");
|
||||
chartView->chart()->setAxisX(axisX, series);
|
||||
QValueAxis *axisY = new QValueAxis();
|
||||
axisY->setRange(-100, 100);
|
||||
//axisY->setTickType(QValueAxis::TicksDynamic);
|
||||
axisY->setTickCount(11);
|
||||
axisY->setLabelFormat("%.2f");
|
||||
chartView->chart()->addAxis(axisY, Qt::AlignLeft);
|
||||
|
||||
chartView->setGeometry(ui->frame->geometry());
|
||||
|
||||
QTimer::singleShot(10,this,SLOT(timeout()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
Scope::~Scope()
|
||||
@@ -67,14 +48,40 @@ Scope::~Scope()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
|
||||
void Scope::mouseMoveEvent(QMouseEvent *e)
|
||||
{
|
||||
//移动整幅图
|
||||
qDebug() << "Scope Move" << e;
|
||||
}
|
||||
|
||||
void Scope::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Scope release" << e;
|
||||
|
||||
isPress = false;
|
||||
}
|
||||
|
||||
void Scope::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Scope press" << e;
|
||||
|
||||
isPress = true;
|
||||
|
||||
/*
|
||||
QPoint temp = e->pos();//获取当前坐标的位置
|
||||
|
||||
if(e->buttons() == Qt::RightButton){ // 这里必须使用buttons(),因为鼠标移动过程中会检测所有按下的键,而这个时候button()是无法检测那个按键被按下,所以必须使用buttons()函数,看清楚,是buttons()不是button()
|
||||
QPointF f = chart->mapToValue(temp) - chart->mapToValue(lastPos);
|
||||
axisX->setRange(axisX->min() - f.x(), axisX->max() - f.x());
|
||||
axisY->setRange(axisY->min() - f.y(), axisY->max() - f.y());
|
||||
chart->update();
|
||||
lastPos = temp;
|
||||
update();
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
void Scope::mouseDoubleClickEvent(QMouseEvent *e)
|
||||
@@ -84,39 +91,297 @@ void Scope::mouseDoubleClickEvent(QMouseEvent *e)
|
||||
|
||||
void Scope::wheelEvent(QWheelEvent *e)
|
||||
{
|
||||
//改变是range
|
||||
|
||||
qDebug() << "Scope wheel" << e;
|
||||
chartView->chart()->scroll(e->angleDelta().y(),0);
|
||||
//以鼠标坐标为中心进行缩放
|
||||
|
||||
//改变缩放
|
||||
switch(e->modifiers())
|
||||
{
|
||||
case Qt::ControlModifier:
|
||||
{
|
||||
QList<QAbstractAxis *> axList = chartView->chart()->axes(Qt::Vertical);
|
||||
for(QAbstractAxis *axis:axList)
|
||||
{
|
||||
QValueAxis *a = qobject_cast<QValueAxis *>(axis);
|
||||
if(e->delta() > 0)
|
||||
{
|
||||
a->setRange(a->min()*0.5,a->max()*0.5);
|
||||
}
|
||||
else if(e->delta() < 0)
|
||||
{
|
||||
a->setRange(a->min()*1.5,a->max()*1.5);
|
||||
}
|
||||
}
|
||||
}break;
|
||||
case Qt::ShiftModifier:
|
||||
{
|
||||
QList<QAbstractAxis *> axList = chartView->chart()->axes(Qt::Horizontal);
|
||||
for(QAbstractAxis *axis:axList)
|
||||
{
|
||||
QValueAxis *a = qobject_cast<QValueAxis *>(axis);
|
||||
//还有点问题,负数的时候不行
|
||||
if(e->delta() > 0)//缩小
|
||||
{
|
||||
a->setRange(a->min()*1.5,a->max()*0.5);
|
||||
}
|
||||
else if(e->delta() < 0)//放大
|
||||
{
|
||||
a->setRange(a->min()*0.5,a->max()*1.5);
|
||||
}
|
||||
}
|
||||
}break;
|
||||
default:
|
||||
{
|
||||
QList<QAbstractAxis *> axList = chartView->chart()->axes(Qt::Horizontal);
|
||||
for(QAbstractAxis *axis:axList)
|
||||
{
|
||||
QValueAxis *a = qobject_cast<QValueAxis *>(axis);
|
||||
|
||||
qreal dwidth = chartView->chart()->plotArea().width()/(a->tickCount()*2);
|
||||
|
||||
if(e->delta() > 0)
|
||||
{
|
||||
chartView->chart()->scroll(-dwidth,0);
|
||||
}
|
||||
else if(e->delta() < 0)
|
||||
{
|
||||
chartView->chart()->scroll(dwidth,0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scope::resizeEvent(QResizeEvent *event)
|
||||
void Scope::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
Q_UNUSED(e)
|
||||
chartView->setGeometry(ui->frame->geometry());
|
||||
}
|
||||
|
||||
void Scope::timeout(void)
|
||||
void Scope::timeout(void)//100Hz
|
||||
{
|
||||
timeseries += 0.01;
|
||||
chartUpdate();
|
||||
|
||||
QTimer::singleShot(10,this,SLOT(timeout()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Scope::chartUpdate(void)
|
||||
{
|
||||
//分别读取每个序列的一个数据
|
||||
//读取新数据,如果没有,那就保持
|
||||
|
||||
setSerieData("sin",sin(timeseries));
|
||||
setSerieData("cos",cos(timeseries));
|
||||
setSerieData("tan",tan(timeseries));
|
||||
//setSerieData("acos",acos(timeseries));
|
||||
//setSerieData("asin",asin(timeseries));
|
||||
//setSerieData("atan",atan(timeseries));
|
||||
|
||||
setSerieData("x1",1 * timeseries + 1);
|
||||
setSerieData("x2",timeseries * timeseries + 1);
|
||||
setSerieData("x3",timeseries * timeseries * timeseries + 1);
|
||||
setSerieData("sqrt",sqrt(timeseries));
|
||||
//setSerieData("pow",pow(timeseries,2));
|
||||
//setSerieData("log",log(timeseries + 1));
|
||||
|
||||
//增加到新的数据 里面
|
||||
|
||||
|
||||
//setSerieData("log10",log10(timeseries));
|
||||
//setSerieData("exp",exp(timeseries));
|
||||
setSerieData("tan+1",tan(timeseries + 1));
|
||||
//setSerieData("log2",log2(timeseries));
|
||||
setSerieData("timeseries",timeseries);
|
||||
setSerieData("12",timeseries * log(timeseries));
|
||||
|
||||
//滚动显示器
|
||||
//当到达最大时(0.95),开始滚动
|
||||
QList<QAbstractAxis *> axList = chartView->chart()->axes(Qt::Horizontal);
|
||||
for(QAbstractAxis *axis:axList)
|
||||
{
|
||||
QValueAxis *a = qobject_cast<QValueAxis *>(axis);
|
||||
qreal dwidth = chartView->chart()->plotArea().width()/(a->tickCount() - 1);
|
||||
|
||||
if(timeseries > (a->min() + (a->max() - a->min()) * 0.95))
|
||||
{
|
||||
|
||||
chartView->chart()->scroll(dwidth * 0.01,0);//每次增加0.01
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//void Scope::addSeries(void)
|
||||
bool Scope::addSeries(QString name = tr("new"))
|
||||
{
|
||||
//查找有没有这个名字的序列
|
||||
bool isContain = false;
|
||||
QList<QAbstractSeries *> slist = chartView->chart()->series();
|
||||
for(QAbstractSeries *serie:slist)
|
||||
{
|
||||
if(serie->name() == name)
|
||||
{
|
||||
isContain = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(isContain == false)
|
||||
{
|
||||
QLineSeries *s = new QLineSeries();
|
||||
s->setColor(ColorMap::Color[ChannelIndex]);
|
||||
s->setName(name);
|
||||
s->append(timeseries,0);
|
||||
chartView->chart()->addSeries(s);
|
||||
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
|
||||
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
|
||||
|
||||
ChannelIndex ++;
|
||||
if(ChannelIndex >= 16)
|
||||
{
|
||||
ChannelIndex = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo() << "this chart has contain a serie name by :" << name;
|
||||
}
|
||||
return isContain;
|
||||
}
|
||||
|
||||
bool Scope::addSeries(QString name = tr("new"), int index = 0)
|
||||
{
|
||||
if(index >= 16)
|
||||
{
|
||||
int a = index/16;
|
||||
ChannelIndex = index - a*16;
|
||||
}
|
||||
|
||||
//查找有没有这个名字的序列
|
||||
bool isContain = false;
|
||||
QList<QAbstractSeries *> slist = chartView->chart()->series();
|
||||
for(QAbstractSeries *serie:slist)
|
||||
{
|
||||
if(serie->name() == name)
|
||||
{
|
||||
isContain = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(isContain == false)
|
||||
{
|
||||
QLineSeries *s = new QLineSeries();
|
||||
s->setColor(ColorMap::Color[ChannelIndex]);
|
||||
s->setName(name);
|
||||
s->append(timeseries,0);
|
||||
chartView->chart()->addSeries(s);
|
||||
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
|
||||
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
|
||||
|
||||
ChannelIndex ++;
|
||||
if(ChannelIndex >= 16)
|
||||
{
|
||||
ChannelIndex = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo() << "this chart has contain a serie name by :" << name;
|
||||
}
|
||||
return isContain;
|
||||
}
|
||||
|
||||
bool Scope::addSeries(QString name = tr("new"),QColor color = QColor("#FFFFFF"))
|
||||
{
|
||||
//查找有没有这个名字的序列
|
||||
bool isContain = false;
|
||||
QList<QAbstractSeries *> slist = chartView->chart()->series();
|
||||
for(QAbstractSeries *serie:slist)
|
||||
{
|
||||
if(serie->name() == name)
|
||||
{
|
||||
isContain = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(isContain == false)
|
||||
{
|
||||
QLineSeries *s = new QLineSeries();
|
||||
s->setColor(color);
|
||||
s->setName(name);
|
||||
s->append(timeseries,0);
|
||||
chartView->chart()->addSeries(s);
|
||||
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
|
||||
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
|
||||
|
||||
ChannelIndex ++;
|
||||
if(ChannelIndex >= 16)
|
||||
{
|
||||
ChannelIndex = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo() << "this chart has contain a serie name by :" << name;
|
||||
}
|
||||
|
||||
return isContain;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Scope::setSerieData(QString name,QVariant data)
|
||||
{
|
||||
//获取当前的时间撮
|
||||
//series->append(timeseries,data.toFloat());
|
||||
//查找有没有这个名字的序列
|
||||
bool isContain = false;
|
||||
QList<QAbstractSeries *> slist = chartView->chart()->series();
|
||||
for(QAbstractSeries *serie:slist)
|
||||
{
|
||||
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
|
||||
if(s->name() == name)
|
||||
{
|
||||
s->append(timeseries,data.toDouble());
|
||||
isContain = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(isContain == false)
|
||||
{
|
||||
/*
|
||||
QLineSeries *s = new QLineSeries();
|
||||
s->setColor(ColorMap::Color[ChannelIndex]);
|
||||
s->setName(name);
|
||||
s->append(timeseries,data.toDouble());
|
||||
chartView->chart()->addSeries(s);
|
||||
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
|
||||
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
|
||||
*/
|
||||
|
||||
addSeries(name,ChannelIndex);
|
||||
|
||||
ChannelIndex ++;
|
||||
if(ChannelIndex >= 16)
|
||||
{
|
||||
ChannelIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scope::removeSerie(QString name)
|
||||
{
|
||||
//找到,删除
|
||||
QList<QAbstractSeries *> slist = chartView->chart()->series();
|
||||
for(QAbstractSeries *serie:slist)
|
||||
{
|
||||
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
|
||||
if(s->name() == name)
|
||||
{
|
||||
chartView->chart()->removeSeries(s);
|
||||
s->clear();
|
||||
s->deleteLater();
|
||||
delete s;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,16 +3,39 @@
|
||||
|
||||
#include <QWidget>
|
||||
#include "QDebug"
|
||||
|
||||
#include <QKeyEvent>
|
||||
#include "QTimer"
|
||||
|
||||
#include "QChart"
|
||||
#include <QChartView>
|
||||
#include <QLineSeries>
|
||||
#include <QValueAxis>
|
||||
#include <QDateTimeAxis>
|
||||
|
||||
using namespace QtCharts;
|
||||
|
||||
namespace ColorMap{
|
||||
static QColor Color[16] = {
|
||||
QColor(165, 42, 42), //Channel1Color
|
||||
QColor (255, 127, 80), //Channel2Color
|
||||
QColor (30, 144, 255), //Channel3Color
|
||||
QColor (218, 165, 32), //Channel4Color
|
||||
QColor (255, 0, 255), //Channel5Color
|
||||
QColor (147, 112, 219), //Channel6Color
|
||||
QColor (0, 255, 255), //Channel7Color
|
||||
QColor (0, 0, 128), //Channel8Color
|
||||
QColor (128, 0, 128), //Channel9Color
|
||||
QColor (46, 139, 87), //Channel10Color
|
||||
QColor (152, 251, 152), //Channel11Color
|
||||
QColor (0, 255, 127), //Channel12Color
|
||||
QColor (0, 0, 255), //Channel13Color
|
||||
QColor (135, 206, 250), //Channel14Color
|
||||
QColor (255, 192, 203), //Channel15Color
|
||||
QColor (127, 255, 0) //Channel16Color
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class Scope;
|
||||
}
|
||||
@@ -26,14 +49,23 @@ public:
|
||||
explicit Scope(QWidget *parent = nullptr);
|
||||
~Scope();
|
||||
|
||||
static int ChannelIndex;
|
||||
|
||||
protected:
|
||||
void mouseMoveEvent(QMouseEvent *e);
|
||||
void mouseReleaseEvent(QMouseEvent *e);
|
||||
void mousePressEvent(QMouseEvent *e);
|
||||
void mouseDoubleClickEvent(QMouseEvent *e);
|
||||
void wheelEvent(QWheelEvent *e);
|
||||
void resizeEvent(QResizeEvent *event);
|
||||
void resizeEvent(QResizeEvent *e);
|
||||
|
||||
public slots:
|
||||
bool addSeries(QString name);
|
||||
bool addSeries(QString name,int index);
|
||||
bool addSeries(QString name, QColor color);
|
||||
void removeSerie(QString name);
|
||||
|
||||
void setSerieData(QString name,QVariant data);
|
||||
|
||||
private slots:
|
||||
void timeout(void);
|
||||
@@ -45,6 +77,11 @@ private:
|
||||
QChartView *chartView;
|
||||
QLineSeries *series;
|
||||
|
||||
qreal timeseries = 0;//时间基准,注意时间长度,可能会出现时间反转的情况
|
||||
|
||||
|
||||
bool isPress = false;
|
||||
|
||||
};
|
||||
|
||||
#endif // SCOPE_H
|
||||
|
||||
@@ -34,19 +34,6 @@
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Raised</enum>
|
||||
</property>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>430</x>
|
||||
<y>20</y>
|
||||
<width>201</width>
|
||||
<height>161</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
||||
Reference in New Issue
Block a user