技术专栏
Qt qcustomplot之 柱状图
首先定义需要的三个柱状图,峰电量,谷电量,平电量。
QCPBars * name;
highpower = new QCPBars(ui->qcustomplot->xAxis, ui->qcustomplot->yAxis);
lowpower = new QCPBars(ui->qcustomplot->xAxis, ui->qcustomplot->yAxis);
evenpower = new QCPBars(ui->qcustomplot->xAxis, ui->qcustomplot->yAxis);
因为这个需要全局调用,所以在头文件的类里进行定义。
然后对柱状图进行设置:
// add title layout element:
customPlot->plotLayout()->insertRow(0);
customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(customPlot, "峰谷平电量"));
// create empty bar chart objects:
customPlot->addPlottable(highpower);
customPlot->addPlottable(lowpower);
customPlot->addPlottable(evenpower);
// set names and colors:
QPen pen;
pen.setWidthF(1.2);
highpower->setName("峰电量"); //
pen.setColor(QColor(255, 0, 0));
highpower->setPen(pen);
highpower->setBrush(QColor(255, 0, 0,50)); // 最后一位时透明度
lowpower->setName("谷电量");
pen.setColor(QColor(1, 92, 191));
lowpower->setPen(pen);
lowpower->setBrush(QColor(1, 92, 191, 50));
evenpower->setName("平电量");
pen.setColor(QColor(150, 222, 0));
evenpower->setPen(pen);
evenpower->setBrush(QColor(150, 222, 0, 70));
// stack bars ontop of each other:
lowpower->moveAbove(highpower);
evenpower->moveAbove(lowpower);
在上面的代码里,设置了三个柱状图的累加的顺序。
下面就是设置XY轴的数据和显示的方式:
// prepare x axis with country labels:
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4<<5<<6<<7;
labels << "星期一"<< "星期二"<< "星期三" << "星期四"<< "星期五"<< "星期六" << "星期日" ;
customPlot->xAxis->setAutoTicks(false);
customPlot->xAxis->setAutoTickLabels(false);
customPlot->xAxis->setTickVector(ticks);
customPlot->xAxis->setTickVectorLabels(labels);
customPlot->xAxis->setTickLabelRotation(0);
customPlot->xAxis->setSubTickCount(0);
customPlot->xAxis->setTickLength(0, 4);
customPlot->xAxis->setPadding(10);
customPlot->xAxis->grid()->setVisible(true);
customPlot->xAxis->setRange(0, 8);
// prepare y axis:
QVector<double> y_ticks;
QVector<QString> y_labels;
y_ticks << 1 << 5<<10<<15<<20<<25<<30<<35<<40<<45<<50;
y_labels << "1" << "5" << "10" << "15"<< "20"<< "25"<< "30"<< "35"<< "40"<< "45";
customPlot->yAxis->setAutoTicks(false);
customPlot->yAxis->setAutoTickLabels(false);
customPlot->yAxis->setTickVector(y_ticks);
customPlot->yAxis->setTickVectorLabels(y_labels);
customPlot->yAxis->setAutoTickStep(false);
customPlot->yAxis->setSubTickCount(0); // 设置每隔的间距
customPlot->yAxis->setRange(0, 45);
customPlot->yAxis->setPadding(5); // a bit more space to the left border
customPlot->yAxis->setNumberFormat("g"); //eEfgG
customPlot->yAxis->setLabel("Kwh");
customPlot->yAxis->grid()->setSubGridVisible(true);
QPen gridPen;
gridPen.setStyle(Qt::SolidLine);
gridPen.setColor(QColor(0, 0, 0, 25));
customPlot->yAxis->grid()->setPen(gridPen);
gridPen.setStyle(Qt::DotLine);
customPlot->yAxis->grid()->setSubGridPen(gridPen);
最后就是数据的获取显示:
// Add data:
QVector<double> highData;
highData = getdata_sql();
highpower->setData(ticks, highData);
QVector<double> lowData;
lowData = getdata_sql();
lowpower->setData(ticks, lowData);
QVector<double> evenData;
evenData = getdata_sql();
evenpower->setData(ticks, evenData);
数据来源设置为随机数:
QVector<double> Class::getdata_sql()
{
QVector<double> Data;
for(int i = 0;i< 8;i++)
{
Data << rand()%15;
}
return Data;
}
其余的可以根据各自的需求去做更改。
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包
暂无数据
相关专栏
-
浏览量:5038次2020-05-08 15:46:11
-
浏览量:2830次2020-08-23 20:59:44
-
浏览量:2246次2020-05-19 16:57:59
-
浏览量:5223次2021-07-05 09:54:53
-
浏览量:7339次2020-08-23 21:25:35
-
浏览量:6338次2020-08-22 16:31:22
-
浏览量:5172次2017-11-16 11:35:44
-
浏览量:5557次2020-08-23 21:17:12
-
浏览量:11269次2021-05-11 19:30:21
-
浏览量:5316次2020-08-23 21:07:51
-
浏览量:2859次2020-08-21 19:39:43
-
浏览量:6635次2018-01-22 14:23:15
-
浏览量:9646次2020-08-19 22:41:20
-
2021-04-28 15:04:42
-
浏览量:4165次2021-03-31 22:12:12
-
浏览量:2918次2020-05-06 15:52:54
-
浏览量:2727次2022-05-26 16:24:43
-
浏览量:34187次2021-06-28 08:13:24
-
浏览量:9082次2020-05-07 14:42:07
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
big_anana
您的支持将鼓励我继续创作!
打赏金额:
¥1
¥5
¥10
¥50
¥100
支付方式:
微信支付
打赏成功!
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注