更新代码

master
feiyangqingyun 2022-05-18 17:38:40 +08:00
parent b750d63c59
commit 1564758973
36 changed files with 145 additions and 405 deletions

View File

@ -214,7 +214,6 @@
![](third/0snap/qcustomplotdemo5.jpg)
![](third/0snap/qcustomplotdemo6.jpg)
![](third/0snap/qcustomplotdemo7.jpg)
![](third/0snap/qcustomplotdemo8.jpg)
### 3.8 网友提供-netfriend
- 动态StackWidget-astackwidget

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

View File

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

View File

@ -30,8 +30,6 @@
#include "frmaxistag.h"
#include "frminteraction.h"
#include "frmscrollbar.h"
#include "frmmultiaxes.h"
#include "frmmultiplot.h"
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
@ -54,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");
}
@ -68,27 +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);
ui->stackedWidget->addWidget(new frmMultiPlot);
}
void frmMain::initNav()
@ -99,8 +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,187 +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>

View File

@ -1,18 +0,0 @@
#include "frmmultiplot.h"
#include "ui_frmmultiplot.h"
frmMultiPlot::frmMultiPlot(QWidget *parent) : QWidget(parent), ui(new Ui::frmMultiPlot)
{
ui->setupUi(this);
this->initForm();
}
frmMultiPlot::~frmMultiPlot()
{
delete ui;
}
void frmMultiPlot::initForm()
{
}

View File

@ -1,26 +0,0 @@
#ifndef FRMMULTIPLOT_H
#define FRMMULTIPLOT_H
#include <QWidget>
namespace Ui {
class frmMultiPlot;
}
class frmMultiPlot : public QWidget
{
Q_OBJECT
public:
explicit frmMultiPlot(QWidget *parent = 0);
~frmMultiPlot();
private:
Ui::frmMultiPlot *ui;
private slots:
//初始化界面数据
void initForm();
};
#endif // FRMMULTIPLOT_H

View File

@ -1,32 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMultiPlot</class>
<widget class="QWidget" name="frmMultiPlot">
<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="customPlot" 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>

View File

