QCustomPlot 属性介绍

QCustomPlot 属性介绍

图表曲线作为一种直观分析的工具,在软件开发中应用的非常广泛,QCustomPlot是Qt的第三方类库,下面整理下QCustomPlot常用的属性。

日期作者版本
2021年7月5日Mister HV1.0


1. 创建配置绘图表

QCPGraph *graph1 = customPlot->addGraph();  //添加一个图表

2. 设置线的样式

graph1->setLineStyle(QCPGraph::lsNone);   //无线
graph1->setLineStyle(QCPGraph::lsLine);    //有线

3. 设置画布曲线的颜色、抗锯齿、名称

 QPen pen;
pen.setWidth(2);//曲线的粗细
pen.setColor(Qt::red);
graph1->setPen(pen);//曲线颜色设置
graph1->setAntialiasedFill(true);//设置抗锯齿
graph1->setName("名称");//设置画布曲线名称

4. 添加、清空、刷新数据

ui.customPlot->graph(0)->addData(key,value);//可以输入单点、也可以多点数据同时传入
ui.customPlot->graph(0)->data().data()->clear();//清空数据
ui.customPlot->replot();//刷新

5. 设置散点样式

graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,
                       QPen(Color, LineWidth),
                       QBrush(Color), DotWidth));//设置圆点的样式,还有其他样式选择,如正方形QCPScatterStyle::ssSquare。

6. 设置 y 轴属性(x轴同理)

ui->customPlot->xAxis->setLabel("x");  //x轴标签
ui->customPlot->yAxis->setLabel("y");   //y轴标签
ui->customPlot->yAxis->setLabelColor(Color);//设置y轴标签颜色
ui->customPlot->yAxis->setLabelFont("微软雅黑");  //y轴标题 字体
ui->customPlot->yAxis->setVisible(true);   //隐藏y轴
ui->customPlot->yAxis->setRange(0, 2);    //设置y轴坐标范围
ui->customPlot->yAxis->setAutoTickCount(8);//设置y轴刻度线个数
ui->customPlot->yAxis->->setTickLength(5, 0);//设置x轴刻度线的长度
ui->customPlot->yAxis->setSubTickLength(2, 0);//设置x轴子刻度线长度
ui->customPlot->yAxis->setTickPen(QPen(Color,Width));//设置y轴刻度颜色和大小
ui->customPlot->yAxis->setSubTickPen(QPen(Color, Width));//设置y轴子刻度线的颜色和大小
ui->customPlot->xAxis->setAutoTicks(true);//设置x轴自动刻度线

7. 设置背景颜色、设置矩形轴框内的背景颜色

ui->customPlot->setBackground(Color);//设置背景颜色
ui->customPlot->axisRect()->setBackground(Color);//设置矩形轴框内的背景颜色

8. 设置刻度标签类型(默认是QCPAxisTickerFixed坐标)

1. QCPAxisTickerDateTime 日期时间坐标轴
customPlot->setInteraction(QCP::iRangeDrag, true);
customPlot->setInteraction(QCP::iRangeZoom, true);//设置可拖动
QDateTime dateTime = QDateTime::currentDateTime();
double  now = dateTime.toTime_t();//当前时间转化为秒
//生成时间刻度对象
QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);
customPlot->xAxis->setTicker(dateTimeTicker);
//dateTimeTicker->setDateTimeSpec(Qt::UTC);//设施世界时间,即不加上时区的时间
dateTimeTicker->setTickCount(12);//设置大刻度
dateTimeTicker->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount);//可读性
customPlot->xAxis->setSubTicks(false);//隐藏子刻度
customPlot->xAxis->setRange(now, now+3600*24);//x轴范围,从当前时间起往后推24小时

在这里插入图片描述

2. QCPAxisTickerText 文本刻度标签
//画柱状图
QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
regen->setAntialiased(false); // 提供更多的清晰度,像素对齐的条形边框 抗锯齿
regen->setStackingGap(1);
regen->setName("Regenerative");
regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
regen->setBrush(QColor(0, 168, 140));
    
// regen->moveAbove(nuclear);
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
customPlot->xAxis->setTicker(textTicker);
customPlot->xAxis->setTickLabelRotation(60);
customPlot->xAxis->setSubTicks(false);
customPlot->xAxis->setTickLength(0, 4);
customPlot->xAxis->setRange(0, 8);
customPlot->xAxis->setTickPen(QPen(Qt::white));
 
// 准备y轴:
customPlot->yAxis->setRange(0, 12.1);
customPlot->yAxis->setPadding(5); // 左边框多留一点空间
customPlot->yAxis->setLabel("Power Consumption in\nKilowatts per Capita (2007)");
//customPlot->yAxis->setBasePen(QPen(Qt::white));
customPlot->yAxis->setTickPen(QPen(Qt::white));
customPlot->yAxis->setSubTickPen(QPen(Qt::white));
// 添加数据:
QVector<double>  regenData;
regenData   << 0.06*10.5 << 0.05*5.5 << 0.04*5.5 << 0.06*5.8 << 0.02*5.2 << 0.07*4.2 << 0.25*11.2;
regen->setData(ticks, regenData);

在这里插入图片描述

3. QCPAxisTickerLog log对数刻度标签
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
customPlot->xAxis->setTicker(logTicker);
customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->xAxis->setRange(0,250);
logTicker->setLogBase(10);
QVector<double> x2(250), y2(250);
for (int i=0; i<250; ++i)
{
    x2[i] = i;
    y2[i] = log(i);
}
customPlot->addGraph(customPlot->xAxis, customPlot->yAxis);
customPlot->graph(0)->setData(x2, y2);

在这里插入图片描述

QCustomPlot 刻度标签
QCustomPlot 图形

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值