QCustomplot 坐标轴的设置
1.坐标轴类型设置
/**
* @brief YAxis::vueAxis
* 设置常数类型轴方法
*/
void YAxis::vueAxis()
{
QSharedPointer<QCPAxisTicker> logTicker(new QCPAxisTicker);
customPlot->yAxis->setTicker(logTicker);
customPlot->yAxis->setScaleType(QCPAxis::stLinear);
customPlot->yAxis->setNumberPrecision(0);
customPlot->yAxis->setNumberFormat("f");
}
/**
* @brief YAxis::logAxis
* 设置对数轴转换方法
*/
void YAxis::logAxis()
{
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
customPlot->yAxis->setTicker(logTicker);
customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->yAxis->setNumberPrecision(0);
customPlot->yAxis->setNumberFormat("eb");
}
因为我大多数都在用对数轴来处理数据,所以我在软件中初始化为对数轴。
2.坐标轴的范围设置
ymin = ui->lineEdit_4->text().toDouble();
ymax = ui->lineEdit_5->text().toDouble();
customPlot->yAxis->setRange(ymin,ymax);//y轴范围
customPlot->xAxis->setRange(ymin,ymax);//X轴范围
3.坐标轴标题设置
customPlot->yAxis->setLabel(ui->lineEdit_3->test());
4.坐标轴显隐
customPlot->yAxis->setTickLabels(true/false); //标签显隐
customPlot->yAxis->setTicks(true/false);//刻度显隐
5.坐标轴标签设置
/**
* @brief YAxis::on_btn_color_clicked
* Y轴标题颜色控制
*/
void YAxis::on_btn_color_clicked()
{
QColor initialColor = QColorDialog::getColor();//颜色对话框
customPlot->yAxis->setLabelColor(initialColor);
customPlot->replot();
}
/**
* @brief YAxis::on_btn_font_clicked
* Y轴标题字体控制
*/
void YAxis::on_btn_font_clicked()
{
bool ok = false;
QFont initialFont = customPlot->font();
QFont font = QFontDialog::getFont(&ok, initialFont);
QFont oldFont = customPlot->yAxis->labelFont();
if (ok)
customPlot->yAxis->setLabelFont(font);
customPlot->replot();
}
/**
* @brief YAxis::on_pushButton_4_clicked
* Y轴刻度颜色控制
*/
void YAxis::on_pushButton_4_clicked()
{
QColor initialColor = QColorDialog::getColor();
customPlot->yAxis->setTickLabelColor(initialColor);
customPlot->replot();
}
/**
* @brief YAxis::on_pushButton_3_clicked
* Y轴刻度字体控制
*/
void YAxis::on_pushButton_3_clicked()
{
bool ok = false;
QFont initialFont = customPlot->font();
QFont font = QFontDialog::getFont(&ok, initialFont);
QFont oldFont = customPlot->yAxis->tickLabelFont();
if (ok)
customPlot->yAxis->setTickLabelFont(font);
customPlot->replot();
}
/**
* @brief YAxis::on_comboBox_currentIndexChanged
* @param arg1
* 修改Y轴的刻度格式
*/
void YAxis::on_comboBox_currentIndexChanged(const QString &arg1)
{
customPlot->yAxis->setNumberFormat(arg1);
customPlot->replot();
}
6.曲线自动对齐
customPlot->rescaleAxes();//默认轴(下X和左Y)全局对齐
customPlot->yAxis->rescale(true);//左Y轴对齐
customPlot->yAxis2->rescale(true);//右Y轴对齐
customPlot->xAxis->rescale(true);//X轴对齐
7.修改背景
customPlot->axisRect()->setBackgroundScaled(true);//启用背景缩放
customPlot->axisRect()->setBackgroundScaledMode(Qt::AspectRatioMode::IgnoreAspectRatio);//自由缩放
customPlot->axisRect()->setBackground(QPixmap(":/Image/lye.png"));//背景图片
customPlot->replot();
总结
本章内容为坐标轴的基本设置,大家可根据自己的需求设计自己的坐标轴弹窗功能。
基础概念-Qt QCustomPlot 介绍
Qt-QCustomplot图像和QChart的区别
Qt-QCustomplot图像设计功能-游标