Qt qcustomplot之 柱状图

big_anana 2020-05-07 19:43:16 2957

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

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个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
big_anana
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区