大家好,欢迎来到IT知识分享网。
1.官网下载qcustomplot .cpp,qcustomplot.h文件
2.将上述两个文件放在自己的工作目录下
3.将上述两个文件引入CMakeLists.txt中如(qcustomplot .cpp,qcustomplot.h放置的位置和main.cpp在同一目录下):
set(SOURCE_FILES main.cpp qcustomplot.h qcustomplot.cpp ) 4.创建一个帮助类,其中包含以下方法
void CMainForm::setupBarChartDemo(QCustomPlot *customPlot) { //this->setFixedSize(1920,1080); std::string demoName = "Bar Chart Demo"; QLinearGradient gradient(0, 0, 0, 400); gradient.setColorAt(0, QColor(90, 90, 90)); gradient.setColorAt(0.38, QColor(105, 105, 105)); gradient.setColorAt(1, QColor(70, 70, 70)); customPlot->setBackground(QBrush(gradient)); // create empty bar chart objects: QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis); QCPBars *nuclear = new QCPBars(customPlot->xAxis, customPlot->yAxis); QCPBars *fossil = new QCPBars(customPlot->xAxis, customPlot->yAxis); regen->setAntialiased(false); // gives more crisp, pixel aligned bar borders nuclear->setAntialiased(false); fossil->setAntialiased(false); regen->setStackingGap(1); nuclear->setStackingGap(1); fossil->setStackingGap(1); fossil->setName("Fossil fuels"); fossil->setPen(QPen(QColor(111, 9, 176).lighter(170))); fossil->setBrush(QColor(111, 9, 176)); nuclear->setName("Nuclear"); nuclear->setPen(QPen(QColor(250, 170, 20).lighter(150))); nuclear->setBrush(QColor(250, 170, 20)); regen->setName("Regenerative"); regen->setPen(QPen(QColor(0, 168, 140).lighter(130))); regen->setBrush(QColor(0, 168, 140)); // stack bars on top of each other: nuclear->moveAbove(fossil); regen->moveAbove(nuclear); // prepare x axis with country labels: 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->setBasePen(QPen(Qt::white)); customPlot->xAxis->setTickPen(QPen(Qt::white)); customPlot->xAxis->grid()->setVisible(true); customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine)); customPlot->xAxis->setTickLabelColor(Qt::white); customPlot->xAxis->setLabelColor(Qt::white); // prepare y axis: customPlot->yAxis->setRange(0, 12.1);//set y customPlot->yAxis->setPadding(5); // a bit more space to the left border 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)); customPlot->yAxis->grid()->setSubGridVisible(true); customPlot->yAxis->setTickLabelColor(Qt::white); customPlot->yAxis->setLabelColor(Qt::white); customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine)); customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine)); // Add data: QVector<double> fossilData, nuclearData, regenData; fossilData << 0.86*10.5 << 0.83*5.5 << 0.84*5.5 << 0.52*5.8 << 0.89*5.2 << 0.90*4.2 << 0.67*11.2; nuclearData << 0.08*10.5 << 0.12*5.5 << 0.12*5.5 << 0.40*5.8 << 0.09*5.2 << 0.00*4.2 << 0.07*11.2; 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; fossil->setData(ticks, fossilData); nuclear->setData(ticks, nuclearData); regen->setData(ticks, regenData); // setup legend: customPlot->legend->setVisible(true); customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter); customPlot->legend->setBrush(QColor(255, 255, 255, 100)); customPlot->legend->setBorderPen(Qt::NoPen); QFont legendFont = font(); legendFont.setPointSize(10); customPlot->legend->setFont(legendFont); customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); } 5.在main.cpp中创建QCustomPlot类,然后调用帮助类中的以上函数(要设置QCustomPlot对象的宽和高)即可完成简单的柱状图
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22184.html