在.h文件中包含
#include “qcustomplot.h”
void SetGraph_Img(QCustomPlot *p_imag); //设置谱图图纸
QCPColorMap *m_pColorMap;//新建一个色图对象
在.cpp文件中如下所示,代码都在注释上,调用·传入一个绘图区域的指针即可
//用来设置方法后的lofar谱图
void TL_System::SetGraph_Img(QCustomPlot *p_imag)
{
// p_imag = ui->widget_Imag; //创建一个画图指针
p_imag->installEventFilter(this); //安装一个事件过滤器
p_imag->clearGraphs();//清楚之前设置
//m_fp5->showTracer(true);//实现数据跟踪(!!重要函数)
p_imag->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom| QCP::iSelectPlottables);//可拖拽+可滚轮缩放
// QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);//日期做X轴
// dateTicker->setDateTimeFormat("hh:mm:ss");//日期格式(可参考QDateTime::fromString()函数)
//fp3->xAxis->setTicker(dateTicker);//设置X轴为时间轴
//fp3->xAxis->setTicker();
p_imag->xAxis->setLabel("水平距离(Km)");//x轴显示文字
p_imag->yAxis->setLabel("深度(m)");//y轴显示文字
// //设置画布背景色
// QLinearGradient plotGradient;
// plotGradient.setStart(0, 0);
// plotGradient.setFinalStop(0, 350);
// plotGradient.setColorAt(0, QColor(80, 80, 80));
// plotGradient.setColorAt(1, QColor(50, 50, 50));
// p_imag->setBackground(plotGradient);
p_imag->setBackground(QColor(255,255,255,0));
p_imag->yAxis->setRangeReversed(true);//设置Y坐标轴反转了一下
p_imag->xAxis->setLabelColor(Qt::white);//文字颜色
p_imag->yAxis->setLabelColor(Qt::white);
p_imag->xAxis->setTickLabelColor(Qt::white);//坐标轴数字颜色
p_imag->yAxis->setTickLabelColor(Qt::white);
p_imag->xAxis->setBasePen(QPen(Qt::white, 1));//坐标轴颜色及宽度
p_imag->yAxis->setBasePen(QPen(Qt::white, 1));
p_imag->xAxis->setTickPen(QPen(Qt::white, 1));//主刻度
p_imag->yAxis->setTickPen(QPen(Qt::white, 1));
p_imag->xAxis->setSubTickPen(QPen(Qt::white, 1));//副刻度
p_imag ->yAxis->setSubTickPen(QPen(Qt::white, 1));
//每条曲线都会独占一个graph()
p_imag->addGraph();
p_imag->graph(0)->setPen(QPen(Qt::blue));//曲线颜色
p_imag->graph(0)->setBrush(QBrush(QColor(0,255,255,20)));//曲线与X轴包围区的颜色
//自动调整XY轴的范围,以便显示出graph(0)中所有的点
//给第一个graph设置rescaleAxes(),后续所有graph都设置rescaleAxes(true)即可实现显示所有曲线
/* fp3->graph(0)->rescaleAxes();*/
//通过传递的轴的QCustomPlot进行注册,简洁理解QCPColorMap的数据为(x轴,y轴;颜色,值value)
m_pColorMap = new QCPColorMap(p_imag->xAxis,p_imag->yAxis);
//m_pColorMap->data()->setSize(100,5000);//设置整个图(x,y)点数,X轴50个点,y轴4096个点
m_pColorMap->data()->setSize(5000,100);
m_pColorMap->data()->setRange(QCPRange(0,100),QCPRange(0,1200));//setRange是设置X轴以及Y轴的范围
// for(int x=0;x<=119;x++)
// {
// for(int y=0;y<8192;y++)
// {
// m_pColorMap->data()->setCell(x,y,qCos(x/10.0)+qSin(y/10.0));
// }
// }
QCPColorScale *colorScale = new QCPColorScale(p_imag); // 构造一个色条
colorScale->setType(QCPAxis::atRight ); // 水平显示atLeft,atRight,atTop,atBottom
p_imag->plotLayout()->addElement(0, 1, colorScale); // 在颜色图右边显示
colorScale->setDataRange(QCPRange(0,100)); //设置色条的数值最大最小
// colorScale->axis()->setBasePen(QPen(Qt::white, 1));
// colorScale->axis()->setTickPen(QPen(Qt::white, 1));
colorScale->axis()->setLabelColor(Qt::white);
colorScale->axis()->setTickLabelColor(Qt::white);
m_pColorMap->setColorScale(colorScale);
QCPColorGradient gradient; // 色条使用的颜色渐变
gradient.setColorStopAt(0.0, QColor("#f6efa6")); // 设置色条开始时的颜色
gradient.setColorStopAt(1.0, QColor("#bf444c")); // 设置色条结束时的颜色
m_pColorMap->setGradient(QCPColorGradient::gpJet);//设置默认渐进色变化(可在QCPColorGradient中查看)
m_pColorMap->rescaleDataRange(true);
//dataTimer =new QTimer;
//connect(dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
//dataTimer->start(0); // Interval 0 means to refresh as fast as possible
// 立即刷新图像
p_imag->rescaleAxes();//自适应大小
p_imag->replot();
}