欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > QPieSeries-饼状图

QPieSeries-饼状图

2024/10/24 4:50:54 来源:https://blog.csdn.net/zhangzx36/article/details/140848753  浏览:    关键词:QPieSeries-饼状图
void Widget::initWindowQPie()
{//[1] 创建饼图QPieSeries* pSeries = new QPieSeries();pSeries->append("苹果", 15);pSeries->append("西瓜", 30);pSeries->append("香蕉", 10);pSeries->append("葡萄", 25);pSeries->append("榴莲", 20);//[2] 表示饼系列中的单个切片qreal total = 0; //qreal 是一个用于表示浮点数的数据类型qDebug() << pSeries->slices().size();for (int i = 0; i < pSeries->slices().size(); i++){total += pSeries->slices().at(i)->value();qDebug()<< "test->total:" << total;}QList<QColor> colors = {Qt::red, Qt::green, Qt::yellow, Qt::blue, Qt::magenta};int colorIndex = 0;foreach (QPieSlice *slice, pSeries->slices()){// 每个切换设置颜色QColor color = colors[colorIndex % colors.size()]; // 循环使用颜色列表slice->setColor(color);qDebug() << "slice->value" << slice->value();qreal percentage = (slice->value() / total) * 100;slice->setLabel(QString("%1 (%2%)").arg(slice->label()).arg(percentage, 0, 'f', 1));// 此属性保留切片标签的可见性slice->setLabelVisible(true);colorIndex++;}QPieSlice *slice = pSeries->slices().at(1);// 此属性用于保存切片是否与饼图分离slice->setExploded(true);slice->setPen(QPen(Qt::darkGreen, 0));slice->setBrush(Qt::green);//![2]//![3] 创建图表QChart *chart = new QChart();chart->addSeries(pSeries);chart->setTitle("水果消费量");chart->legend()->hide();//![3]//![4] 图表视图QChartView *chartView = new QChartView();chartView->setChart(chart);chartView->setRenderHint(QPainter::Antialiasing);//![4]QVBoxLayout* pLayout = new QVBoxLayout(this);pLayout->addWidget(chartView);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com