update chart zoom in and out

This commit is contained in:
guangzong chen 2022-06-10 19:19:38 -04:00
parent c6db7dc49e
commit fc5eb437d0
No known key found for this signature in database
GPG Key ID: E7F2158184087A5A
6 changed files with 157 additions and 66 deletions

View File

@ -15,7 +15,7 @@ find_package(Qt6 COMPONENTS
Charts Charts
REQUIRED) REQUIRED)
add_executable(com main.cpp main_window.cpp main_window.h) add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h)
#add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h) #add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h)
target_link_libraries(com target_link_libraries(com
Qt::Core Qt::Core

83
GChartView.cpp Normal file
View File

@ -0,0 +1,83 @@
//
// Created by zong on 6/10/22.
//
// You may need to build the project (run Qt uic code generator) to get "ui_gchartview.h" resolved
#include "GChartView.h"
GChartView::GChartView(QWidget *parent) : QChartView(parent) {
data = new QLineSeries();
axis_x = new QValueAxis();
axis_y = new QValueAxis();
chart = new QChart();
chart->setTitle("WIP");
axis_x->setRange(0, 5);
axis_y->setRange(-2.5, 2.5);
axis_x->setTickType(QValueAxis::TickType::TicksDynamic);
axis_x->setTickInterval(1);
axis_x->setLabelFormat("%d");
axis_y->setTickCount(21);
chart->addAxis(axis_x, Qt::AlignBottom);
chart->addAxis(axis_y, Qt::AlignLeft);
chart->addSeries(data);
this->setChart(chart);
data->attachAxis(axis_x);
data->attachAxis(axis_y);
}
GChartView::~GChartView() {
}
void GChartView::wheelEvent(QWheelEvent *event) {
auto pixels = event->pixelDelta();
auto degrees = event->angleDelta();
auto cursor_y = event->position().y();
auto cur_range_max = axis_y->max();
auto cur_range_min = axis_y->min();
// left and top corner is origin
auto pos_max = chart->mapToPosition(QPointF(0, cur_range_min)).y();
auto pos_min = chart->mapToPosition(QPointF(0, cur_range_max)).y();
auto new_range = 5.0;
auto center = (qreal) 0.0;
if (cursor_y < pos_max && cursor_y > pos_min) {
center = chart->mapToValue(QPointF(0, cursor_y)).y();
auto cur_range = (cur_range_max - cur_range_min);
if (pixels.y() > 0)
new_range = cur_range * 0.8;
else
new_range = cur_range / 0.8;
}
// setup new axis
auto new_max = (center + new_range / 2.0);
auto new_min = (center - new_range / 2.0);
if (new_max > 2.5) {
new_min -= (new_max - 2.5);
new_max = 2.5;
}
if (new_min < -2.5) {
new_max += (-2.5 - new_min);
new_min = 2.5;
new_max = new_max > 2.5 ? 2.5 : new_max;
}
axis_y->setRange(new_min, new_max);
qDebug() << new_min << " " << new_max;
}
void GChartView::append(qreal x, qreal y) {
data->append(x, y);
while (data->count() > 5000) {
data->remove(0);
}
auto beg = data->at(0);
auto end = data->at(data->count());
if (end.x() > 5) {
axis_x->setRange(beg.x(), end.x());
}
}

38
GChartView.h Normal file
View File

@ -0,0 +1,38 @@
//
// Created by zong on 6/10/22.
//
#ifndef COM_GCHARTVIEW_H
#define COM_GCHARTVIEW_H
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
#include <QChart>
QT_BEGIN_NAMESPACE
namespace Ui { class gchartview; }
QT_END_NAMESPACE
class GChartView : public QChartView {
Q_OBJECT
public:
explicit GChartView(QWidget *parent = nullptr);
~GChartView() override;
void wheelEvent(QWheelEvent *even) override;
private:
QLineSeries* data;
QValueAxis* axis_x;
QValueAxis* axis_y;
QChart* chart;
public:
void append(qreal x, qreal y);
};
#endif //COM_GCHARTVIEW_H

12
main.ui
View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1089</width> <width>2028</width>
<height>943</height> <height>1035</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -160,7 +160,7 @@
</property> </property>
</widget> </widget>
</widget> </widget>
<widget class="QChartView" name="plotChartView"> <widget class="GChartView" name="plotChartView">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>600</width> <width>600</width>
@ -177,7 +177,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1089</width> <width>2028</width>
<height>31</height> <height>31</height>
</rect> </rect>
</property> </property>
@ -186,9 +186,9 @@
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>
<class>QChartView</class> <class>GChartView</class>
<extends>QGraphicsView</extends> <extends>QGraphicsView</extends>
<header>qchartview.h</header> <header>GChartView.h</header>
</customwidget> </customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>

View File

@ -42,8 +42,6 @@ void main_window::config_enable(bool ena) {
receivefileStream.setDevice(&receivefile); receivefileStream.setDevice(&receivefile);
rawDataFile.setFileName(filename + "raw"); rawDataFile.setFileName(filename + "raw");
rawDataFile.open(QIODevice::Append); rawDataFile.open(QIODevice::Append);
} }
} }
@ -69,35 +67,6 @@ void main_window::setUiComponent() {
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port())); QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port()));
QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port())); QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port()));
lineChart.chart->setTitle("WIP");
lineChart.axis_x->setRange(0, 5);
lineChart.axis_y->setRange(-2.5, 2.5);
lineChart.axis_x->setTickCount(11);
lineChart.axis_y->setTickCount(11);
lineChart.chart->addAxis(lineChart.axis_x, Qt::AlignBottom);
lineChart.chart->addAxis(lineChart.axis_y, Qt::AlignLeft);
lineChart.chart->addSeries(lineChart.data);
plotChartView->setChart(lineChart.chart);
lineChart.data->attachAxis(lineChart.axis_x);
lineChart.data->attachAxis(lineChart.axis_y);
// linedata = new QLineSeries();
// chart = new QChart();
// chart->setTitle("WIP");
// QValueAxis *axis_x = new QValueAxis();
// axis_x->setRange(0, 5);
// axis_x->setTickCount(11);
// QValueAxis *axis_y = new QValueAxis();
// axis_y->setRange(-2.5, 2.5);
// axis_y->setTickCount(11);
// chart->legend()->hide();
// chart->addAxis(axis_x, Qt::AlignBottom);
// chart->addAxis(axis_y, Qt::AlignLeft);
// chart->addSeries(linedata);
// plotChartView->setChart(chart);
// linedata->attachAxis(axis_x);
// linedata->attachAxis(axis_y);
} }
@ -140,7 +109,7 @@ main_window::main_window(QWidget *parent) : QMainWindow(parent) {
} }
main_window::~main_window() { main_window::~main_window() {
// delete ui; delete ui;
} }
void main_window::update_ui_port() { void main_window::update_ui_port() {
@ -154,7 +123,7 @@ void main_window::open_port() {
if (port.isOpen()) { if (port.isOpen()) {
port.close(); port.close();
config_enable(true); config_enable(true);
statusbar->showMessage("close port success"); statusbar->showMessage("close port success", 500);
return; return;
} }
if (portsComboBox->count() == 0) { return; } if (portsComboBox->count() == 0) { return; }
@ -166,7 +135,7 @@ void main_window::open_port() {
else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop); else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop);
else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop); else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop);
else { else {
box.setText("wrong stopbits settings"), box.exec(); msgBox.setText("wrong stopbits settings"), msgBox.exec();
return; return;
} }
// TODO: read info from config // TODO: read info from config
@ -176,7 +145,7 @@ void main_window::open_port() {
port.setFlowControl(QSerialPort::NoFlowControl); port.setFlowControl(QSerialPort::NoFlowControl);
if (port.open(QIODevice::ReadWrite)) { if (port.open(QIODevice::ReadWrite)) {
config_enable(false); config_enable(false);
statusbar->showMessage("open port success"); statusbar->showMessage("open port success", 500);
} else { } else {
config_enable(true); config_enable(true);
} }
@ -190,7 +159,7 @@ bool first = true;
void main_window::read_data() { void main_window::read_data() {
static int receive_num_cnt = 0; static int receive_num_cnt = -2;
static int receive_byte_cnt = 0; static int receive_byte_cnt = 0;
static int val = 0; static int val = 0;
auto data = port.readAll(); auto data = port.readAll();
@ -217,37 +186,38 @@ void main_window::read_data() {
data = first_frame; data = first_frame;
first = false; first = false;
} }
// TODO: BUG int data overflow
rawDataFile.write(data); rawDataFile.write(data);
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
if (receive_byte_cnt % 4 == 0) { if (receive_byte_cnt % 4 == 0) {
if (data[i] != 0) { if (data[i] != 0) {
// TODO: raise error msgBox.setText("Error occurred, please restart transmission.");
msgBox.exec();
return; return;
} }
receive_num_cnt++; receive_num_cnt++;
if (receive_num_cnt == -1)
continue;
int real_number = val; int real_number = val;
if (real_number & 0x800000) { if (real_number & 0x800000) {
// negtive number // negative number
real_number = -(~real_number & 0x7fffff + 1); real_number = -(~real_number & 0x7fffff + 1);
} }
receivefileStream << real_number << " "; receivefileStream << real_number << " ";
double scaled = (double) real_number / (double) (0x7fffff) * 2.5; double scaled = (double) real_number / (double) (0x7fffff) * 2.5;
lineChart.data->append((double) receive_byte_cnt / 1000.0, scaled); plotChartView->append ((double) receive_num_cnt / 1000.0, scaled);
while (lineChart.data->count() > 5000) {
// data more than 5000 second, remove first data
lineChart.data->remove(0);
}
val = 0; val = 0;
continue; continue;
} }
val <<= 8; val <<= 8;
val |= data[i]; val |= data[i];
receive_byte_cnt++;
} }
QString new_text = QString(data.toHex(' ').toUpper()) + " "; QString new_text = QString(data.toHex(' ').toUpper()) + " ";
hexTextEdit->moveCursor(QTextCursor::End); hexTextEdit->moveCursor(QTextCursor::End);
hexTextEdit->insertPlainText(new_text); hexTextEdit->insertPlainText(new_text);
auto tmp = hexTextEdit->toPlainText(); auto tmp = hexTextEdit->toPlainText();
asciiTextEdit->moveCursor(QTextCursor::End);
asciiTextEdit->insertPlainText(QString(data));
if (tmp.length() > 1e3) { if (tmp.length() > 1e3) {
hexTextEdit->setPlainText(tmp.last((int) 1e3)); hexTextEdit->setPlainText(tmp.last((int) 1e3));
hexTextEdit->moveCursor(QTextCursor::End); hexTextEdit->moveCursor(QTextCursor::End);

View File

@ -14,18 +14,18 @@
#include <QSplineSeries> #include <QSplineSeries>
#include <QValueAxis> #include <QValueAxis>
typedef struct _Glinechart{ //typedef struct _Glinechart{
QLineSeries* data; // QLineSeries* data;
QValueAxis* axis_x; // QValueAxis* axis_x;
QValueAxis* axis_y; // QValueAxis* axis_y;
QChart* chart; // QChart* chart;
_Glinechart() { // _Glinechart() {
data = new QLineSeries(); // data = new QLineSeries();
axis_x = new QValueAxis(); // axis_x = new QValueAxis();
axis_y = new QValueAxis(); // axis_y = new QValueAxis();
chart = new QChart(); // chart = new QChart();
} // }
}GLineChart; //}GLineChart;
class main_window : public QMainWindow { class main_window : public QMainWindow {
Q_OBJECT Q_OBJECT
@ -62,10 +62,10 @@ private:
QTextEdit *asciiTextEdit; QTextEdit *asciiTextEdit;
QMenuBar *menubar; QMenuBar *menubar;
QStatusBar *statusbar; QStatusBar *statusbar;
QChartView *plotChartView; GChartView *plotChartView;
QMessageBox box; QMessageBox msgBox;
GLineChart lineChart; // GLineChart lineChart;
private: private:
void setUiComponent(); void setUiComponent();