wumu
19 小时以前 99b38ba1c1a960d678775a32de3bbf236d8e78b7
analysisbyrediscache.cpp
@@ -26,6 +26,10 @@
    m_model = new QStandardItemModel(this);
    ui->tableView->setModel(m_model);
    QStringList labels;
    labels<<"股票名字"<<"市值"<<"收盘价"<<"涨跌幅"<<"排名"<<"交易时间"<<"排名变化";
    m_model->setHorizontalHeaderLabels(labels);
}
AnalysisByRedisCache::~AnalysisByRedisCache()
@@ -54,6 +58,39 @@
    m_result = result;
}
// 滑动窗口计算排名变化
void AnalysisByRedisCache::calcWindowRank(int start)
{
    // 滑动窗口排名,6个要2+的前100
    QVector<int> ranks;
    for(int i=start;i<m_model->rowCount();++i){
        ranks.append(m_model->item(i,4)->text().toInt());
        if(i >= 6+start){
            // 先统计排名计数
            int cntBig=0, cntSmall=0;
            for(int j=0;j<ranks.size();++j){
                if(ranks.at(j) > 100){
                    cntBig++;
                }else{
                    cntSmall++;
                }
            }
            // 统计对比
            if(cntSmall == 2 && ranks.back() <= 100){
                // 将当前6个元素定为符合的窗口,背景色设置为紫色
                for(int k=0;k<6;++k){
                    m_model->item(i-k,4)->setData(QColor("red"),Qt::BackgroundColorRole);
                }
            }
            // 清除窗口第一个元素
            ranks.pop_front();
        }
    }
}
void AnalysisByRedisCache::searchAndAnalysisData(QString name)
{
    QElapsedTimer timer;
@@ -71,7 +108,20 @@
            double market_capital = it["market_capital"].toDouble() / 100000000;
            items.append(new QStandardItem(QString::number(market_capital)));
            items.append(new QStandardItem(it["close"].toString()));
            items.append(new QStandardItem(it["percent"].toString()));
            QStandardItem *percentItem = new QStandardItem(it["percent"].toString());
            if(percentItem->text().toDouble() > 0){
                percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加装饰颜色为红色
                percentItem->setData(QColor("red"),Qt::TextColorRole); // 添加文本颜色为红色
                items.at(0)->setData(QColor("red"),Qt::TextColorRole); // 设置股票名字为红色
            }else if(percentItem->text().toDouble() < 0){
                percentItem->setData(QColor("green"),Qt::DecorationRole); // 添加装饰颜色为红色
                percentItem->setData(QColor("green"),Qt::TextColorRole); // 设置背景颜色为绿色
                items.at(0)->setData(QColor("green"),Qt::TextColorRole); // 设置股票名字为绿色
            }
            items.append(percentItem);
            //items.append(new QStandardItem(it["percent"].toString()));
            items.append(new QStandardItem(it["amount_rank"].toString()));
            items.append(new QStandardItem(it["time_trade"].toString()));
            QString amount_rank = it["amount_rank"].toString();
@@ -96,6 +146,8 @@
    }
    qDebug()<<"cnt="<<cnt;
    qDebug() << "Query executed in" << timer.elapsed() << "ms";
    qDebug()<<"滑动窗口排名-cnt:"<<m_model->rowCount()<<cnt<<"start:"<<m_model->rowCount() - cnt + 1;
    calcWindowRank(m_model->rowCount() - cnt + 1);
}
void AnalysisByRedisCache::on_pushButton_analysisByRedis_clicked()
{