QPieSeries类以饼状图的形式展示数据。这个类就描述了饼状图的属性。QPieSeries类是由定义为QPieSlice对象的切片组成。切片可以有任何值,因为QPieSeries对象计算切片与序列中所有切片之和的百分比,以确定图表中切片的实际大小。QPieSeries 其实就是由 一个一个 QPieSlice 对象构成的。
饼图的大小和位置由相对值控制,相对值的范围从0.0到1.0。这些与实际的图表矩形相关。
默认情况下,饼图被定义为一个完整的饼图。也是跨度是 360度,但是我们也可以通过设置序列的起始角度和角度跨度来创建局部饼图。一个完整的派是360度,0在12点钟方向。就是在小于360度的范围内来将局部内容展示和显示的。
这个简单的使用的效果就是这种,我们可以通过设置 QPieSlice 来改变这个颜色样式的。
QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
//![3]
QChart *chart = new QChart(); //绘图设备
chart->addSeries(series); //关联图
chart->setTitle("Simple piechart example");
chart->legend()->hide(); //坐标轴
//![3]
//![4]
QChartView *chartView = new QChartView(chart); //把绘图设备放置在 绘图gui 控件里
chartView->setRenderHint(QPainter::Antialiasing);
//![4]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
#include
#include
#include
#include
#include
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]
QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);
//![1]
//![2]
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
//![2]
//![3]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple piechart example");
chart->legend()->hide();
//![3]
//![4]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![4]
//![5]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
//![5]
return a.exec();
}
这种的效果是下面这种。
QPieSeries *series = new QPieSeries();
series->setHoleSize(0.35); //这个就是控制 那个保留的镂空大小, 是 0 - 1取值的
series->append("Protein 4.2%", 4.2);
QPieSlice *slice = series->append("Fat 15.6%", 15.6);
slice->setExploded();
slice->setLabelVisible();
series->append("Other 23.8%", 23.8);
series->append("Carbs 56.4%", 56.4);
QChartView *chartView = new QChartView();
chartView->setRenderHint(QPainter::Antialiasing);
chartView->chart()->setTitle("Donut with a lemon glaze (100g)");
chartView->chart()->addSeries(series);
chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean);
chartView->chart()->legend()->setFont(QFont("Arial", 7));
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
#include
#include
#include
#include
#include
#include
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]
QPieSeries *series = new QPieSeries();
series->setHoleSize(0.35);
series->append("Protein 4.2%", 4.2);
QPieSlice *slice = series->append("Fat 15.6%", 15.6);
slice->setExploded();
slice->setLabelVisible();
series->append("Other 23.8%", 23.8);
series->append("Carbs 56.4%", 56.4);
//![1]
//![2]
QChartView *chartView = new QChartView();
chartView->setRenderHint(QPainter::Antialiasing);
chartView->chart()->setTitle("Donut with a lemon glaze (100g)");
chartView->chart()->addSeries(series);
chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean);
chartView->chart()->legend()->setFont(QFont("Arial", 7));
//![2]
//![3]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
//![3]
return a.exec();
}
这个类还可以继承重载实现下面的嵌套效果