优化一下这段代码//饼状图 QPieSeries *pie_series; pie_series = new QPieSeries(this); //定义各扇形切片的颜色 static const QStringList list_pie_color = { "#6480D6","#A1DC85","#FFAD25","#FF7777","#84D1EF","#4CB383", }; //设置数据 QList<qreal> list_data = {3.1, 3.2, 3.3, 3.4, 3.5, 3.6}; //扇形 for (int i = 0; i < list_pie_color.size(); i++) { QPieSlice* pie_slice = new QPieSlice(this); pie_slice->setLabelVisible(true); pie_slice->setValue(list_data[i]); pie_slice->setLabel(QString::number(list_data[i])); pie_slice->setColor(list_pie_color[i]); pie_slice->setLabelColor(list_pie_color[i]); pie_slice->setBorderColor(list_pie_color[i]); pie_series->append(pie_slice); } //图表视图 QChart* chart = new QChart; chart->setTitle("岩性分布统计图"); // chart->setTheme(QChart::ChartThemeDark); //标题字体 QFont font = qApp->font(); font.setBold(true); font.setPointSize(16); chart->setTitleFont(font); //加入饼图 chart->addSeries(pie_series); chart->setAnimationOptions(QChart::SeriesAnimations); //图例 chart->legend()->setAlignment(Qt::AlignBottom); chart->legend()->setBackgroundVisible(false); ui->graphicsView_type_pie->setChart(chart);
时间: 2024-04-17 08:24:24 浏览: 231
这段代码可以进行以下优化:
1. 使用列表初始化方式数据和颜色:
```cpp
static const QList<qreal> list_data = {3.1, 3.2, 3.3, 3.4, 3.5, 3.6};
static const QStringList list_pie_color = {"#6480D6","#A1DC85","#FFAD25","#FF7777","#84D1EF","#4CB383"};
```
使用`QList<qreal>`代替`QList<double>`,并使用`QStringList`代替`QString`数组。
2. 使用范围for循环简化代码:
```cpp
for (int i = 0; i < list_data.size(); i++)
{
QPieSlice* pie_slice = new QPieSlice(this);
pie_slice->setLabelVisible(true);
pie_slice->setValue(list_data[i]);
pie_slice->setLabel(QString::number(list_data[i]));
pie_slice->setColor(list_pie_color[i]);
pie_slice->setLabelColor(list_pie_color[i]);
pie_slice->setBorderColor(list_pie_color[i]);
pie_series->append(pie_slice);
}
```
可以将循环改为范围for循环,遍历`list_data`并使用其中的值和对应的颜色创建饼图切片。
3. 将部分代码移到初始化列表中:
```cpp
QChart* chart = new QChart;
chart->setTitle("岩性分布统计图");
chart->setTitleFont(QFont(qApp->font().family(), 16, QFont::Bold));
chart->addSeries(pie_series);
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->legend()->setAlignment(Qt::AlignBottom);
chart->legend()->setBackgroundVisible(false);
ui->graphicsView_type_pie->setChart(chart);
```
将一些操作移动到初始化列表中,使代码更简洁。
优化后的代码如下所示:
```cpp
static const QList<qreal> list_data = {3.1, 3.2, 3.3, 3.4, 3.5, 3.6};
static const QStringList list_pie_color = {"#6480D6","#A1DC85","#FFAD25","#FF7777","#84D1EF","#4CB383"};
QPieSeries *pie_series = new QPieSeries(this);
for (int i = 0; i < list_data.size(); i++)
{
QPieSlice* pie_slice = new QPieSlice(this);
pie_slice->setLabelVisible(true);
pie_slice->setValue(list_data[i]);
pie_slice->setLabel(QString::number(list_data[i]));
pie_slice->setColor(list_pie_color[i]);
pie_slice->setLabelColor(list_pie_color[i]);
pie_slice->setBorderColor(list_pie_color[i]);
pie_series->append(pie_slice);
}
QChart* chart = new QChart;
chart->setTitle("岩性分布统计图");
chart->setTitleFont(QFont(qApp->font().family(), 16, QFont::Bold));
chart->addSeries(pie_series);
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->legend()->setAlignment(Qt::AlignBottom);
chart->legend()->setBackgroundVisible(false);
ui->graphicsView_type_pie->setChart(chart);
```
阅读全文