@ -15,17 +15,19 @@ frmAdvancedAxes::~frmAdvancedAxes()
void frmAdvancedAxes::initForm()
{
#ifndef qcustomplot_v1_3
// configure axis rect:
ui->customPlot->plotLayout()->clear(); // clear default axis rect so we can start from scratch
QCPAxisRect *wideAxisRect = new QCPAxisRect(ui->customPlot);
wideAxisRect->setupFullAxesBox(true);
wideAxisRect->axis(QCPAxis::atRight, 0)->setTickLabels(true);
wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor(QColor("#6050F8")); // add an extra axis on the left and color its numbers
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
ui->customPlot->plotLayout()->addElement(0, 0, wideAxisRect); // insert axis rect in first row
ui->customPlot->plotLayout()->addElement(1, 0, subLayout); // sub layout in second row (grid layout will grow accordingly)
//ui->customPlot->plotLayout()->setRowStretchFactor(1, 2);
// prepare axis rects that will be placed in the sublayout:
QCPAxisRect *subRectLeft = new QCPAxisRect(ui->customPlot, false); // false means to not setup default axes
QCPAxisRect *subRectRight = new QCPAxisRect(ui->customPlot, false);
@ -33,6 +35,7 @@ void frmAdvancedAxes::initForm()
subLayout->addElement(0, 1, subRectRight);
subRectRight->setMaximumSize(100, 100); // make bottom right axis rect size fixed 100x100
subRectRight->setMinimumSize(100, 100); // make bottom right axis rect size fixed 100x100
// setup axes in sub layout axis rects:
subRectLeft->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);
subRectRight->addAxes(QCPAxis::atBottom | QCPAxis::atRight);
@ -40,11 +43,13 @@ void frmAdvancedAxes::initForm()
subRectRight->axis(QCPAxis::atRight)->ticker()->setTickCount(2);
subRectRight->axis(QCPAxis::atBottom)->ticker()->setTickCount(2);
subRectLeft->axis(QCPAxis::atBottom)->grid()->setVisible(true);
// synchronize the left and right margins of the top and bottom axis rects:
QCPMarginGroup *marginGroup = new QCPMarginGroup(ui->customPlot);
subRectLeft->setMarginGroup(QCP::msLeft, marginGroup);
subRectRight->setMarginGroup(QCP::msRight, marginGroup);
wideAxisRect->setMarginGroup(QCP::msLeft | QCP::msRight, marginGroup);
// move newly created axes on "axes" layer and grids on "grid" layer:
foreach (QCPAxisRect *rect, ui->customPlot->axisRects()) {
foreach (QCPAxis *axis, rect->axes()) {
@ -79,6 +84,7 @@ void frmAdvancedAxes::initForm()
mainGraphCos->rescaleKeyAxis();
mainGraphCos->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black), QBrush(Qt::white), 6));
mainGraphCos->setPen(QPen(QColor(120, 120, 120), 2));
QCPGraph *mainGraphGauss = ui->customPlot->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft, 1));
mainGraphGauss->data()->set(dataGauss);
mainGraphGauss->setPen(QPen(QColor("#8070B8"), 2));
@ -103,12 +109,11 @@ void frmAdvancedAxes::initForm()
subBars->setBrush(QColor("#705BE8"));
subBars->keyAxis()->setSubTicks(false);
subBars->rescaleAxes();
// setup a ticker for subBars key axis that only gives integer ticks:
QSharedPointer<QCPAxisTickerFixed> intTicker(new QCPAxisTickerFixed);
intTicker->setTickStep(1.0);
intTicker->setScaleStrategy(QCPAxisTickerFixed::ssMultiples);
subBars->keyAxis()->setTicker(intTicker);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@ -15,7 +15,6 @@ frmBarChart::~frmBarChart()
void frmBarChart::initForm()
{
#ifndef qcustomplot_v1_3
// set dark background gradient:
QLinearGradient gradient(0, 0, 0, 400);
gradient.setColorAt(0, QColor(90, 90, 90));
@ -27,12 +26,14 @@ void frmBarChart::initForm()
QCPBars *regen = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPBars *nuclear = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPBars *fossil = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
regen->setAntialiased(false); // gives more crisp, pixel aligned bar borders
nuclear->setAntialiased(false);
fossil->setAntialiased(false);
regen->setStackingGap(1);
nuclear->setStackingGap(1);
fossil->setStackingGap(1);
// set names and colors:
fossil->setName("Fossil fuels");
fossil->setPen(QPen(QColor(111, 9, 176).lighter(170)));
@ -43,6 +44,7 @@ void frmBarChart::initForm()
regen->setName("Regenerative");
regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
regen->setBrush(QColor(0, 168, 140));
// stack bars on top of each other:
nuclear->moveAbove(fossil);
regen->moveAbove(nuclear);
@ -52,6 +54,7 @@ void frmBarChart::initForm()
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
ui->customPlot->xAxis->setTicker(textTicker);
@ -97,5 +100,4 @@ void frmBarChart::initForm()
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@ -27,6 +27,7 @@ void frmColorMap::initForm()
int ny = 200;
colorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4)); // and span the coordinate range -4..4 in both key (x) and value (y) dimensions
// now we assign some data, by accessing the QCPColorMapData instance of the color map:
double x, y, z;
for (int xIndex = 0; xIndex < nx; ++xIndex) {

View File

@ -15,9 +15,9 @@ frmDate::~frmDate()
void frmDate::initForm()
{
#ifndef qcustomplot_v1_3
// set locale to english, so we get english month names:
ui->customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// seconds of current time, we'll use it as starting point in time for data:
double now = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0;
srand(8); // set the random seed, so we always get the same random data
@ -40,21 +40,26 @@ void frmDate::initForm()
}
ui->customPlot->graph()->data()->set(timeData);
}
// configure bottom axis to show date instead of number:
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("d. MMMM\nyyyy");
ui->customPlot->xAxis->setTicker(dateTicker);
// configure left axis text labels:
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTick(10, "a bit\nlow");
textTicker->addTick(50, "quite\nhigh");
ui->customPlot->yAxis->setTicker(textTicker);
// set a more compact font size for bottom and left axis tick labels:
ui->customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
ui->customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
// set axis labels:
ui->customPlot->xAxis->setLabel("Date");
ui->customPlot->yAxis->setLabel("Random wobbly lines value");
// make top and right axes visible but without ticks and labels:
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
@ -62,12 +67,13 @@ void frmDate::initForm()
ui->customPlot->yAxis2->setTicks(false);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setTickLabels(false);
// set axis ranges to show all data:
ui->customPlot->xAxis->setRange(now, now + 24 * 3600 * 249);
ui->customPlot->yAxis->setRange(0, 60);
// show legend with slightly transparent background brush:
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setBrush(QColor(255, 255, 255, 150));
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@ -60,10 +60,12 @@ void frmFinancial::initForm()
volumeAxisRect->setMaximumSize(QSize(QWIDGETSIZE_MAX, 100));
volumeAxisRect->axis(QCPAxis::atBottom)->setLayer("axes");
volumeAxisRect->axis(QCPAxis::atBottom)->grid()->setLayer("grid");
// bring bottom and main axis rect closer together:
ui->customPlot->plotLayout()->setRowSpacing(0);
volumeAxisRect->setAutoMargins(QCP::msLeft | QCP::msRight | QCP::msBottom);
volumeAxisRect->setMargins(QMargins(0, 0, 0, 0));
// create two bar plottables, for positive (green) and negative (red) volume bars:
ui->customPlot->setAutoAddPlottableToLegend(false);
QCPBars *volumePos = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));
@ -72,6 +74,7 @@ void frmFinancial::initForm()
int v = std::rand() % 20000 + std::rand() % 20000 + std::rand() % 20000 - 10000 * 3;
(v < 0 ? volumeNeg : volumePos)->addData(startTime + 3600 * 5.0 * i, qAbs(v)); // add data to either volumeNeg or volumePos, depending on sign of v
}
volumePos->setWidth(3600 * 4);
volumePos->setPen(Qt::NoPen);
volumePos->setBrush(QColor(100, 180, 110));
@ -82,6 +85,7 @@ void frmFinancial::initForm()
// interconnect x axis ranges of main and bottom axis rects:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), volumeAxisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
connect(volumeAxisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis, SLOT(setRange(QCPRange)));
// configure axes of both main and bottom axis rect:
QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);
dateTimeTicker->setDateTimeSpec(Qt::UTC);
@ -100,6 +104,5 @@ void frmFinancial::initForm()
QCPMarginGroup *group = new QCPMarginGroup(ui->customPlot);
ui->customPlot->axisRect()->setMarginGroup(QCP::msLeft | QCP::msRight, group);
volumeAxisRect->setMarginGroup(QCP::msLeft | QCP::msRight, group);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

