CustomPieChart.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // "总数");
  75. painter.setFont(globalFont);
  76. for (int count = 0; count < total; count++) {
  77. startLength += (count > 0) ? int(360 * 16 * dataList[count - 1] / sum) : 0;
  78. int arcLength = int(360 * 16 * dataList[count] / sum); // 弧长
  79. double angle = 360 * dataList[count] / sum; // 扇形的弧度
  80. startAngle += (count > 0) ? 360 * dataList[count - 1] / sum : 0;
  81. double radian = angle / 2 + startAngle; // 当前弧度(所在扇形的二等分角度 + 所在扇形的起始角度)
  82. double offset = fontSize; // 偏移量
  83. double offsetX = 0; // x轴偏移量
  84. double offsetY = 0; // y轴偏移量
  85. /* 绘制扇形区域 */
  86. painter.setPen(Qt::NoPen);
  87. QRadialGradient radialGradient(midPoint, midPoint, radius);
  88. radialGradient.setColorAt(0, Qt::transparent);
  89. radialGradient.setColorAt(ringSize, Qt::transparent);
  90. radialGradient.setColorAt((ringSize + 0.01 > 1) ? 1 : ringSize + 0.01,
  91. qFuzzyCompare(ringSize, 1)
  92. ? Qt::transparent
  93. : colorList[count]); // 从颜色列表中取出颜色并设为区域背景色
  94. radialGradient.setColorAt(1, qFuzzyCompare(ringSize, 1) ? Qt::transparent : colorList[count]);
  95. painter.setBrush(colorList[count]);
  96. painter.drawPie(QRectF(-radius, -radius, diameter, diameter), startLength, arcLength); //
  97. // 绘制饼图的扇形区域
  98. /* 绘制说明 */
  99. painter.setPen(colorList[count]);
  100. painter.setBrush(colorList[count]);
  101. legendFont.setPointSizeF(!isSetLegendFont ? radius / 5 : legendFont.pointSizeF());
  102. painter.setFont(legendFont);
  103. fontSize = legendFont.pointSize();
  104. painter.drawRect(QRectF(-(radius * 2.1 + fontSize * 1.2), -(radius / 2.1) + count * (fontSize * 1.3),
  105. fontSize / 2, fontSize / 2));
  106. painter.drawText(
  107. QRectF(-(radius * 2.1), -(radius / 2.1 + fontSize / 3) + count * (fontSize * 1.3), radius, radius),
  108. tagList[count]);
  109. /* 绘制标签折线 */
  110. tagFont.setPointSizeF(!isSetTagFont ? radius / 8 : tagFont.pointSizeF());
  111. painter.setFont(tagFont);
  112. fontSize = tagFont.pointSize();
  113. QPointF point = QPointF(midPoint + radius * cos(radian * M_PI / 180),
  114. midPoint - radius * sin(radian * M_PI / 180)); // 弧度在坐标轴的象限点
  115. QPolygonF polygon; // 多段线
  116. polygon << point;
  117. QString tagText = QString("%1").arg(dataList[count] * 100) + "%";
  118. if (sum - 0.01 > 1 || sum + 0.01 < 1) {
  119. tagText = QString("%1").arg(dataList[count]);
  120. }
  121. double textWidth = tagText.size() / 2 * fontSize;
  122. if (dataList[count] != 0) { // 数据不为零才可以绘制折线和标签
  123. if (radian > 0 && radian <= 90) { // 第一象限
  124. offsetX += offset;
  125. offsetY -= offset;
  126. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  127. offsetX += textWidth;
  128. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  129. offsetX -= textWidth;
  130. offsetY -= (fontSize * 1.5);
  131. } else if (radian > 90 && radian <= 180) { // 第二象限
  132. offsetX -= offset;
  133. offsetY -= offset;
  134. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  135. offsetX -= textWidth;
  136. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  137. offsetY -= (fontSize * 1.5);
  138. } else if (radian > 180 && radian <= 270) { // 第三象限
  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. } else if (radian > 270 && radian <= 360) { // 第四象限
  145. offsetX += offset;
  146. offsetY += offset;
  147. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  148. offsetX += textWidth;
  149. polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
  150. offsetX -= textWidth;
  151. }
  152. painter.drawPolyline(polygon);
  153. /* 绘制标签 */
  154. painter.drawText(QRectF(point.x() + offsetX, point.y() + offsetY, diameter, diameter),
  155. QStringLiteral("%1").arg(tagText));
  156. }
  157. }
  158. painter.restore();
  159. }
  160. /* 初始化饼图部件 */
  161. void CustomPieChart::initPieChartWidget()
  162. {
  163. isSetTitleFont = false;
  164. isSetTagFont = false;
  165. isSetLegendFont = false;
  166. isSetSumFont = false;
  167. isSetSumTextFont = false;
  168. setRingSize(0.6);
  169. /* 标题部件 */
  170. titleWidget = new QWidget(this);
  171. titleWidget->installEventFilter(this);
  172. /* 饼图部件 */
  173. pieChartWidget = new QWidget(this);
  174. pieChartWidget->installEventFilter(this);
  175. QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
  176. vBoxLayout->setSpacing(0);
  177. vBoxLayout->setMargin(0);
  178. vBoxLayout->addWidget(titleWidget, 1);
  179. vBoxLayout->addWidget(pieChartWidget, 9);
  180. setLayout(vBoxLayout);
  181. }
  182. /* 增加切片 */
  183. void CustomPieChart::addSlice(const QString &tag, const int &data, const QColor &color)
  184. {
  185. tagList << tag; // 标签名列表
  186. dataList << data; // 数据列表
  187. colorList << color; // 颜色列表
  188. total = dataList.size(); // 数据表长度
  189. sum += (total == 1) ? 0 : dataList[total - 1]; // 数据总量
  190. }
  191. /* 设置系列 */
  192. void CustomPieChart::setSeries(QStringList tagList, QList<double> dataList, QList<QColor> colorList)
  193. {
  194. total = dataList.size(); // 数据表长度
  195. sum = 0; // 数据总量
  196. this->tagList = tagList; // 标签名列表
  197. this->dataList = dataList; // 数据列表
  198. this->colorList = colorList; // 颜色列表
  199. for (int count = 0; count < total; count++) {
  200. sum += dataList[count];
  201. }
  202. }
  203. /* 设置全局字体 */
  204. void CustomPieChart::setGlobalFont(const QFont &font)
  205. {
  206. globalFont = font;
  207. titleFont = font;
  208. tagFont = font;
  209. legendFont = font;
  210. sumFont = font;
  211. sumTextFont = font;
  212. sumFont = font;
  213. isSetTitleFont = true;
  214. isSetTagFont = true;
  215. isSetLegendFont = true;
  216. isSetSumFont = true;
  217. isSetSumTextFont = true;
  218. }
  219. /* 设置标题字体 */
  220. void CustomPieChart::setTitleFont(const QFont &font)
  221. {
  222. titleFont = font;
  223. isSetTitleFont = true;
  224. }
  225. /* 设置标签字体 */
  226. void CustomPieChart::setTagFont(const QFont &font)
  227. {
  228. tagFont = font;
  229. isSetTagFont = true;
  230. }
  231. /* 设置说明字体 */
  232. void CustomPieChart::setLegendFont(const QFont &font)
  233. {
  234. legendFont = font;
  235. isSetLegendFont = true;
  236. }
  237. /* 设置总数字体 */
  238. void CustomPieChart::setSumFont(const QFont &font)
  239. {
  240. sumFont = font;
  241. isSetSumFont = true;
  242. }
  243. /* 设置"总数"文本字体 */
  244. void CustomPieChart::setSumTextFont(const QFont &font)
  245. {
  246. sumTextFont = font;
  247. isSetSumTextFont = true;
  248. }
  249. /* 设置圆环大小 */
  250. void CustomPieChart::setRingSize(const double &ringSize)
  251. {
  252. this->ringSize = (ringSize > 1) ? 1 : ringSize;
  253. }