CustomPieChart.cpp 11 KB

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