update chart zoom in and out
This commit is contained in:
parent
c6db7dc49e
commit
fc5eb437d0
@ -15,7 +15,7 @@ find_package(Qt6 COMPONENTS
|
||||
Charts
|
||||
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)
|
||||
target_link_libraries(com
|
||||
Qt::Core
|
||||
|
83
GChartView.cpp
Normal file
83
GChartView.cpp
Normal 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
38
GChartView.h
Normal 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
12
main.ui
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1089</width>
|
||||
<height>943</height>
|
||||
<width>2028</width>
|
||||
<height>1035</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -160,7 +160,7 @@
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QChartView" name="plotChartView">
|
||||
<widget class="GChartView" name="plotChartView">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>600</width>
|
||||
@ -177,7 +177,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1089</width>
|
||||
<width>2028</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -186,9 +186,9 @@
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>QChartView</class>
|
||||
<class>GChartView</class>
|
||||
<extends>QGraphicsView</extends>
|
||||
<header>qchartview.h</header>
|
||||
<header>GChartView.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
|
@ -42,8 +42,6 @@ void main_window::config_enable(bool ena) {
|
||||
receivefileStream.setDevice(&receivefile);
|
||||
rawDataFile.setFileName(filename + "raw");
|
||||
rawDataFile.open(QIODevice::Append);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -69,35 +67,6 @@ void main_window::setUiComponent() {
|
||||
|
||||
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_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() {
|
||||
// delete ui;
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void main_window::update_ui_port() {
|
||||
@ -154,7 +123,7 @@ void main_window::open_port() {
|
||||
if (port.isOpen()) {
|
||||
port.close();
|
||||
config_enable(true);
|
||||
statusbar->showMessage("close port success");
|
||||
statusbar->showMessage("close port success", 500);
|
||||
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("2")) port.setStopBits(QSerialPort::TwoStop);
|
||||
else {
|
||||
box.setText("wrong stopbits settings"), box.exec();
|
||||
msgBox.setText("wrong stopbits settings"), msgBox.exec();
|
||||
return;
|
||||
}
|
||||
// TODO: read info from config
|
||||
@ -176,7 +145,7 @@ void main_window::open_port() {
|
||||
port.setFlowControl(QSerialPort::NoFlowControl);
|
||||
if (port.open(QIODevice::ReadWrite)) {
|
||||
config_enable(false);
|
||||
statusbar->showMessage("open port success");
|
||||
statusbar->showMessage("open port success", 500);
|
||||
} else {
|
||||
config_enable(true);
|
||||
}
|
||||
@ -190,7 +159,7 @@ bool first = true;
|
||||
|
||||
|
||||
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 val = 0;
|
||||
auto data = port.readAll();
|
||||
@ -217,37 +186,38 @@ void main_window::read_data() {
|
||||
data = first_frame;
|
||||
first = false;
|
||||
}
|
||||
// TODO: BUG int data overflow
|
||||
rawDataFile.write(data);
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
if (receive_byte_cnt % 4 == 0) {
|
||||
if (data[i] != 0) {
|
||||
// TODO: raise error
|
||||
msgBox.setText("Error occurred, please restart transmission.");
|
||||
msgBox.exec();
|
||||
return;
|
||||
}
|
||||
receive_num_cnt++;
|
||||
if (receive_num_cnt == -1)
|
||||
continue;
|
||||
int real_number = val;
|
||||
if (real_number & 0x800000) {
|
||||
// negtive number
|
||||
// negative number
|
||||
real_number = -(~real_number & 0x7fffff + 1);
|
||||
}
|
||||
receivefileStream << real_number << " ";
|
||||
double scaled = (double) real_number / (double) (0x7fffff) * 2.5;
|
||||
lineChart.data->append((double) receive_byte_cnt / 1000.0, scaled);
|
||||
while (lineChart.data->count() > 5000) {
|
||||
// data more than 5000 second, remove first data
|
||||
lineChart.data->remove(0);
|
||||
}
|
||||
plotChartView->append ((double) receive_num_cnt / 1000.0, scaled);
|
||||
val = 0;
|
||||
continue;
|
||||
}
|
||||
val <<= 8;
|
||||
val |= data[i];
|
||||
receive_byte_cnt++;
|
||||
}
|
||||
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
||||
hexTextEdit->moveCursor(QTextCursor::End);
|
||||
hexTextEdit->insertPlainText(new_text);
|
||||
auto tmp = hexTextEdit->toPlainText();
|
||||
asciiTextEdit->moveCursor(QTextCursor::End);
|
||||
asciiTextEdit->insertPlainText(QString(data));
|
||||
if (tmp.length() > 1e3) {
|
||||
hexTextEdit->setPlainText(tmp.last((int) 1e3));
|
||||
hexTextEdit->moveCursor(QTextCursor::End);
|
||||
|
@ -14,18 +14,18 @@
|
||||
#include <QSplineSeries>
|
||||
#include <QValueAxis>
|
||||
|
||||
typedef struct _Glinechart{
|
||||
QLineSeries* data;
|
||||
QValueAxis* axis_x;
|
||||
QValueAxis* axis_y;
|
||||
QChart* chart;
|
||||
_Glinechart() {
|
||||
data = new QLineSeries();
|
||||
axis_x = new QValueAxis();
|
||||
axis_y = new QValueAxis();
|
||||
chart = new QChart();
|
||||
}
|
||||
}GLineChart;
|
||||
//typedef struct _Glinechart{
|
||||
// QLineSeries* data;
|
||||
// QValueAxis* axis_x;
|
||||
// QValueAxis* axis_y;
|
||||
// QChart* chart;
|
||||
// _Glinechart() {
|
||||
// data = new QLineSeries();
|
||||
// axis_x = new QValueAxis();
|
||||
// axis_y = new QValueAxis();
|
||||
// chart = new QChart();
|
||||
// }
|
||||
//}GLineChart;
|
||||
|
||||
class main_window : public QMainWindow {
|
||||
Q_OBJECT
|
||||
@ -62,10 +62,10 @@ private:
|
||||
QTextEdit *asciiTextEdit;
|
||||
QMenuBar *menubar;
|
||||
QStatusBar *statusbar;
|
||||
QChartView *plotChartView;
|
||||
GChartView *plotChartView;
|
||||
|
||||
QMessageBox box;
|
||||
GLineChart lineChart;
|
||||
QMessageBox msgBox;
|
||||
// GLineChart lineChart;
|
||||
|
||||
private:
|
||||
void setUiComponent();
|
||||
|
Loading…
x
Reference in New Issue
Block a user