• 561阅读
  • 2回复

利用QCustomPlot绘制饼图 [复制链接]

上一主题 下一主题
离线maxlogo
 

只看楼主 倒序阅读 楼主  发表于: 04-22
— 本帖被 20091001753 从 Qt 作品展 移动到本区(2020-04-22) —





第一列为 南丁格尔玫瑰图
第二列为 普通的饼图和圆环饼图
第三列为 嵌套饼图

  1. void MainWindow::setupPieDemo(QCustomPlot *customPlot)
  2. {
  3.     QCPPie *pie1 = new QCPPie(customPlot);
  4.     QVector<QString> labels = {
  5.         "rose1","rose2","rose3","rose4","rose5","rose6","rose7","rose8",
  6.     };
  7.     QVector<double> values1 = {
  8.         10, 5, 15, 25, 20, 35, 30, 40
  9.     };
  10.     pie1->add(labels, values1);
  11.     pie1->setHoleSize(0.3);
  12.     pie1->setRadius(0.4);
  13.     pie1->setCenter(0.15, 0.25);
  14.     pie1->setLabelVisible(false);
  15.     pie1->setRoseType(QCPPie::rtRadius);
  16.     connect(pie1, &QCPPie::hoveredSlice, [](QCPPieSlice *slice, bool hovered){
  17.         slice->setLabelVisible(hovered);
  18.     });
  19.     QCPPie *pie2 = new QCPPie(customPlot);
  20.     pie2->add(labels, values1);
  21.     pie2->setHoleSize(0.5);
  22.     pie2->setRadius(0.4);
  23.     pie2->setCenter(0.15, 0.75);
  24.     pie2->setRoseType(QCPPie::rtArea);
  25.     QCPPie *pie3 = new QCPPie(customPlot);
  26.     QVector<QString> labels3 = {
  27.         "直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"
  28.     };
  29.     QVector<double> values3 = {
  30.         335, 310, 234, 135, 1548
  31.     };
  32.     pie3->add(labels3, values3);
  33.     pie3->setRadius(0.4);
  34.     pie3->setCenter(0.4, 0.25);
  35.     pie3->setTextFormat("{b}:{d}%");
  36.     QCPPie *pie4 = new QCPPie(customPlot);
  37.     pie4->add(labels3, values3);
  38.     pie4->setRadius(0.4);
  39.     pie4->setCenter(0.4, 0.75);
  40.     pie4->setHoleSize(0.7);
  41.     pie4->setLabelVisible(false);
  42.     pie4->setLabelPosition(QCPPieSlice::lpCenter);
  43.     pie4->setLabelFont(QFont("sans-serif", 30, QFont::Bold));
  44.     connect(pie4, &QCPPie::hoveredSlice, [](QCPPieSlice *slice, bool hovered){
  45.         slice->setLabelVisible(hovered);
  46.     });
  47.     QCPPie *pie5 = new QCPPie(customPlot);
  48.     QVector<QString> labels5 = {
  49.         "直达", "营销广告", "搜索引擎"
  50.     };
  51.     QVector<double> values5 = {
  52.         335, 679, 1548
  53.     };
  54.     pie5->add(labels5, values5);
  55.     pie5->setRadius(0.3);
  56.     pie5->setLabelPosition(QCPPieSlice::lpInside);
  57.     pie5->setLabelColor(Qt::white);
  58.     pie5->setSelectedMode(QCPPie::smSingle);
  59.     pie5->setCenter(0.75, 0.5);
  60.     QCPPie *pie6 = new QCPPie(customPlot);
  61.     QVector<QString> labels6 = {
  62.         "直达", "邮件营销", "联盟广告", "视频广告", "百度", "谷歌", "必应", "其他"
  63.     };
  64.     QVector<double> values6 = {
  65.         335, 310, 234, 135, 1048, 251, 147, 102
  66.     };
  67.     pie6->add(labels6, values6);
  68.     pie6->setName("访问来源");
  69.     pie6->setRadius(0.55);
  70.     pie6->setHoleSize(0.75);
  71.     pie6->setCenter(0.75, 0.5);
  72.     pie6->setTextFormat("{a}\n{b}:{c} {d}%");
  73. }

8条评分好评度+2贡献值+2金钱+20威望+2
巫添良 好评度 +1 - 06-11
巫添良 贡献值 +1 - 06-11
巫添良 威望 +1 - 06-11
巫添良 金钱 +10 - 06-11
20091001753 好评度 +1 - 04-22
20091001753 贡献值 +1 - 04-22
20091001753 威望 +1 - 04-22
20091001753 金钱 +10 - 04-22
个人博客:
简书:https://www.jianshu.com/u/14fa805306bd
CSDN:https://blog.csdn.net/qq10097355
思否:https://segmentfault.com/u/lowbees/articles
离线maxlogo

只看该作者 1楼 发表于: 04-22
以后会在个人博客中讲解
个人博客:
简书:https://www.jianshu.com/u/14fa805306bd
CSDN:https://blog.csdn.net/qq10097355
思否:https://segmentfault.com/u/lowbees/articles
离线clickto

只看该作者 2楼 发表于: 04-22
快速回复
限100 字节
 
上一个 下一个