View File

@ -15,7 +15,7 @@ frmItem::~frmItem()
void frmItem::showEvent(QShowEvent *)
{
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
dataTimer.start(0);
}
void frmItem::hideEvent(QHideEvent *)
@ -35,6 +35,7 @@ void frmItem::initForm()
x[i] = i / (double)(n - 1) * 34 - 17;
y[i] = qExp(-x[i] * x[i] / 20.0) * qSin(k * x[i] + phase);
}
graph->setData(x, y);
graph->setPen(QPen(Qt::blue));
graph->rescaleKeyAxis();
@ -150,10 +151,9 @@ void frmItem::bracketDataSlot()
x[i] = i / (double)(n - 1) * 34 - 17;
y[i] = qExp(-x[i] * x[i] / 20.0) * qSin(k * x[i] + phase);
}
ui->customPlot->graph()->setData(x, y);
phaseTracer->setGraphKey((8 * M_PI + fmod(M_PI * 1.5 - phase, 6 * M_PI)) / k);
ui->customPlot->replot();
// calculate frames per second:

View File

@ -17,9 +17,11 @@ void frmLineStyle::initForm()
{
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setFont(QFont("Helvetica", 9));
QPen pen;
QStringList lineNames;
lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";
// add graphs with different line styles:
for (int i = QCPGraph::lsNone; i <= QCPGraph::lsImpulse; ++i) {
ui->customPlot->addGraph();
@ -28,23 +30,28 @@ void frmLineStyle::initForm()
ui->customPlot->graph()->setName(lineNames.at(i - QCPGraph::lsNone));
ui->customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);
ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
// generate data:
QVector<double> x(15), y(15);
for (int j = 0; j < 15; ++j) {
x[j] = j / 15.0 * 5 * 3.14 + 0.01;
y[j] = 7 * qSin(x[j]) / x[j] - (i - QCPGraph::lsNone) * 5 + (QCPGraph::lsImpulse) * 5 + 2;
}
ui->customPlot->graph()->setData(x, y);
ui->customPlot->graph()->rescaleAxes(true);
}
// zoom out a bit:
ui->customPlot->yAxis->scaleRange(1.1, ui->customPlot->yAxis->range().center());
ui->customPlot->xAxis->scaleRange(1.1, ui->customPlot->xAxis->range().center());
// set blank axis lines:
ui->customPlot->xAxis->setTicks(false);
ui->customPlot->yAxis->setTicks(true);
ui->customPlot->xAxis->setTickLabels(false);
ui->customPlot->yAxis->setTickLabels(true);
// make top right axes clones of bottom left axes:
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

