123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "CustomPieChart.h"
- CustomPieChart::CustomPieChart(QWidget *parent) : QWidget(parent)
- {
- title = "默认标题";
- total = 0;
- initPieChartWidget();
- }
- CustomPieChart::CustomPieChart(const QString &title, const QString &tag, const int &data, const QColor &color,
- QWidget *parent)
- : QWidget(parent)
- {
- this->title = title;
- total = 1;
- sum = data;
- addSlice(tag, data, color);
- initPieChartWidget();
- }
- CustomPieChart::CustomPieChart(const QString &title, QStringList tagList, QList<int> dataList, QList<QColor> colorList,
- QWidget *parent)
- : QWidget(parent)
- {
- this->title = title;
- setSeries(tagList, dataList, colorList);
- initPieChartWidget();
- }
- CustomPieChart::~CustomPieChart() { }
- bool CustomPieChart::eventFilter(QObject *widget, QEvent *event)
- {
- if (widget == pieChartWidget && event->type() == QEvent::Paint && total != 0) {
- drawPieChart();
- } else if (widget == titleWidget && event->type() == QEvent::Paint) {
- QPainter painter(titleWidget);
- painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
- painter.save();
-
- painter.setPen(Qt::white);
- titleFont.setPointSizeF(!isSetTitleFont ? titleWidget->height() * 0.75 : titleFont.pointSizeF());
- titleFont.setWeight(!isSetTitleFont ? QFont::Bold : titleFont.weight());
- painter.setFont(titleFont);
- painter.drawText(QRectF(0, 0, width(), height()), title);
- painter.restore();
- }
- return QWidget::eventFilter(widget, event);
- }
- void CustomPieChart::drawPieChart()
- {
- int width = this->width();
- int height = this->height();
- double min = qMin(width, height);
- double diameter = min * 5 / 9;
- double radius = diameter / 2;
- int startLength = 0;
- int midPoint = 0;
- double startAngle = 0;
- QPainter painter(pieChartWidget);
- painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
- painter.translate(width / 2, height / 2);
- painter.save();
- fontSize = globalFont.pointSize();
-
- sumFont.setPointSizeF(!isSetSumFont ? radius / 2.5 : sumFont.pointSizeF());
- painter.setFont(sumFont);
- painter.setPen(QColor(54, 235, 171));
- painter.drawText(QRectF(-radius, -radius - (radius / 4), diameter, diameter), Qt::AlignCenter,
- QString::number(sum));
-
- painter.setPen(Qt::white);
- sumTextFont.setPointSizeF(!isSetSumTextFont ? radius / 5 : sumTextFont.pointSizeF());
- painter.setFont(sumTextFont);
- painter.drawText(QRectF(-radius, -radius + fontSize, diameter, diameter), Qt::AlignCenter, "总数");
- painter.setFont(globalFont);
- for (int count = 0; count < total; count++) {
- startLength += (count > 0) ? int(360 * 16 * dataList[count - 1] / sum) : 0;
- int arcLength = int(360 * 16 * dataList[count] / sum);
- double angle = 360 * dataList[count] / sum;
- startAngle += (count > 0) ? 360 * dataList[count - 1] / sum : 0;
- double radian = angle / 2 + startAngle;
- double offset = fontSize;
- double offsetX = 0;
- double offsetY = 0;
-
- painter.setPen(Qt::NoPen);
- QRadialGradient radialGradient(midPoint, midPoint, radius);
- radialGradient.setColorAt(0, Qt::transparent);
- radialGradient.setColorAt(ringSize, Qt::transparent);
- radialGradient.setColorAt((ringSize + 0.01 > 1) ? 1 : ringSize + 0.01,
- qFuzzyCompare(ringSize, 1)
- ? Qt::transparent
- : colorList[count]);
- radialGradient.setColorAt(1, qFuzzyCompare(ringSize, 1) ? Qt::transparent : colorList[count]);
- painter.setBrush(radialGradient);
- painter.drawPie(QRectF(-radius, -radius, diameter, diameter), startLength, arcLength);
-
- painter.setPen(colorList[count]);
- painter.setBrush(colorList[count]);
- legendFont.setPointSizeF(!isSetLegendFont ? radius / 5 : legendFont.pointSizeF());
- painter.setFont(legendFont);
- fontSize = legendFont.pointSize();
- painter.drawRect(QRectF(-(radius * 2.1 + fontSize * 1.2), -(radius / 2.1) + count * (fontSize * 1.3),
- fontSize / 2, fontSize / 2));
- painter.drawText(
- QRectF(-(radius * 2.1), -(radius / 2.1 + fontSize / 3) + count * (fontSize * 1.3), radius, radius),
- tagList[count]);
-
- tagFont.setPointSizeF(!isSetTagFont ? radius / 5 : tagFont.pointSizeF());
- painter.setFont(tagFont);
- fontSize = tagFont.pointSize();
- QPointF point = QPointF(midPoint + radius * cos(radian * M_PI / 180),
- midPoint - radius * sin(radian * M_PI / 180));
- QPolygonF polygon;
- polygon << point;
- QString tagText = QString::number(dataList[count] / sum * 100, 'f', 0) + "%";
- double textWidth = tagText.size() / 2 * fontSize;
- if (dataList[count] != 0) {
- if (radian > 0 && radian <= 90) {
- offsetX += offset;
- offsetY -= offset;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX += textWidth;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX -= textWidth;
- offsetY -= (fontSize * 1.5);
- } else if (radian > 90 && radian <= 180) {
- offsetX -= offset;
- offsetY -= offset;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX -= textWidth;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetY -= (fontSize * 1.5);
- } else if (radian > 180 && radian <= 270) {
- offsetX -= offset;
- offsetY += offset;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX -= textWidth;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- } else if (radian > 270 && radian <= 360) {
- offsetX += offset;
- offsetY += offset;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX += textWidth;
- polygon << QPointF(point.x() + offsetX, point.y() + offsetY);
- offsetX -= textWidth;
- }
- painter.drawPolyline(polygon);
-
- painter.drawText(QRectF(point.x() + offsetX, point.y() + offsetY, diameter, diameter),
- QStringLiteral("%1").arg(tagText));
- }
- }
- painter.restore();
- }
- void CustomPieChart::initPieChartWidget()
- {
- isSetTitleFont = false;
- isSetTagFont = false;
- isSetLegendFont = false;
- isSetSumFont = false;
- isSetSumTextFont = false;
- setRingSize(0.6);
-
- titleWidget = new QWidget(this);
- titleWidget->installEventFilter(this);
-
- pieChartWidget = new QWidget(this);
- pieChartWidget->installEventFilter(this);
- QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
- vBoxLayout->setSpacing(0);
- vBoxLayout->setMargin(0);
- vBoxLayout->addWidget(titleWidget, 1);
- vBoxLayout->addWidget(pieChartWidget, 9);
- setLayout(vBoxLayout);
- }
- void CustomPieChart::addSlice(const QString &tag, const int &data, const QColor &color)
- {
- tagList << tag;
- dataList << data;
- colorList << color;
- total = dataList.size();
- sum += (total == 1) ? 0 : dataList[total - 1];
- }
- void CustomPieChart::setSeries(QStringList tagList, QList<int> dataList, QList<QColor> colorList)
- {
- total = dataList.size();
- sum = 0;
- this->tagList = tagList;
- this->dataList = dataList;
- this->colorList = colorList;
- for (int count = 0; count < total; count++) {
- sum += dataList[count];
- }
- }
- void CustomPieChart::setGlobalFont(const QFont &font)
- {
- globalFont = font;
- titleFont = font;
- tagFont = font;
- legendFont = font;
- sumFont = font;
- sumTextFont = font;
- sumFont = font;
- isSetTitleFont = true;
- isSetTagFont = true;
- isSetLegendFont = true;
- isSetSumFont = true;
- isSetSumTextFont = true;
- }
- void CustomPieChart::setTitleFont(const QFont &font)
- {
- titleFont = font;
- isSetTitleFont = true;
- }
- void CustomPieChart::setTagFont(const QFont &font)
- {
- tagFont = font;
- isSetTagFont = true;
- }
- void CustomPieChart::setLegendFont(const QFont &font)
- {
- legendFont = font;
- isSetLegendFont = true;
- }
- void CustomPieChart::setSumFont(const QFont &font)
- {
- sumFont = font;
- isSetSumFont = true;
- }
- void CustomPieChart::setSumTextFont(const QFont &font)
- {
- sumTextFont = font;
- isSetSumTextFont = true;
- }
- void CustomPieChart::setRingSize(const double &ringSize)
- {
- this->ringSize = (ringSize > 1) ? 1 : ringSize;
- }
|