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; }

其余的可以根据各自的需求去做更改。

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:Qt qcustomplot之 柱状图 https://www.yhzz.com.cn/a/16024.html

上一篇 2023-05-13
下一篇 2023-05-13

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。