View File

@ -17,6 +17,7 @@ void frmLogarithmic::initForm()
{
ui->customPlot->setNoAntialiasingOnDrag(true); // more performance/responsiveness during dragging
ui->customPlot->addGraph();
QPen pen;
pen.setColor(QColor(255, 170, 100));
pen.setWidth(2);
@ -54,6 +55,7 @@ void frmLogarithmic::initForm()
dataPlusSinExp[i].key = i / 10.0;
dataPlusSinExp[i].value = qSin(dataPlusSinExp[i].key) * qExp(dataPlusSinExp[i].key);
}
for (int i = 0; i < dataFactorialCount; ++i) {
dataFactorial[i].key = i;
dataFactorial[i].value = 1.0;
@ -61,6 +63,7 @@ void frmLogarithmic::initForm()
dataFactorial[i].value *= k; // factorial
}
}
ui->customPlot->graph(0)->data()->set(dataLinear);
ui->customPlot->graph(1)->data()->set(dataMinusSinExp);
ui->customPlot->graph(2)->data()->set(dataPlusSinExp);
@ -70,6 +73,7 @@ void frmLogarithmic::initForm()
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
ui->customPlot->yAxis2->setScaleType(QCPAxis::stLogarithmic);
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
ui->customPlot->yAxis->setTicker(logTicker);
ui->customPlot->yAxis2->setTicker(logTicker);
@ -87,7 +91,5 @@ void frmLogarithmic::initForm()
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setBrush(QBrush(QColor(255, 255, 255, 150)));
ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop); // make legend align in top left corner or axis rect
// make range draggable and zoomable:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

View File

@ -41,6 +41,7 @@ void frmMultiAxis::initForm()
ui->customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);
ui->customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));
ui->customPlot->graph(1)->setName("Bottom maxwell function");
QCPErrorBars *errorBars = new QCPErrorBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
errorBars->removeFromLegend();
errorBars->setDataPlottable(ui->customPlot->graph(1));
@ -75,15 +76,18 @@ void frmMultiAxis::initForm()
QVector<double> x2(250), y2(250);
QVector<double> x3(250), y3(250);
QVector<double> x4(250), y4(250);
for (int i = 0; i < 25; ++i) { // data for graph 0
x0[i] = 3 * i / 25.0;
y0[i] = qExp(-x0[i] * x0[i] * 0.8) * (x0[i] * x0[i] + x0[i]);
}
for (int i = 0; i < 15; ++i) { // data for graph 1
x1[i] = 3 * i / 15.0;;
y1[i] = qExp(-x1[i] * x1[i]) * (x1[i] * x1[i]) * 2.6;
y1err[i] = y1[i] * 0.25;
}
for (int i = 0; i < 250; ++i) { // data for graphs 2, 3 and 4
x2[i] = i / 250.0 * 3 * M_PI;
x3[i] = x2[i];
@ -100,19 +104,23 @@ void frmMultiAxis::initForm()
ui->customPlot->graph(2)->setData(x2, y2);
ui->customPlot->graph(3)->setData(x3, y3);
ui->customPlot->graph(4)->setData(x4, y4);
// activate top and right axes, which are invisible by default:
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
// set ranges appropriate to show data:
ui->customPlot->xAxis->setRange(0, 2.7);
ui->customPlot->yAxis->setRange(0, 2.6);
ui->customPlot->xAxis2->setRange(0, 3.0 * M_PI);
ui->customPlot->yAxis2->setRange(-70, 35);
// set pi ticks on top axis:
ui->customPlot->xAxis2->setTicker(QSharedPointer<QCPAxisTickerPi>(new QCPAxisTickerPi));
// add title layout element:
ui->customPlot->plotLayout()->insertRow(0);
ui->customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(ui->customPlot, "Way too many graphs in one plot", QFont("sans", 12, QFont::Bold)));
// set labels:
ui->customPlot->xAxis->setLabel("Bottom axis with outward ticks");
ui->customPlot->yAxis->setLabel("Left axis label");

