QT 基于qcustomplot实现热力图(一)-CSDN博客
QT 基于qcustomplot实现热力图(三)-CSDN博客
背景
在某些项目中使用到热力图,上一篇讲到了静态的而力图,这次讲讲解动态热力图,再上篇的代码中进行此修改;
优化
其他功能均不需要修改修改后的
mainwindow.cpp如下:
修改前:
for (int i = 0; i < data_.size(); ++i)
{
// 更新 ColorMap 数据
for (int j = 0; j < data_.at(i).size(); ++j)
{
m_colorMap_->data()->setCell(i, j, data_[i][j]);
}
}
timer_.stop();
修改后
#if static_Type
for (int i = 0; i < data_.size(); ++i)
{
// 更新 ColorMap 数据
for (int j = 0; j < data_.at(i).size(); ++j)
{
m_colorMap_->data()->setCell(i, j, data_[i][j]);
}
}
timer_.stop();
#else
for (int i = 0; i < data_.size(); ++i)
{
// 更新 ColorMap 数据
for (int j = 0; j < data_.at(i).size(); ++j)
{
m_colorMap_->data()->setCell(i, j, qrand() % 100);
}
}
#endif
完整mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
int numRows = 10;
int numCols = 10;
#define static_Type 0 //静态热力图
#define dynamic_Type 1 //动态热力图
#define real_update_Type 0 //实时刷新热力图
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//添加画布
ui->widget->addGraph();
m_colorMap_ = new QCPColorMap(ui->widget->xAxis, ui->widget->yAxis);
//添加一个色阶
m_colorScale_ = new QCPColorScale(ui->widget);
ui->widget->plotLayout()->addElement(0, 1, m_colorScale_); // add it to the right of the main axis rect
m_colorScale_->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is already the default)
m_colorMap_->setColorScale(m_colorScale_); // associate the color map with the color scale
//设置渐变色风格
m_colorMap_->setGradient(QCPColorGradient::gpJet);
//设置颜色空间的大小m*n矩形 下面需要注意行和列对应的x和y
m_colorMap_->data()->setSize(numCols, numRows);
m_colorMap_->data()->setRange(QCPRange(0, numCols-1), QCPRange(0, numRows-1));
// 设置颜色映射
QCPColorGradient gradient;
gradient.setColorInterpolation(QCPColorGradient::ciRGB);
gradient.setColorStopAt(0, Qt::blue);
gradient.setColorStopAt(0.5, Qt::green);
gradient.setColorStopAt(1, Qt::red);
m_colorMap_->setGradient(gradient);
//x周可自由变换
ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables);
#if !real_update_Type
//初始化数据
for(int i = 0 ; i < numCols; i++)
{
QVector<double>tmp;
for(int j = 0; j < numCols; j++)
{
tmp.append(/*i*10*/qrand() % 100);
}
data_.append(tmp);
}
//初始化x
for(int i = 0 ; i < numRows; i++)
{
ydata_.append(i);
}
//初始化y
for(int i = 0 ; i < numCols; i++)
{
xdata_.append(i);
}
#endif
timer_.start(1*100);
connect(&timer_, SIGNAL(timeout()), this, SLOT(timeoutPro()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timeoutPro()
{
#if static_Type
for (int i = 0; i < data_.size(); ++i)
{
// 更新 ColorMap 数据
for (int j = 0; j < data_.at(i).size(); ++j)
{
m_colorMap_->data()->setCell(i, j, data_[i][j]);
}
}
timer_.stop();
#else
for (int i = 0; i < data_.size(); ++i)
{
// 更新 ColorMap 数据
for (int j = 0; j < data_.at(i).size(); ++j)
{
m_colorMap_->data()->setCell(i, j, qrand() % 100);
}
}
#endif
m_colorMap_->rescaleDataRange();
m_colorMap_->rescaleAxes();
ui->widget->replot();
}