日志
QCustomPlot封装的股票折线图
2017-02-14 12:46
简介:QCustomplot 版本2.0,Qt5.5.1(不特定版本)。
功能介绍: 1.根据自己的思路简单封装了一下时间轴。 2.鼠标移动显示当前时间点对应的股票点数值。 3.静态数据模拟显示折线。 敬请大家指点,有更好的意见与建议留言。 源码如下: 【1】pro文件,支持QCustomplot修改: greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport 【2】股票窗口控件 1.头文件 stockwidget.h #ifndef STOCKWIDGET_H #define STOCKWIDGET_H #include <QWidget> #include <QTimer> namespace Ui { class StockWidget; } class StockWidget : public QWidget { Q_OBJECT public: explicit StockWidget(QWidget *parent = 0); ~StockWidget(); // 设置股票plot void setupStockPlot(); private slots: //模拟实时数据 void on_pBtn_start_clicked(); //模拟静态数据 void on_pBtn_addData_clicked(); //鼠标移动 void slot_mouseMove(QMouseEvent* event); private: Ui::StockWidget *ui; }; #endif // STOCKWIDGET_H 2.源文件stockwidget.cpp #include "stockwidget.h" #include "ui_stockwidget.h" StockWidget::StockWidget(QWidget *parent) : QWidget(parent), ui(new Ui::StockWidget) { ui->setupUi(this); setupStockPlot(); } StockWidget::~StockWidget() { delete ui; } void StockWidget::setupStockPlot() { connect(ui->stock_plot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(slot_mouseMove(QMouseEvent*))); //设置y轴 QSharedPointer<QCPAxisTickerFixed> fixedTicker(new QCPAxisTickerFixed); fixedTicker->setTickCount(3); fixedTicker->setTickStepStrategy(QCPAxisTicker::tssReadability); fixedTicker->setScaleStrategy(QCPAxisTickerFixed::ssNone); ui->stock_plot->yAxis->setRange(3270.5, 3280.5); ui->stock_plot->yAxis->setTicker(fixedTicker); ui->stock_plot->yAxis->setAntialiased(true); //设置x轴 QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText); textTicker->setSubTickCount(0); textTicker->setTickCount(3); textTicker->addTick(5, QObject::tr("09:30")); //刻度定位5便于刻度标签显示出来。 textTicker->addTick(120, QObject::tr("11:30")); textTicker->addTick(235, QObject::tr("15:00")); //刻度定位235便于刻度标签显示出来。 ui->stock_plot->xAxis->setTicker(textTicker); ui->stock_plot->xAxis->setLabel(QObject::tr("Time")); ui->stock_plot->xAxis->setTickLabelColor(QColor(Qt::red)); ui->stock_plot->xAxis->setRange(QCPRange(0, 240)); ui->stock_plot->xAxis->grid()->setVisible(false); ui->stock_plot->xAxis->setTickLengthIn(0); ui->stock_plot->xAxis->setTickPen(QPen(Qt::NoPen)); ui->stock_plot->xAxis->setAntialiased(true); //设置数据折线 ui->stock_plot->addGraph(); ui->stock_plot->graph()->setName(QObject::tr("上证指数")); ui->stock_plot->graph()->setPen(QPen(Qt::red,2)); ui->stock_plot->graph()->setAntialiased(true); ui->stock_plot->graph()->setAdaptiveSampling(true); ui->stock_plot->graph()->setLineStyle(QCPGraph::lsLine); ui->stock_plot->graph()->setScatterStyle(QCPScatterStyle::ssNone); on_pBtn_addData_clicked(); } void StockWidget::on_pBtn_start_clicked() { } void StockWidget::on_pBtn_addData_clicked() { QVector<double> value(240); QVector<double> key(240); int iCount = 240; for(int i = 0; i < iCount; ++i){ value = double(qrand()%100)/10.0 + 3270.0; key = i; } ui->stock_plot->graph()->setData(key, value); ui->stock_plot->replot(); } void StockWidget::slot_mouseMove(QMouseEvent *event) { if(ui->stock_plot->graph()->dataCount() == 0){ return; } double dKey; double dValue; ui->stock_plot->graph()->pixelsToCoords(event->pos(), dKey, dValue); QString strdTime; QString strValue; if(dKey >= 0 && dKey <= 240){ QDate curDate = QDate::currentDate(); if(dKey <= 120){ QDateTime curDayTime(curDate, QTime(9, 30, 0)); strdTime = curDayTime.addSecs(dKey*60).toString("yyyy.MM.dd, hh:mm"); strValue = QObject::tr("%1").arg(ui->stock_plot->graph()->dataMainValue(dKey)); }else{ //处理此处时注意!11:30之后的数据,索引从1开始的,所以此处-1 QDateTime curDayTime(curDate, QTime(13, 0, 0)); strdTime = curDayTime.addSecs((dKey-120)*60).toString("yyyy.MM.dd, hh:mm"); strValue = QObject::tr("%1").arg(ui->stock_plot->graph()->dataMainValue(dKey-1)); } }else{ ui->pLb_Msg->setText(QObject::tr("上证指数:")); return; } QString showMsg = QObject::tr("上证指数:") + QObject::tr(" 时间:%1").arg(strdTime) + QObject::tr(" 数据:%1").arg(strValue); ui->pLb_Msg->setText(showMsg); } 3.ui文件(图片没有搞进来,简单介绍) ui->stock_plot :拖一个Widget提升为QCustomPlot即可。 ui->pLb_Msg :显示指数标签。 ui->pBtn_addData:添加数据的按钮。 |
下一篇: 无
上一篇: QWidget的键盘事件焦点