View File

@ -19,6 +19,7 @@ void frmParametricCurve::initForm()
QCPCurve *fermatSpiral1 = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPCurve *fermatSpiral2 = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPCurve *deltoidRadial = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
// generate the curve data points:
const int pointCount = 500;
QVector<QCPCurveData> dataSpiral1(pointCount), dataSpiral2(pointCount), dataDeltoid(pointCount);
@ -29,21 +30,25 @@ void frmParametricCurve::initForm()
dataSpiral2[i] = QCPCurveData(i, -dataSpiral1[i].key, -dataSpiral1[i].value);
dataDeltoid[i] = QCPCurveData(i, 2 * qCos(2 * theta) + qCos(1 * theta) + 2 * qSin(theta), 2 * qSin(2 * theta) - qSin(1 * theta));
}
// pass the data to the curves; we know t (i in loop above) is ascending, so set alreadySorted=true (saves an extra internal sort):
fermatSpiral1->data()->set(dataSpiral1, true);
fermatSpiral2->data()->set(dataSpiral2, true);
deltoidRadial->data()->set(dataDeltoid, true);
// color the curves:
fermatSpiral1->setPen(QPen(Qt::blue));
fermatSpiral1->setBrush(QBrush(QColor(0, 0, 255, 20)));
fermatSpiral2->setPen(QPen(QColor(255, 120, 0)));
fermatSpiral2->setBrush(QBrush(QColor(255, 120, 0, 30)));
QRadialGradient radialGrad(QPointF(310, 180), 200);
radialGrad.setColorAt(0, QColor(170, 20, 240, 100));
radialGrad.setColorAt(0.5, QColor(20, 10, 255, 40));
radialGrad.setColorAt(1, QColor(120, 20, 240, 10));
deltoidRadial->setPen(QPen(QColor(170, 20, 240)));
deltoidRadial->setBrush(QBrush(radialGrad));
// set some basic ui->customPlot config:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setupFullAxesBox();

View File

@ -28,6 +28,7 @@ void frmPolarPlot::initForm()
ticker->setTickCount(8);
polarAxis->setTicker(ticker);
*/
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
angularAxis->setRangeDrag(false);
angularAxis->setTickLabelMode(QCPPolarAxisAngular::lmUpright);

View File

@ -21,6 +21,7 @@ void frmQuadratic::initForm()
x[i] = i / 50.0 - 1; // x goes from -1 to 1
y[i] = x[i] * x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);

View File

@ -16,7 +16,7 @@ frmRealtimeData::~frmRealtimeData()
void frmRealtimeData::showEvent(QShowEvent *)
{
timeStart = QTime::currentTime();
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
dataTimer.start(0);
}
void frmRealtimeData::hideEvent(QHideEvent *)
@ -35,6 +35,7 @@ void frmRealtimeData::initForm()
ui->customPlot->yAxis->setTickLabelFont(font);
ui->customPlot->legend->setFont(font);
*/
ui->customPlot->addGraph(); // blue line
ui->customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
ui->customPlot->addGraph(); // red line
@ -51,7 +52,7 @@ void frmRealtimeData::initForm()
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
}
void frmRealtimeData::realtimeDataSlot()
@ -68,6 +69,7 @@ void frmRealtimeData::realtimeDataSlot()
//ui->customPlot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
ui->customPlot->replot();

View File

