CustomPieChart.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "CustomPieChart.h"
  2. CustomPieChart::CustomPieChart(QWidget *parent) : QWidget(parent)
  3. {
  4. title = "默认标题"; // 标题名字
  5. total = 0;
  6. initPieChartWidget();
  7. }
  8. CustomPieChart::CustomPieChart(const QString &title, const QString &tag, const int &data, const QColor &color,
  9. QWidget *parent)
  10. : QWidget(parent)
  11. {
  12. this->title = title; // 标题名字
  13. total = 1; // 数据长度
  14. sum = data; // 数据总量
  15. addSlice(tag, data, color);
  16. initPieChartWidget();
  17. }
  18. CustomPieChart::CustomPieChart(const QString &title, QStringList tagList, QList<int> dataList, QList<QColor> colorList,
  19. QWidget *parent)
  20. : QWidget(parent)
  21. {
  22. this->title = title; // 标题名字
  23. setSeries(tagList, dataList, colorList);
  24. initPieChartWidget();
  25. }
  26. CustomPieChart::~CustomPieChart() { }
  27. /* 过滤绘制事件 */
  28. bool CustomPieChart::eventFilter(QObject *widget, QEvent *event)
  29. {
  30. if (widget == pieChartWidget && event->type() == QEvent::Paint && total != 0) {
  31. drawPieChart(); // 绘制饼图部件
  32. } else if (widget == titleWidget && event->type() == QEvent::Paint) {
  33. QPainter painter(titleWidget);
  34. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); // 绘图和绘图文字抗锯齿
  35. painter.save();
  36. /* 绘制标题部件 */
  37. painter.setPen(Qt::white);
  38. titleFont.setPointSizeF(!isSetTitleFont ? titleWidget->height() * 0.75 : titleFont.pointSizeF());
  39. titleFont.setWeight(!isSetTitleFont ? QFont::Bold : titleFont.weight());
  40. painter.setFont(titleFont);
  41. painter.drawText(QRectF(0, 0, width(), height()), title);
  42. painter.restore();
  43. }
  44. return QWidget::eventFilter(widget, event);
  45. }
  46. /* 绘制饼图 */
  47. void CustomPieChart::drawPieChart()
  48. {
  49. int width = this->width();
  50. int height = this->height();
  51. double min = qMin(width, height); // 宽和高中最小的值
  52. double diameter = min * 5 / 9; // 直径
  53. double radius = diameter / 2; // 半径
  54. int startLength = 0; // 起始长度
  55. int midPoint = 0; // 坐标原点
  56. double startAngle = 0; // 起始角度
  57. QPainter painter(pieChartWidget);
  58. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); // 绘图和绘图文字抗锯齿
  59. painter.translate(width / 2, height / 2); // 偏移起始点
  60. painter.save();
  61. fontSize = globalFont.pointSize();
  62. /* 绘制总数 */
  63. sumFont.setPointSizeF(!isSetSumFont ? radius / 2.5 : sumFont.pointSizeF());
  64. painter.setFont(sumFont);
  65. painter.setPen(QColor(54, 235, 171));
  66. painter.drawText(QRectF(-radius, -radius - (radius / 4), diameter, diameter), Qt::AlignCenter,
  67. QString::number(sum));
  68. /* 绘制"总数"文本 */
  69. painter.setPen(Qt::white);
  70. sumTextFont.setPointSizeF(!isSetSumTextFont ? radius / 5 : sumTextFont.pointSizeF());
  71. painter.setFont(sumTextFont);
  72. painter.drawText(QRectF(-radius, -radius + fontSize, diameter, diameter), Qt::AlignCenter, "总数");
  73. painter.setFont(globalFont);
  74. for (int count = 0; count < total; count++) {
  75. startLength += (count > 0) ? int(360 * 16 * dataList[count - 1] / sum) : 0;
  76. int arcLength = int(360 * 16 * dataList[count] / sum); // 弧长
  77. double angle = 360 * dataList[count] / sum; // 扇形的弧度
  78. startAngle += (count > 0) ? 360 * dataList[count - 1] / sum : 0;
  79. double radian = angle / 2 + startAngle; // 当前弧度(所在扇形的二等分角度 + 所在扇形的起始角度)
  80. double offset = fontSize; // 偏移量
  81. double offsetX = 0; // x轴偏移量
  82. double offsetY = 0; // y轴偏移量
  83. /* 绘制扇形区域 */
  84. painter.setPen(Qt::NoPen);
  85. QRadialGradient radialGradient(midPoint, midPoint, radius);
  86. radialGradient.setColorAt(0, Qt::transparent);
  87. radialGradient.setColorAt(ringSize, Qt::transparent);
  88. radialGradient.setColorAt((ringSize + 0.01 > 1) ? 1 : ringSize + 0.01,
  89. qFuzzyCompare(ringSize, 1)
  90. ? Qt::transparent
  91. : colorList[count]); // 从颜色列表中取出颜色并设为区域背景色
  92. radialGradient.setColorAt(1, qFuzzyCompare(ringSize, 1) ? Qt::transparent : colorList[count]);
  93. painter.setBrush(radialGradient);
  94. painter.drawPie(QRectF(-radius, -radius, diameter, diameter), startLength, arcLength); // 绘制饼图的扇形区域
  95. /* 绘制说明 */
  96. painter.setPen(colorList[count]);
  97. painter.setBrush(colorList[count]);
  98. legendFont.setPointSizeF(!isSetLegendFont ? radius / 5 : legendFont.pointSizeF());
  99. painter.setFont(legendFont);
  100. fontSize = legendFont.pointSize();
  101. painter.drawRect(QRectF(-(radius * 2.1 + fontSize * 1.2), -(radius / 2.1) + count * (fontSize * 1.3),
  102. fontSize / 2, fontSize / 2));
  103. painter.drawText(
  104. QRectF(-(radius * 2.1), -(radius / 2.1 + fontSize / 3) + count * (fontSize * 1.3), radius, radius),
  105. tagList[count]);
  106. /* 绘制标签折线 */
  107. tagFont.setPointSizeF(!isSetTagFont ? radius / 5 : tagFont.pointSizeF());
  108. painter.setFont(tagFont);
  109. fontSize = tagFont.pointSize();
  110. QPointF point = QPointF(midPoint + radius * cos(radian * M_PI / 180),
  111. midPoint - radius * sin(radian * M_PI / 180)); // 弧度在坐标轴的象限点
  112. QPolygonF polygon; // 多段线
  113. polygon << point;
  114. QString tagText = QString::number(dataList[count] / sum * 100, 'f', 0) + "%";
  115. double textWidth = tagText.size() / 2 * fontSize;
  116. if (dataList[count] != 0) { // 数据不为零才可以绘制折线和标签
  117. if (radian > 0 && radian <= 90) { // 第一象限
  118. offsetX += offset;
  119. offsetY -= offset;
  120. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  121. offsetX += textWidth;
  122. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  123. offsetX -= textWidth;
  124. offsetY -= (fontSize * 1.5);
  125. } else if (radian > 90 && radian <= 180) { // 第二象限
  126. offsetX -= offset;
  127. offsetY -= offset;
  128. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  129. offsetX -= textWidth;
  130. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  131. offsetY -= (fontSize * 1.5);
  132. } else if (radian > 180 && radian <= 270) { // 第三象限
  133. offsetX -= offset;
  134. offsetY += offset;
  135. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  136. offsetX -= textWidth;
  137. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  138. } else if (radian > 270 && radian <= 360) { // 第四象限
  139. offsetX += offset;
  140. offsetY += offset;
  141. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  142. offsetX += textWidth;
  143. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  144. offsetX -= textWidth;
  145. }
  146. painter.drawPolyline(polygon);
  147. /* 绘制标签 */
  148. painter.drawText(QRectF(point.x() + offsetX, point.y() + offsetY, diameter, diameter),
  149. QStringLiteral("%1").arg(tagText));
  150. }
  151. }
  152. painter.restore();
  153. }
  154. /* 初始化饼图部件 */
  155. void CustomPieChart::initPieChartWidget()
  156. {
  157. isSetTitleFont = false;
  158. isSetTagFont = false;
  159. isSetLegendFont = false;
  160. isSetSumFont = false;
  161. isSetSumTextFont = false;
  162. setRingSize(0.6);
  163. /* 标题部件 */
  164. titleWidget = new QWidget(this);
  165. titleWidget->installEventFilter(this);
  166. /* 饼图部件 */
  167. pieChartWidget = new QWidget(this);
  168. pieChartWidget->installEventFilter(this);
  169. QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
  170. vBoxLayout->setSpacing(0);
  171. vBoxLayout->setMargin(0);
  172. vBoxLayout->addWidget(titleWidget, 1);
  173. vBoxLayout->addWidget(pieChartWidget, 9);
  174. setLayout(vBoxLayout);
  175. }
  176. /* 增加切片 */
  177. void CustomPieChart::addSlice(const QString &tag, const int &data, const QColor &color)
  178. {
  179. tagList << tag; // 标签名列表
  180. dataList << data; // 数据列表
  181. colorList << color; // 颜色列表
  182. total = dataList.size(); // 数据表长度
  183. sum += (total == 1) ? 0 : dataList[total - 1]; // 数据总量
  184. }
  185. /* 设置系列 */
  186. void CustomPieChart::setSeries(QStringList tagList, QList<int> dataList, QList<QColor> colorList)
  187. {
  188. total = dataList.size(); // 数据表长度
  189. sum = 0; // 数据总量
  190. this->tagList = tagList; // 标签名列表
  191. this->dataList = dataList; // 数据列表
  192. this->colorList = colorList; // 颜色列表
  193. for (int count = 0; count < total; count++) {
  194. sum += dataList[count];
  195. }
  196. }
  197. /* 设置全局字体 */
  198. void CustomPieChart::setGlobalFont(const QFont &font)
  199. {
  200. globalFont = font;
  201. titleFont = font;
  202. tagFont = font;
  203. legendFont = font;
  204. sumFont = font;
  205. sumTextFont = font;
  206. sumFont = font;
  207. isSetTitleFont = true;
  208. isSetTagFont = true;
  209. isSetLegendFont = true;
  210. isSetSumFont = true;
  211. isSetSumTextFont = true;
  212. }
  213. /* 设置标题字体 */
  214. void CustomPieChart::setTitleFont(const QFont &font)
  215. {
  216. titleFont = font;
  217. isSetTitleFont = true;
  218. }
  219. /* 设置标签字体 */
  220. void CustomPieChart::setTagFont(const QFont &font)
  221. {
  222. tagFont = font;
  223. isSetTagFont = true;
  224. }
  225. /* 设置说明字体 */
  226. void CustomPieChart::setLegendFont(const QFont &font)
  227. {
  228. legendFont = font;
  229. isSetLegendFont = true;
  230. }
  231. /* 设置总数字体 */
  232. void CustomPieChart::setSumFont(const QFont &font)
  233. {
  234. sumFont = font;
  235. isSetSumFont = true;
  236. }
  237. /* 设置"总数"文本字体 */
  238. void CustomPieChart::setSumTextFont(const QFont &font)
  239. {
  240. sumTextFont = font;
  241. isSetSumTextFont = true;
  242. }
  243. /* 设置圆环大小 */
  244. void CustomPieChart::setRingSize(const double &ringSize)
  245. {
  246. this->ringSize = (ringSize > 1) ? 1 : ringSize;
  247. }