Qt QChart 建立圖表

鹹魚Doyoung發表於2020-12-20

Qt QChart 建立圖表

@

效果

Qt QChart 建立圖表

流程

graph LR q(value 資料) q-->s1(QPieSlice)-->ps(QPieSeries)--餅狀圖-->c(QChart)-->v(QChartView)-->w(QWidget) q-->s2(...)-->ps q-->bs(QBarSet)-->b(QBarSeries)--柱狀圖-->c q-->bs1(...)-->b q-->p(QPointF)-->l(QLineSeries/QSplineSeries)--折/曲線圖-->c q-->p1(...)-->l l1(QLineSeries upper)-->a(QAreaSeries)--區域圖-->c l2(QLineSeries lower)-->a

程式碼

1. 餅圖

	// 儲存多個扇形
	QList<QPieSlice *> slices;
	for (int i = 1; i <= 10; ++i)
	{
		// 建立一個扇形
		QPieSlice * slice = new QPieSlice(QString::number(i),i);
		slices << slice;
	}
	// 建立一個餅圖系列
	QPieSeries * pieSeries = new QPieSeries;
	// 當滑鼠懸浮時設定標籤可見,設定餅圖扇形分離
	QObject::connect(pieSeries,&QPieSeries::hovered
		, [](QPieSlice *slice, bool state) {
		slice->setLabelVisible(state);
		slice->setExploded(state);
	});
	// 將所有扇形所加到餅圖中
	pieSeries->append(slices);

	// 建立一個圖表
	QChart * chart = new QChart;
	// 設定標題
	chart->setTitle(QStringLiteral("餅圖"));
	// 設定動畫
	chart->setAnimationOptions(QChart::AllAnimations);
	// 設定圖表的系列
	chart->addSeries(pieSeries);

	// 建立圖表檢視,顯示圖表
    pView = new QChartView(chart);
	// 設定抗鋸齒
	pView->setRenderHint(QPainter::Antialiasing);

	// 新增圖表檢視到佈局
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(pView);

2. 柱圖


	// 儲存柱集合
	QList<QBarSet *> sets;
	for (int i = 1; i <= 5; ++i)
	{
		// 建立一個柱集合
		QBarSet * set = new QBarSet(QString("set").append(QString::number(i)));
		QList<qreal> values;
		for (int j = 1; j <= 5; ++j)
		{
			values << qrand()%100;
			qDebug() << values;
		}
		set->append(values);
		sets << set;
	}
	// 建立一個柱圖系列
	QBarSeries * barSeries = new QBarSeries;
	// 新增柱集合
	barSeries->append(sets);

	QChart * chart = new QChart;
	chart->setTitle(QStringLiteral("柱狀圖"));
	chart->setAnimationOptions(QChart::AllAnimations);
	chart->addSeries(barSeries);

	pView = new QChartView(chart);
	pView->setRenderHint(QPainter::Antialiasing);

	QVBoxLayout *layout = new QVBoxLayout(this);
	layout->addWidget(pView);

3. 折/曲線圖

	// 建立並儲存點資訊
	QList<QPointF> points;
	points << QPointF(0, -1) << QPointF(2, 2) << QPointF(3, 5) << QPointF(5, -5)
		<< QPointF(6, 0) << QPointF(7, 3);

	// 建立線圖系列
	QLineSeries * lineSeries = new QLineSeries;
	// 曲線
	//QSplineSeries * lineSeries = new QSplineSeries;
	
	// 追加點
	lineSeries->append(points);

	QChart * chart = new QChart;
	chart->setTitle(QStringLiteral("線圖"));
	chart->setAnimationOptions(QChart::AllAnimations);
	chart->addSeries(lineSeries);

	pView = new QChartView(chart);
	pView->setRenderHint(QPainter::Antialiasing);

	QVBoxLayout *layout = new QVBoxLayout(this);
	layout->addWidget(pView);

4. 區域圖


	QList<QPointF> pointsLower;
	pointsLower << QPointF(0, -1) << QPointF(2, 2) << QPointF(3, 5) << QPointF(5, -5)
		<< QPointF(6, 0) << QPointF(7, 3);
	// 建立一條線
	QLineSeries * lineSeriesLower = new QLineSeries;
	lineSeriesLower->append(pointsLower);

	QList<QPointF> pointsUpper;
	pointsUpper << QPointF(0, -1) << QPointF(2, 4) << QPointF(3, 3) << QPointF(5, 3)
		<< QPointF(6, 1) << QPointF(7, 5);
	// 建立另一條線
	QLineSeries * lineSeriesUpper = new QLineSeries;
	lineSeriesUpper->append(pointsUpper);

	// 兩條線組成面
	QAreaSeries * areaSeries = new QAreaSeries(lineSeriesLower, lineSeriesUpper);

	QChart * chart = new QChart;
	chart->setTitle(QStringLiteral("區域圖"));
	chart->setAnimationOptions(QChart::AllAnimations);
	chart->addSeries(areaSeries);

	pView = new QChartView(chart);
	pView->setRenderHint(QPainter::Antialiasing);

	QVBoxLayout *layout = new QVBoxLayout(this);
	layout->addWidget(pView);

相關文章