@ -18,6 +18,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->axisRect()->setBackground(QPixmap(":/image/bg1.jpg"));
ui->customPlot->addGraph();
ui->customPlot->graph()->setLineStyle(QCPGraph::lsLine);
QPen pen;
pen.setColor(QColor(255, 200, 20, 200));
pen.setStyle(Qt::DashLine);
@ -27,6 +28,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(QPixmap(":/image/data.png")));
// set graph name, will show up in legend next to icon:
ui->customPlot->graph()->setName("Data from Photovoltaic\nenergy barometer 2011");
// set data:
QVector<double> year, value;
year << 2005 << 2006 << 2007 << 2008 << 2009 << 2010 << 2011;
@ -49,6 +51,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->yAxis2->setSubTicks(false);
ui->customPlot->xAxis->setRange(2004.5, 2011.5);
ui->customPlot->yAxis->setRange(0, 52);
// setup legend:
ui->customPlot->legend->setFont(QFont(font().family(), 7));
ui->customPlot->legend->setIconSize(50, 20);

View File

@ -70,6 +70,7 @@ void frmScatterStyle::initForm()
ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
}
}
// set blank axis lines:
ui->customPlot->rescaleAxes();
ui->customPlot->xAxis->setTicks(false);

View File

@ -21,6 +21,7 @@ void frmSimple::initForm()
ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
ui->customPlot->addGraph();
ui->customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(251), y0(251), y1(251);
for (int i = 0; i < 251; ++i) {
@ -28,15 +29,18 @@ void frmSimple::initForm()
y0[i] = qExp(-i / 150.0) * qCos(i / 10.0); // exponentially decaying cosine
y1[i] = qExp(-i / 150.0); // exponential envelope
}
// configure right and top axis to show ticks but no labels:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// pass data points to graphs:
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);

View File

@ -21,6 +21,7 @@ void frmSincScatter::initForm()
ui->customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// add confidence band graphs:
ui->customPlot->addGraph();
QPen pen;
pen.setStyle(Qt::DotLine);
pen.setWidth(1);
@ -32,6 +33,7 @@ void frmSincScatter::initForm()
ui->customPlot->legend->removeItem(ui->customPlot->legend->itemCount() - 1); // don't show two confidence band graphs in legend
ui->customPlot->graph(1)->setPen(pen);
ui->customPlot->graph(0)->setChannelFillGraph(ui->customPlot->graph(1));
// add theory curve graph:
ui->customPlot->addGraph();
pen.setStyle(Qt::DashLine);
@ -39,12 +41,14 @@ void frmSincScatter::initForm()
pen.setColor(Qt::red);
ui->customPlot->graph(2)->setPen(pen);
ui->customPlot->graph(2)->setName("Theory Curve");
// add data point graph:
ui->customPlot->addGraph();
ui->customPlot->graph(3)->setPen(QPen(Qt::blue));
ui->customPlot->graph(3)->setName("Measurement");
ui->customPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
ui->customPlot->graph(3)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, 4));
// add error bars:
QCPErrorBars *errorBars = new QCPErrorBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
errorBars->removeFromLegend();
@ -62,6 +66,7 @@ void frmSincScatter::initForm()
yConfLower[i] = y0[i] - 0.15;
x0[i] *= 1000;
}
QVector<double> x1(50), y1(50), y1err(50);
for (int i = 0; i < 50; ++i) {
// generate a gaussian distributed random number:
@ -74,6 +79,7 @@ void frmSincScatter::initForm()
x1[i] *= 1000;
y1err[i] = 0.15;
}
// pass data to graphs and let Qui->customPlot determine the axes ranges so the whole thing is visible:
ui->customPlot->graph(0)->setData(x0, yConfUpper);
ui->customPlot->graph(1)->setData(x0, yConfLower);
@ -82,12 +88,14 @@ void frmSincScatter::initForm()
errorBars->setData(y1err);
ui->customPlot->graph(2)->rescaleAxes();
ui->customPlot->graph(3)->rescaleAxes(true);
// setup look of bottom tick labels:
ui->customPlot->xAxis->setTickLabelRotation(30);
ui->customPlot->xAxis->ticker()->setTickCount(9);
ui->customPlot->xAxis->setNumberFormat("ebc");
ui->customPlot->xAxis->setNumberPrecision(1);
ui->customPlot->xAxis->moveRange(-10);
// make top right axes clones of bottom left axes. Looks prettier:
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

