实现原理
实时设置QCPColorMap颜色图的大小setSize
以及映射到轴的范围setRange
,并且通过setCell
设置新的颜色值
void MainWindow::setupWaterfall()
{
int scanIndex = ui->horizontalSlider->value(); // 当前扫查位置
int lawId = ui->horizontalSlider_2->value();
int lawOffset = mFile.GetGroupLawDataOffset(0, lawId);
QCPAxis *yAxis = ui->customPlot->axisRect(1)->axis(QCPAxis::atLeft); // 左轴
// 当扫查位置超过左轴的范围时需要重新设置轴的范围
if (yAxis->range().upper < scanIndex)
yAxis->setRange(scanIndex, yAxis->range().size(), Qt::AlignRight);
else if (yAxis->range().lower > scanIndex)
yAxis->setRange(scanIndex, yAxis->range().size(), Qt::AlignLeft);
// 当扫查位置超过当前颜色图的大小时,需要重新设置颜色图的大小
if (scanIndex + 1 > mBottomColorMap->data()->valueSize()) {
mBottomColorMap->data()->setSize(pointQty, scanIndex+1);
mBottomColorMap->data()->setValueRange(QCPRange(0, scanIndex+1)); // 设置颜色图映射在value轴的范围
}
// 设置颜色图的值
for (int i = 0; i < pointQty; ++i) {
for (int j = 0; j < mBottomColorMap->data()->valueSize(); ++j) {
data = mFile.GetAllLawData(j, 0); // 提取数据
unsigned char z = (data + lawOffset)[i]; // 颜色取值位置
mBottomColorMap->data()->setCell(i, j, z);
}
}
ui->customPlot->replot();
}