更新代码

This commit is contained in:
feiyangqingyun
2022-05-18 17:39:04 +08:00
parent 96938b94f9
commit a155ad45d3
32 changed files with 145 additions and 322 deletions

View File

@@ -1,13 +1,10 @@
FORMS += \
$$PWD/frmmain.ui \
$$PWD/frmmultiaxes.ui
$$PWD/frmmain.ui
HEADERS += \
$$PWD/frmmain.h \
$$PWD/frmmultiaxes.h \
$$PWD/iconhelper.h
SOURCES += \
$$PWD/frmmain.cpp \
$$PWD/frmmultiaxes.cpp \
$$PWD/iconhelper.cpp

View File

@@ -30,7 +30,6 @@
#include "frmaxistag.h"
#include "frminteraction.h"
#include "frmscrollbar.h"
#include "frmmultiaxes.h"
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
@@ -53,8 +52,17 @@ void frmMain::showEvent(QShowEvent *)
bar->setValue(bar->maximum());
}
QList<QColor> frmMain::colors = QList<QColor>();
void frmMain::initForm()
{
//颜色集合供其他界面使用
colors << QColor(211, 78, 78) << QColor(29, 185, 242) << QColor(170, 162, 119) << QColor(255, 192, 1);
colors << QColor(0, 176, 180) << QColor(0, 113, 193) << QColor(255, 192, 0);
colors << QColor(72, 103, 149) << QColor(185, 87, 86) << QColor(0, 177, 125);
colors << QColor(214, 77, 84) << QColor(71, 164, 233) << QColor(34, 163, 169);
colors << QColor(59, 123, 156) << QColor(162, 121, 197) << QColor(72, 202, 245);
colors << QColor(0, 150, 121) << QColor(111, 9, 176) << QColor(250, 170, 20);
ui->scrollArea->setFixedWidth(170);
ui->widgetLeft->setProperty("flag", "left");
}
@@ -67,26 +75,29 @@ void frmMain::initWidget()
ui->stackedWidget->addWidget(new frmSincScatter);
ui->stackedWidget->addWidget(new frmScatterStyle);
ui->stackedWidget->addWidget(new frmScatterPixmap);
ui->stackedWidget->addWidget(new frmLineStyle);
ui->stackedWidget->addWidget(new frmDate);
ui->stackedWidget->addWidget(new frmTextureBrush);
ui->stackedWidget->addWidget(new frmMultiAxis);
ui->stackedWidget->addWidget(new frmLogarithmic);
ui->stackedWidget->addWidget(new frmRealtimeData);
ui->stackedWidget->addWidget(new frmParametricCurve);
ui->stackedWidget->addWidget(new frmBarChart);
ui->stackedWidget->addWidget(new frmStatistical);
ui->stackedWidget->addWidget(new frmSimpleItem);
ui->stackedWidget->addWidget(new frmItem);
ui->stackedWidget->addWidget(new frmStyled);
ui->stackedWidget->addWidget(new frmAdvancedAxes);
ui->stackedWidget->addWidget(new frmColorMap);
ui->stackedWidget->addWidget(new frmFinancial);
ui->stackedWidget->addWidget(new frmPolarPlot);
ui->stackedWidget->addWidget(new frmAxisTag);
ui->stackedWidget->addWidget(new frmInterAction);
ui->stackedWidget->addWidget(new frmScrollBar);
ui->stackedWidget->addWidget(new frmMultiAxes);
}
void frmMain::initNav()
@@ -97,7 +108,7 @@ void frmMain::initNav()
<< "曲线条样式" << "日期数据图" << "纹理画刷图" << "双坐标曲线" << "对数曲线图"
<< "动态正弦图" << "环形曲线图" << "垂直柱状图" << "箱形盒须图" << "静态指示线"
<< "动态指示线" << "曲线样式图" << "高级坐标轴" << "颜色热力图" << "金融曲线图"
<< "南丁格尔图" << "坐标轴指示" << "相互作用图" << "滚动条曲线" << "多坐标轴图";
<< "南丁格尔图" << "坐标轴指示" << "相互作用图" << "滚动条曲线";
//自动生成按钮
for (int i = 0; i < names.count(); i++) {

View File

@@ -13,6 +13,7 @@ class frmMain : public QWidget
Q_OBJECT
public:
static QList<QColor> colors;
explicit frmMain(QWidget *parent = 0);
~frmMain();

View File

@@ -1,186 +0,0 @@
#include "frmmultiaxes.h"
#include "ui_frmmultiaxes.h"
#include "qfile.h"
#include "qdebug.h"
frmMultiAxes::frmMultiAxes(QWidget *parent) : QWidget(parent), ui(new Ui::frmMultiAxes)
{
ui->setupUi(this);
this->initForm();
}
frmMultiAxes::~frmMultiAxes()
{
delete ui;
}
void frmMultiAxes::initForm()
{
//Y坐标轴的数量
count = 4;
this->initPlot(ui->customPlot1, QCPAxis::atLeft, count);
this->initPlot(ui->customPlot2, QCPAxis::atRight, count);
this->loadPlot(ui->customPlot1, QCPAxis::atLeft, count);
this->loadPlot(ui->customPlot2, QCPAxis::atRight, count);
for (int i = 0; i < count; ++i) {
this->loadData(ui->customPlot1, i);
this->loadData(ui->customPlot2, i);
}
}
void frmMultiAxes::selectionChangedByUser()
{
//联动处理 坐标轴、刻度尺、画布曲线 选中一样则选中所有
QCustomPlot *customPlot = (QCustomPlot *)sender();
QCPAxis::AxisType type;
if (customPlot == ui->customPlot1) {
type = QCPAxis::atLeft;
} else if (customPlot == ui->customPlot2) {
type = QCPAxis::atRight;
}
int selecteIndex = -1;
for (int i = 0; i < count; ++i) {
//获取对应的坐标轴
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
//获取对应的画布曲线
QCPGraph *graph = customPlot->graph(i);
//判断 坐标轴、刻度尺、画布曲线 是否选中了一个
if (axis->selectedParts().testFlag(QCPAxis::spAxis) || axis->selectedParts().testFlag(QCPAxis::spTickLabels) || graph->selected()) {
//设置坐标轴、刻度尺同时选中
axis->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
//设置画布曲线选中
graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
selecteIndex = i;
break;
}
}
//存在选中则设置当前选中的可拖动和缩放
//不存在则设置所有的可拖动和缩放
QCPAxisRect *axisRect = customPlot->axisRect();
if (selecteIndex >= 0) {
QCPAxis *axis = axisRect->axis(type, selecteIndex);
axisRect->setRangeDragAxes(customPlot->xAxis, axis);
axisRect->setRangeZoomAxes(customPlot->xAxis, axis);
} else {
#ifndef qcustomplot_v1_3
axisRect->setRangeDragAxes(axisRect->axes());
axisRect->setRangeZoomAxes(axisRect->axes());
#endif
}
}
void frmMultiAxes::initPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count)
{
//选中对应的坐标轴或者画布等
connect(customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChangedByUser()));
//connect(customPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis, SLOT(setRange(QCPRange)));
if (type == QCPAxis::atLeft) {
customPlot->yAxis->setVisible(true);
customPlot->yAxis2->setVisible(false);
} else if (type == QCPAxis::atRight) {
customPlot->yAxis->setVisible(false);
customPlot->yAxis2->setVisible(true);
}
//添加多个坐标轴
for (int i = 0; i < count - 1; ++i) {
customPlot->axisRect()->addAxis(type);
}
//设置边距、范围值、网格可见、刻度数量等
for (int i = 0; i < count; ++i) {
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
axis->setPadding(10);
axis->setRange(0, 1);
axis->grid()->setVisible(true);
//axis->grid()->setSubGridVisible(true);
axis->ticker()->setTickCount(10);
}
//设置初始范围值
customPlot->xAxis->setRange(0.5, 10.5);
//设置刻度数量
customPlot->xAxis->ticker()->setTickCount(10);
//设置可拖动和缩放选中等
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectPlottables);
}
void frmMultiAxes::loadPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count)
{
//颜色集合
QList<QColor> colors;
colors << QColor(211, 78, 78) << QColor(29, 185, 242) << QColor(170, 162, 119) << QColor(255, 192, 1);
colors << QColor(0, 176, 180) << QColor(0, 113, 193) << QColor(255, 192, 0);
colors << QColor(72, 103, 149) << QColor(185, 87, 86) << QColor(0, 177, 125);
colors << QColor(214, 77, 84) << QColor(71, 164, 233) << QColor(34, 163, 169);
colors << QColor(59, 123, 156) << QColor(162, 121, 197) << QColor(72, 202, 245);
colors << QColor(0, 150, 121) << QColor(111, 9, 176) << QColor(250, 170, 20);
for (int i = 0; i < count; ++i) {
//分配一种颜色
QColor color;
if (i < colors.count()) {
color = colors.at(i);
}
QPen pen(color);
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
//刻度文本颜色
axis->setTickLabelColor(color);
//刻度线条颜色
axis->setBasePen(pen);
//大刻度尺颜色
axis->setTickPen(pen);
//小刻度尺颜色
axis->setSubTickPen(pen);
//画布线条颜色
QCPGraph *graph = customPlot->addGraph(customPlot->xAxis, axis);
graph->setPen(pen);
}
}
void frmMultiAxes::loadData(QCustomPlot *customPlot, int index)
{
//下面演示从文本文件读取数据,具体格式可以自行打开文件查看
//真实场景自己准备数据调用 setData 即可
int flag = index > 2 ? 0 : index;
QString fileName = QString(":/data/data%1.txt").arg(flag);
QFile file(fileName);
if (file.open(QFile::ReadOnly | QIODevice::Text)) {
QString value = file.readAll();
file.close();
QVector<double> keys, values;
QStringList list = value.split(" ");
int len = list.length();
//如果不是2的倍数则长度要减去1
if (len % 2 != 0) {
len = (len - 1);
}
for (int i = 0; i < len / 2; i++) {
keys << i;
}
//每两个16进制数字组合成一个数字
for (int i = 0; i < len; i = i + 2) {
QString strHex = QString("%1%2").arg(list.at(i)).arg(list.at(i + 1));
qint16 value = strHex.toUShort(NULL, 16);
values << value;
}
//获取对应画布
QCPGraph *graph = customPlot->graph(index);
//设置数据到画布
graph->setData(keys, values);
//自适应坐标轴
graph->rescaleAxes();
}
}

View File

@@ -1,36 +0,0 @@
#ifndef FRMMULTIAXES_H
#define FRMMULTIAXES_H
#include <QWidget>
#include "qcustomplot.h"
namespace Ui {
class frmMultiAxes;
}
class frmMultiAxes : public QWidget
{
Q_OBJECT
public:
explicit frmMultiAxes(QWidget *parent = 0);
~frmMultiAxes();
private:
Ui::frmMultiAxes *ui;
int count;
private slots:
//初始化界面数据
void initForm();
//用户按下区域改变
void selectionChangedByUser();
//初始化图表控件
void initPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count);
//加载图表控件
void loadPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count);
//加载数据到图表控件
void loadData(QCustomPlot *customPlot, int index);
};
#endif // FRMMULTIAXES_H

View File

@@ -1,35 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMultiAxes</class>
<widget class="QWidget" name="frmMultiAxes">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCustomPlot" name="customPlot1" native="true"/>
</item>
<item>
<widget class="QCustomPlot" name="customPlot2" native="true"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header location="global">qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>