返回

Qt实现扇形图

发布时间:2023-05-17 11:55:43 181
#include "donutbreakdownchart.h"
#include "mainslice.h"
#include
#include

QT_CHARTS_USE_NAMESPACE


DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
: QChart(QChart::ChartTypeCartesian, parent, wFlags)
{
// create the series for main center pie
m_mainSeries = new QPieSeries();
m_mainSeries->setPieSize(0.7);
QChart::addSeries(m_mainSeries);
}

void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
{
QFont font("Arial", 8);

// add breakdown series as a slice to center pie
MainSlice *mainSlice = new MainSlice(breakdownSeries);
mainSlice->setName(breakdownSeries->name());
mainSlice->setValue(breakdownSeries->sum());
m_mainSeries->append(mainSlice);

// customize the slice
mainSlice->setBrush(color);
mainSlice->setLabelVisible();
mainSlice->setLabelColor(Qt::white);
mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
mainSlice->setLabelFont(font);

// position and customize the breakdown series
breakdownSeries->setPieSize(0.8);
breakdownSeries->setHoleSize(0.7);
breakdownSeries->setLabelsVisible();
const auto slices = breakdownSeries->slices();
for (QPieSlice *slice : slices) {
color = color.lighter(115);
slice->setBrush(color);
slice->setLabelFont(font);
}

// add the series to the chart
QChart::addSeries(breakdownSeries);

// recalculate breakdown donut segments
recalculateAngles();

// update customize legend markers
updateLegendMarkers();
}

void DonutBreakdownChart::recalculateAngles()
{
qreal angle = 0;
const auto slices = m_mainSeries->slices();
for (QPieSlice *slice : slices) {
QPieSeries *breakdownSeries = qobject_cast<MainSlice *>(slice)->breakdownSeries();
breakdownSeries->setPieStartAngle(angle);
angle += slice->percentage() * 360.0; // full pie is 360.0
breakdownSeries->setPieEndAngle(angle);
}
}

void DonutBreakdownChart::updateLegendMarkers()
{
// go through all markers
const auto allseries = series();
for (QAbstractSeries *series : allseries) {
const auto markers = legend()->markers(series);
for (QLegendMarker *marker : markers) {
QPieLegendMarker *pieMarker = qobject_cast<QPieLegendMarker *>(marker);
if (series == m_mainSeries) {
// hide markers from main series
pieMarker->setVisible(false);
} else {
// modify markers from breakdown series
pieMarker->setLabel(QString("%1 %2%")
.arg(pieMarker->slice()->label())
.arg(pieMarker->slice()->percentage() * 100, 0, 'f', 2));
pieMarker->setFont(QFont("Arial", 8));
}
}
}
}

Qt实现扇形图_扇形图

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Qt实现文件树 2023-05-17 08:51:22