QCustomPlot 属性介绍
图表曲线作为一种直观分析的工具,在软件开发中应用的非常广泛,QCustomPlot是Qt的第三方类库,下面整理下QCustomPlot常用的属性。
日期 | 作者 | 版本 |
---|---|---|
2021年7月5日 | Mister H | V1.0 |
文章目录
- QCustomPlot 属性介绍
- 1. 创建配置绘图表
- 2. 设置线的样式
- 3. 设置画布曲线的颜色、抗锯齿、名称
- 4. 添加、清空、刷新数据
- 5. 设置散点样式
- 6. 设置 y 轴属性(x轴同理)
- 7. 设置背景颜色、设置矩形轴框内的背景颜色
- 8. 设置刻度标签类型(默认是QCPAxisTickerFixed坐标)
- 1. QCPAxisTickerDateTime 日期时间坐标轴
- 2. QCPAxisTickerText 文本刻度标签
- 3. QCPAxisTickerLog log对数刻度标签
1. 创建配置绘图表
复制代码
1
2
3QCPGraph *graph1 = customPlot->addGraph(); //添加一个图表
2. 设置线的样式
复制代码
1
2
3
4
5graph1->setLineStyle(QCPGraph::lsNone); //无线 graph1->setLineStyle(QCPGraph::lsLine); //有线
3. 设置画布曲线的颜色、抗锯齿、名称
复制代码
1
2
3
4
5
6
7QPen pen; pen.setWidth(2);//曲线的粗细 pen.setColor(Qt::red); graph1->setPen(pen);//曲线颜色设置 graph1->setAntialiasedFill(true);//设置抗锯齿 graph1->setName("名称");//设置画布曲线名称
4. 添加、清空、刷新数据
复制代码
1
2
3
4ui.customPlot->graph(0)->addData(key,value);//可以输入单点、也可以多点数据同时传入 ui.customPlot->graph(0)->data().data()->clear();//清空数据 ui.customPlot->replot();//刷新
5. 设置散点样式
复制代码
1
2
3
4graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Color, LineWidth), QBrush(Color), DotWidth));//设置圆点的样式,还有其他样式选择,如正方形QCPScatterStyle::ssSquare。
6. 设置 y 轴属性(x轴同理)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18ui->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. 设置背景颜色、设置矩形轴框内的背景颜色
复制代码
1
2
3ui->customPlot->setBackground(Color);//设置背景颜色 ui->customPlot->axisRect()->setBackground(Color);//设置矩形轴框内的背景颜色
8. 设置刻度标签类型(默认是QCPAxisTickerFixed坐标)
1. QCPAxisTickerDateTime 日期时间坐标轴
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14customPlot->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 文本刻度标签
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34//画柱状图 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 innKilowatts 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对数刻度标签
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14QSharedPointer<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 图形
最后
以上就是俊秀手链最近收集整理的关于QCustomPlot 属性介绍QCustomPlot 属性介绍的全部内容,更多相关QCustomPlot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复