CustomPie.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "CustomPie.h"
  2. #include <QPainter>
  3. CustomPie::CustomPie(QWidget *parent) : QWidget(parent) { }
  4. CustomPie::~CustomPie() { }
  5. void CustomPie::paintEvent(QPaintEvent *)
  6. {
  7. int width = this->width();
  8. int height = this->height();
  9. int side = qMin(width, height);
  10. // 绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
  11. QPainter painter(this);
  12. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  13. painter.translate(width / 2, height / 2);
  14. painter.scale(side / 200.0, side / 200.0);
  15. // 绘制饼图
  16. drawPie(&painter);
  17. }
  18. void CustomPie::drawPie(QPainter *painter)
  19. {
  20. painter->save();
  21. int radius = 93;
  22. QRect rect(-radius, -radius, radius * 2, radius * 2);
  23. double startAngle = 0;
  24. double sum = getSumValue();
  25. // 逐个取出值并绘制饼图区域和对应的文字
  26. int count = labels.count();
  27. for (int i = 0; i < count; ++i) {
  28. // 取出值并计算当前值占比面积
  29. double value = values.at(i);
  30. double arcLength = value / sum * 360;
  31. double percent = value / sum * 100;
  32. QRect pieRect = rect;
  33. // 如果当前区域展开则需要设置边框
  34. painter->setPen(Qt::NoPen);
  35. if (explodedIndex == i || explodedAll) {
  36. painter->setPen(borderColor);
  37. QPoint center = pieRect.center();
  38. int mid = startAngle + arcLength / 2;
  39. center += getOffsetPoint(mid);
  40. pieRect.moveCenter(center);
  41. }
  42. // 从颜色集合中取出颜色
  43. painter->setBrush(colors.at(i));
  44. painter->drawPie(pieRect, startAngle * 16, arcLength * 16);
  45. QString strValue = labels.at(i);
  46. if (showPercent && percent > 7) {
  47. strValue = QString("%1%2%3%")
  48. .arg(strValue)
  49. .arg(strValue.isEmpty() ? "" : "\n")
  50. .arg(QString::number(percent, 'f', 0));
  51. }
  52. int mid = startAngle + arcLength / 2;
  53. int offset = 60;
  54. if (percent >= 50) {
  55. offset = 45;
  56. } else if (percent >= 30) {
  57. offset = 55;
  58. } else if (percent >= 15) {
  59. offset = 60;
  60. }
  61. QPoint p = getOffsetPoint(mid, offset);
  62. QRect textRect;
  63. textRect.setX(p.x() - 40);
  64. textRect.setY(p.y() - 30);
  65. textRect.setWidth(80);
  66. textRect.setHeight(60);
  67. painter->setPen(Qt::black);
  68. // painter->drawRect(textRect);
  69. QFont font;
  70. font.setPixelSize(strValue.isEmpty() ? 20 : 17);
  71. painter->setFont(font);
  72. painter->setPen(textColor);
  73. painter->drawText(textRect, Qt::AlignCenter, strValue);
  74. startAngle += arcLength;
  75. }
  76. painter->restore();
  77. }
  78. double CustomPie::getSumValue()
  79. {
  80. return labels.size();
  81. }
  82. QPoint CustomPie::getOffsetPoint(double angel, int offset)
  83. {
  84. return QPoint {};
  85. }
  86. QColor CustomPie::getTextColor() const
  87. {
  88. return textColor;
  89. }
  90. QColor CustomPie::getBorderColor() const
  91. {
  92. return borderColor;
  93. }
  94. void CustomPie::setExplodedAll(bool explodedAll)
  95. {
  96. this->explodedAll = explodedAll;
  97. }
  98. void CustomPie::setExplodedIndex(int index)
  99. {
  100. this->explodedIndex = index;
  101. }
  102. void CustomPie::setDefaultColor(bool defaultColor)
  103. {
  104. if (defaultColor) { }
  105. }
  106. void CustomPie::setTextColor(const QColor &textColor)
  107. {
  108. this->textColor = textColor;
  109. }
  110. void CustomPie::setBorderColor(const QColor &borderColor)
  111. {
  112. this->borderColor = borderColor;
  113. }
  114. void CustomPie::setColors(const QList<QColor> &colors)
  115. {
  116. this->colors = colors;
  117. }
  118. void CustomPie::initPie() { }
  119. void CustomPie::appendPie(const QString &label, double value, const QString &tip)
  120. {
  121. labels.append(label);
  122. values.append(value);
  123. }
  124. void CustomPie::setDataPie() { }
  125. void CustomPie::loadPercent() { }
  126. void CustomPie::clearPie() { }
  127. void CustomPie::setHoleSize(double holeSize) { }