View File

@ -20,18 +20,22 @@ void frmStyled::initForm()
QVector<double> x2(100), y2(100);
QVector<double> x3(20), y3(20);
QVector<double> x4(20), y4(20);
for (int i = 0; i < x1.size(); ++i) {
x1[i] = i / (double)(x1.size() - 1) * 10;
y1[i] = qCos(x1[i] * 0.8 + qSin(x1[i] * 0.16 + 1.0)) * qSin(x1[i] * 0.54) + 1.4;
}
for (int i = 0; i < x2.size(); ++i) {
x2[i] = i / (double)(x2.size() - 1) * 10;
y2[i] = qCos(x2[i] * 0.85 + qSin(x2[i] * 0.165 + 1.1)) * qSin(x2[i] * 0.50) + 1.7;
}
for (int i = 0; i < x3.size(); ++i) {
x3[i] = i / (double)(x3.size() - 1) * 10;
y3[i] = 0.05 + 3 * (0.5 + qCos(x3[i] * x3[i] * 0.2 + 2) * 0.5) / (double)(x3[i] + 0.7) + std::rand() / (double)RAND_MAX * 0.01;
}
for (int i = 0; i < x4.size(); ++i) {
x4[i] = x3[i];
y4[i] = (0.5 - y3[i]) + ((x4[i] - 2) * (x4[i] - 2) * 0.02);

View File

@ -13,21 +13,29 @@ frmAxisTag::~frmAxisTag()
delete ui;
}
void frmAxisTag::showEvent(QShowEvent *)
{
dataTimer.start(50);
}
void frmAxisTag::hideEvent(QHideEvent *)
{
dataTimer.stop();
}
void frmAxisTag::initForm()
{
mPlot = ui->customPlot;
// configure plot to have two right axes:
mPlot->yAxis->setTickLabels(false);
connect(mPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), mPlot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
mPlot->yAxis2->setVisible(true);
mPlot->axisRect()->addAxis(QCPAxis::atRight);
mPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(30); // add some padding to have space for tags
mPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30); // add some padding to have space for tags
ui->customPlot->yAxis->setTickLabels(false);
connect(ui->customPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->axisRect()->addAxis(QCPAxis::atRight);
ui->customPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(30); // add some padding to have space for tags
ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30); // add some padding to have space for tags
// create graphs:
mGraph1 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph2 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 1));
mGraph1 = ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph2 = ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1));
mGraph1->setPen(QPen(QColor(250, 120, 0)));
mGraph2->setPen(QPen(QColor(0, 180, 60)));
@ -37,8 +45,7 @@ void frmAxisTag::initForm()
mTag2 = new AxisTag(mGraph2->valueAxis());
mTag2->setPen(mGraph2->pen());
connect(&mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
mDataTimer.start(40);
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}
void frmAxisTag::timerSlot()
@ -50,10 +57,10 @@ void frmAxisTag::timerSlot()
mGraph2->addData(count2, qCos(count2 / 50.0) + qSin(count2 / 50.0 / 0.4364) * 0.15);
// make key axis range scroll with the data:
mPlot->xAxis->rescale();
ui->customPlot->xAxis->rescale();
mGraph1->rescaleValueAxis(false, true);
mGraph2->rescaleValueAxis(false, true);
mPlot->xAxis->setRange(mPlot->xAxis->range().upper, 100, Qt::AlignRight);
ui->customPlot->xAxis->setRange(ui->customPlot->xAxis->range().upper, 100, Qt::AlignRight);
// update the vertical axis tag positions and texts to match the rightmost data point of the graphs:
double graph1Value = mGraph1->dataMainValue(mGraph1->dataCount() - 1);
@ -63,5 +70,5 @@ void frmAxisTag::timerSlot()
mTag1->setText(QString::number(graph1Value, 'f', 2));
mTag2->setText(QString::number(graph2Value, 'f', 2));
mPlot->replot();
ui->customPlot->replot();
}

View File

