重写图线显示
This commit is contained in:
@@ -1,6 +1,165 @@
|
||||
#include "Chart.h"
|
||||
#include "Chart.h"
|
||||
|
||||
Chart::Chart(QObject *parent) : QObject(parent)
|
||||
Chart::Chart(QChart *chart, QWidget *parent)
|
||||
{
|
||||
chart->legend()->setAlignment(Qt::AlignRight);
|
||||
chart->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
|
||||
chart->setTheme(QChart::ChartThemeLight);
|
||||
|
||||
QValueAxis *axisX = new QValueAxis();
|
||||
axisX->setRange(-5, 5);
|
||||
axisX->setTickCount(11);
|
||||
axisX->setLabelFormat("%.2f");
|
||||
chart->addAxis(axisX, Qt::AlignBottom);
|
||||
|
||||
QValueAxis *axisY = new QValueAxis();
|
||||
axisY->setRange(-3, 3);
|
||||
axisY->setTickCount(7);
|
||||
axisY->setLabelFormat("%.2f");
|
||||
chart->addAxis(axisY, Qt::AlignLeft);
|
||||
|
||||
setParent(parent);
|
||||
setChart(chart);
|
||||
}
|
||||
|
||||
Chart::Chart(QWidget *parent)
|
||||
{
|
||||
setParent(parent);
|
||||
|
||||
chart()->legend()->setAlignment(Qt::AlignRight);
|
||||
chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
|
||||
chart()->setTheme(QChart::ChartThemeLight);
|
||||
|
||||
QValueAxis *axisX = new QValueAxis();
|
||||
axisX->setRange(-5, 5);
|
||||
axisX->setTickCount(11);
|
||||
axisX->setLabelFormat("%.2f");
|
||||
chart()->addAxis(axisX, Qt::AlignBottom);
|
||||
|
||||
QValueAxis *axisY = new QValueAxis();
|
||||
axisY->setRange(-3, 3);
|
||||
axisY->setTickCount(7);
|
||||
axisY->setLabelFormat("%.2f");
|
||||
chart()->addAxis(axisY, Qt::AlignLeft);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Chart::~Chart()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Chart::mouseMoveEvent(QMouseEvent *e)
|
||||
{
|
||||
const QPoint curPos = e->pos();
|
||||
if (isPress)
|
||||
{
|
||||
QPoint offset = curPos - lastPoint;
|
||||
lastPoint = curPos;
|
||||
chart()->scroll(-offset.x(), offset.y());
|
||||
}
|
||||
}
|
||||
|
||||
void Chart::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
isPress = false;
|
||||
setCursor(Qt::ArrowCursor);
|
||||
}
|
||||
|
||||
void Chart::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
if (e->button() == Qt::RightButton)
|
||||
{
|
||||
lastPoint = e->pos();
|
||||
isPress = true;
|
||||
setCursor(Qt::ClosedHandCursor);
|
||||
}
|
||||
}
|
||||
|
||||
void Chart::mouseDoubleClickEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Chart double clicked" << e;
|
||||
}
|
||||
|
||||
void Chart::wheelEvent(QWheelEvent *e)
|
||||
{
|
||||
//以鼠标坐标为中心进行缩放
|
||||
QPointF curVal = chart()->mapToValue(QPointF(e->pos()));
|
||||
//缩放比例
|
||||
const double factor = 1.5;
|
||||
switch(e->modifiers())
|
||||
{
|
||||
case Qt::ControlModifier://垂直缩放
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chart()->axisY());
|
||||
const double Min = axis->min();
|
||||
const double Max = axis->max();
|
||||
const double Central = curVal.y();
|
||||
|
||||
double bottomOffset;
|
||||
double topOffset;
|
||||
if (e->delta() > 0)
|
||||
{//放大
|
||||
bottomOffset = 1.0 / factor * (Central - Min);
|
||||
topOffset = 1.0 / factor * (Max - Central);
|
||||
}
|
||||
else
|
||||
{//缩小
|
||||
bottomOffset = 1.0 * factor * (Central - Min);
|
||||
topOffset = 1.0 * factor * (Max - Central);
|
||||
}
|
||||
chart()->axisY()->setRange(Central - bottomOffset, Central + topOffset);
|
||||
|
||||
}break;
|
||||
case Qt::ShiftModifier://水平缩放
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chart()->axisX());
|
||||
const double Min = axis->min();
|
||||
const double Max = axis->max();
|
||||
const double Central = curVal.x();
|
||||
|
||||
double bottomOffset;
|
||||
double topOffset;
|
||||
if (e->delta() > 0)
|
||||
{//放大
|
||||
bottomOffset = 1.0 / factor * (Central - Min);
|
||||
topOffset = 1.0 / factor * (Max - Central);
|
||||
}
|
||||
else
|
||||
{//缩小
|
||||
bottomOffset = 1.0 * factor * (Central - Min);
|
||||
topOffset = 1.0 * factor * (Max - Central);
|
||||
}
|
||||
chart()->axisX()->setRange(Central - bottomOffset, Central + topOffset);
|
||||
}break;
|
||||
default://水平移动
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chart()->axisX());
|
||||
qreal dwidth = chart()->plotArea().width()/(axis->tickCount()*2);
|
||||
if(e->delta() > 0)
|
||||
{
|
||||
chart()->scroll(-dwidth,0);
|
||||
}
|
||||
else if(e->delta() < 0)
|
||||
{
|
||||
chart()->scroll(dwidth,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Chart::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
//qDebug() << e;
|
||||
//Q_UNUSED(e)
|
||||
chart()->setPos(0,0);
|
||||
chart()->setGeometry(this->geometry());
|
||||
}
|
||||
|
||||
bool Chart::event(QEvent *event)
|
||||
{
|
||||
return QGraphicsView::event(event);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,60 @@
|
||||
#ifndef CHART_H
|
||||
#ifndef CHART_H
|
||||
#define CHART_H
|
||||
|
||||
#include <QObject>
|
||||
#include "QWidget"
|
||||
#include "QDebug"
|
||||
#include "QChart"
|
||||
#include <QChartView>
|
||||
#include <QLineSeries>
|
||||
#include <QValueAxis>
|
||||
#include <QDateTimeAxis>
|
||||
#include <QSplineSeries>
|
||||
|
||||
class Chart : public QObject
|
||||
#include <QMouseEvent>
|
||||
#include <QGraphicsSimpleTextItem>
|
||||
|
||||
using namespace QtCharts;
|
||||
|
||||
class Chart : public QChartView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Chart(QObject *parent = nullptr);
|
||||
explicit Chart( QWidget *parent = nullptr);
|
||||
explicit Chart(QChart *chart, QWidget *parent = nullptr);
|
||||
|
||||
~Chart();
|
||||
|
||||
protected:
|
||||
|
||||
bool event(QEvent *event);
|
||||
|
||||
void mouseMoveEvent(QMouseEvent *e);
|
||||
void mouseReleaseEvent(QMouseEvent *e);
|
||||
void mousePressEvent(QMouseEvent *e);
|
||||
void mouseDoubleClickEvent(QMouseEvent *e);
|
||||
void wheelEvent(QWheelEvent *e);
|
||||
void resizeEvent(QResizeEvent *e);
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
QGraphicsSimpleTextItem *m_coordItem;
|
||||
|
||||
QVariant m_xMin;
|
||||
QVariant m_xMax;
|
||||
QVariant m_yMin;
|
||||
QVariant m_yMax;
|
||||
|
||||
QPoint lastPoint;
|
||||
|
||||
bool isPress = false;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // CHART_H
|
||||
|
||||
@@ -20,42 +20,21 @@ Scope::Scope(QWidget *parent) :
|
||||
this->setStyleSheet(stylesheet);
|
||||
file.close();
|
||||
|
||||
|
||||
|
||||
timeseries = 0;
|
||||
|
||||
chartView = new QChartView(this);
|
||||
chartView = new Chart(this);//new QChartView(this);
|
||||
chartView->setRenderHint(QPainter::Antialiasing);
|
||||
chartView->chart()->legend()->setAlignment(Qt::AlignRight);
|
||||
chartView->chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
|
||||
chartView->chart()->setTheme(QChart::ChartThemeLight);
|
||||
|
||||
//chartView->setMouseTracking(true);
|
||||
//chartView->setRubberBand(QChartView::RectangleRubberBand);
|
||||
//chartView->chart()->setAcceptDrops(true);
|
||||
|
||||
QValueAxis *axisX = new QValueAxis();
|
||||
axisX->setRange(-5, 5);
|
||||
axisX->setTickCount(11);
|
||||
axisX->setLabelFormat("%.2f");
|
||||
chartView->chart()->addAxis(axisX, Qt::AlignBottom);
|
||||
|
||||
QValueAxis *axisY = new QValueAxis();
|
||||
axisY->setRange(-3, 3);
|
||||
axisY->setTickCount(7);
|
||||
axisY->setLabelFormat("%.2f");
|
||||
chartView->chart()->addAxis(axisY, Qt::AlignLeft);
|
||||
|
||||
chartView->setGeometry(ui->frame->geometry());
|
||||
|
||||
//默认不使用,因为使用显卡加速图形会闪
|
||||
setUseOpenGL(false);
|
||||
|
||||
|
||||
updateTimer = new QTimer(this);
|
||||
connect(updateTimer,SIGNAL(timeout()),
|
||||
this,SLOT(timeout()));
|
||||
updateTimer->start(50);//20hz刷新
|
||||
|
||||
qInfo() << "Scope inital ready";
|
||||
}
|
||||
|
||||
Scope::~Scope()
|
||||
@@ -67,98 +46,33 @@ Scope::~Scope()
|
||||
|
||||
void Scope::mouseMoveEvent(QMouseEvent *e)
|
||||
{
|
||||
//移动整幅图
|
||||
qDebug() << "Scope Move" << e;
|
||||
QWidget::mouseMoveEvent(e);
|
||||
}
|
||||
|
||||
void Scope::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Scope release" << e;
|
||||
isPress = false;
|
||||
QWidget::mouseReleaseEvent(e);
|
||||
}
|
||||
|
||||
void Scope::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Scope press" << e;
|
||||
isPress = true;
|
||||
QWidget::mousePressEvent(e);
|
||||
}
|
||||
|
||||
void Scope::mouseDoubleClickEvent(QMouseEvent *e)
|
||||
{
|
||||
qDebug() << "Scope double clicked" << e;
|
||||
QWidget::mouseDoubleClickEvent(e);
|
||||
}
|
||||
|
||||
void Scope::wheelEvent(QWheelEvent *e)
|
||||
{
|
||||
//以鼠标坐标为中心进行缩放
|
||||
QPointF curVal = chartView->chart()->mapToValue(QPointF(e->pos()));
|
||||
//缩放比例
|
||||
const double factor = 1.5;
|
||||
switch(e->modifiers())
|
||||
{
|
||||
case Qt::ControlModifier://垂直缩放
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisY());
|
||||
const double Min = axis->min();
|
||||
const double Max = axis->max();
|
||||
const double Central = curVal.y();
|
||||
|
||||
double bottomOffset;
|
||||
double topOffset;
|
||||
if (e->delta() > 0)
|
||||
{//放大
|
||||
bottomOffset = 1.0 / factor * (Central - Min);
|
||||
topOffset = 1.0 / factor * (Max - Central);
|
||||
}
|
||||
else
|
||||
{//缩小
|
||||
bottomOffset = 1.0 * factor * (Central - Min);
|
||||
topOffset = 1.0 * factor * (Max - Central);
|
||||
}
|
||||
chartView->chart()->axisY()->setRange(Central - bottomOffset, Central + topOffset);
|
||||
|
||||
}break;
|
||||
case Qt::ShiftModifier://水平缩放
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisX());
|
||||
const double Min = axis->min();
|
||||
const double Max = axis->max();
|
||||
const double Central = curVal.x();
|
||||
|
||||
double bottomOffset;
|
||||
double topOffset;
|
||||
if (e->delta() > 0)
|
||||
{//放大
|
||||
bottomOffset = 1.0 / factor * (Central - Min);
|
||||
topOffset = 1.0 / factor * (Max - Central);
|
||||
}
|
||||
else
|
||||
{//缩小
|
||||
bottomOffset = 1.0 * factor * (Central - Min);
|
||||
topOffset = 1.0 * factor * (Max - Central);
|
||||
}
|
||||
chartView->chart()->axisX()->setRange(Central - bottomOffset, Central + topOffset);
|
||||
}break;
|
||||
default://水平移动
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisX());
|
||||
qreal dwidth = chartView->chart()->plotArea().width()/(axis->tickCount()*2);
|
||||
if(e->delta() > 0)
|
||||
{
|
||||
chartView->chart()->scroll(-dwidth,0);
|
||||
}
|
||||
else if(e->delta() < 0)
|
||||
{
|
||||
chartView->chart()->scroll(dwidth,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
QWidget::wheelEvent(e);
|
||||
}
|
||||
|
||||
void Scope::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
Q_UNUSED(e)
|
||||
chartView->setGeometry(ui->frame->geometry());
|
||||
QWidget::resizeEvent(e);
|
||||
}
|
||||
|
||||
void Scope::timeout(void)//100Hz
|
||||
@@ -175,8 +89,8 @@ void Scope::chartUpdate(void)
|
||||
//读取新数据,如果没有,那就保持
|
||||
|
||||
//滚动显示器
|
||||
//当到达最大时(0.95),开始滚动
|
||||
|
||||
//当到达最大时(0.95),开始滚动
|
||||
if(isScroll == true)
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisX());
|
||||
@@ -186,8 +100,6 @@ void Scope::chartUpdate(void)
|
||||
chartView->chart()->scroll(dwidth * 0.01,0);//每次增加0.01
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -318,6 +230,26 @@ bool Scope::addSeries(QString name = tr("new"),QColor color = QColor("#FFFFFF"))
|
||||
|
||||
void Scope::setSerieData(QString name,QVariant data)
|
||||
{
|
||||
//出新了比以前更大或者更小的值,那么就设置一下量程
|
||||
{
|
||||
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisY());
|
||||
if(data.toDouble() > axis->max())
|
||||
{
|
||||
//设置最大值
|
||||
const double Min = axis->min();
|
||||
chartView->chart()->axisY()->setRange(Min, data.toDouble() * 1.1);
|
||||
|
||||
}
|
||||
else if(data.toDouble() < axis->min())
|
||||
{
|
||||
//设置最小值
|
||||
const double Max = axis->max();
|
||||
chartView->chart()->axisY()->setRange(data.toDouble() * 1.1, Max);
|
||||
}
|
||||
//delete axis;
|
||||
}
|
||||
|
||||
|
||||
//获取当前的时间撮
|
||||
//series->append(timeseries,data.toFloat());
|
||||
//查找有没有这个名字的序列
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
|
||||
#include "QMimeData"
|
||||
|
||||
|
||||
|
||||
#include "Chart.h"
|
||||
using namespace QtCharts;
|
||||
|
||||
|
||||
@@ -99,7 +102,7 @@ private slots:
|
||||
private:
|
||||
Ui::Scope *ui;
|
||||
|
||||
QChartView *chartView;
|
||||
Chart *chartView;
|
||||
QLineSeries *series;
|
||||
|
||||
QTimer *updateTimer = nullptr;
|
||||
@@ -109,7 +112,10 @@ private:
|
||||
bool isOpenGL = false;
|
||||
bool isScroll = true;
|
||||
|
||||
bool isPress = false;
|
||||
double maxValue;
|
||||
double minValue;
|
||||
|
||||
//Chart *mychart = nullptr;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user