首页 > 技术知识 > 正文

首先定义需要的三个柱状图,峰电量,谷电量,平电量。

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

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

猜你喜欢