@ -17,14 +17,17 @@ public:
explicit frmAxisTag(QWidget *parent = 0);
~frmAxisTag();
protected:
void showEvent(QShowEvent *);
void hideEvent(QHideEvent *);
private:
Ui::frmAxisTag *ui;
QCustomPlot *mPlot;
QPointer<QCPGraph> mGraph1;
QPointer<QCPGraph> mGraph2;
AxisTag *mTag1;
AxisTag *mTag2;
QTimer mDataTimer;
QTimer dataTimer;
private slots:
void initForm();

View File

@ -16,21 +16,23 @@ frmScrollBar::~frmScrollBar()
void frmScrollBar::initForm()
{
// The following plot setup is mostly taken from the plot demos:
ui->plot->addGraph();
ui->plot->graph()->setPen(QPen(Qt::blue));
ui->plot->graph()->setBrush(QBrush(QColor(0, 0, 255, 20)));
ui->plot->addGraph();
ui->plot->graph()->setPen(QPen(Qt::red));
ui->customPlot->addGraph();
ui->customPlot->graph()->setPen(QPen(Qt::blue));
ui->customPlot->graph()->setBrush(QBrush(QColor(0, 0, 255, 20)));
ui->customPlot->addGraph();
ui->customPlot->graph()->setPen(QPen(Qt::red));
QVector<double> x(500), y0(500), y1(500);
for (int i = 0; i < 500; ++i) {
x[i] = (i / 499.0 - 0.5) * 10;
y0[i] = qExp(-x[i] * x[i] * 0.25) * qSin(x[i] * 5) * 5;
y1[i] = qExp(-x[i] * x[i] * 0.25) * 5;
}
ui->plot->graph(0)->setData(x, y0);
ui->plot->graph(1)->setData(x, y1);
ui->plot->axisRect()->setupFullAxesBox(true);
ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);
ui->customPlot->axisRect()->setupFullAxesBox(true);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
// configure scroll bars:
// Since scroll bars only support integer values, we'll set a high default range of -500..500 and
@ -43,27 +45,27 @@ void frmScrollBar::initForm()
// create connection between axes and scroll bars:
connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horzScrollBarChanged(int)));
connect(ui->verticalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(vertScrollBarChanged(int)));
connect(ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
connect(ui->plot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
// initialize axis range (and scroll bar positions via signals we just connected):
ui->plot->xAxis->setRange(0, 6, Qt::AlignCenter);
ui->plot->yAxis->setRange(0, 10, Qt::AlignCenter);
ui->customPlot->xAxis->setRange(0, 6, Qt::AlignCenter);
ui->customPlot->yAxis->setRange(0, 10, Qt::AlignCenter);
}
void frmScrollBar::horzScrollBarChanged(int value)
{
if (qAbs(ui->plot->xAxis->range().center() - value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->plot->xAxis->setRange(value / 100.0, ui->plot->xAxis->range().size(), Qt::AlignCenter);
ui->plot->replot();
if (qAbs(ui->customPlot->xAxis->range().center() - value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->customPlot->xAxis->setRange(value / 100.0, ui->customPlot->xAxis->range().size(), Qt::AlignCenter);
ui->customPlot->replot();
}
}
void frmScrollBar::vertScrollBarChanged(int value)
{
if (qAbs(ui->plot->yAxis->range().center() + value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->plot->yAxis->setRange(-value / 100.0, ui->plot->yAxis->range().size(), Qt::AlignCenter);
ui->plot->replot();
if (qAbs(ui->customPlot->yAxis->range().center() + value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->customPlot->yAxis->setRange(-value / 100.0, ui->customPlot->yAxis->range().size(), Qt::AlignCenter);
ui->customPlot->replot();
}
}

View File

@ -15,7 +15,7 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QCustomPlot" name="plot" native="true">
<widget class="QCustomPlot" name="customPlot" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
@ -44,7 +44,7 @@
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header>qcustomplot.h</header>
<header location="global">qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>

View File

@ -83,7 +83,4 @@
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/qcustomplotdemo/snap/23.jpg)
### 2.24 滚动条曲线
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/qcustomplotdemo/snap/24.jpg)
### 2.25 多坐标轴图
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/qcustomplotdemo/snap/25.jpg)
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/qcustomplotdemo/snap/24.jpg)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.