【Qt】Qt再學習(二):Chart Themes Example(常用圖表)
1、簡介
該示例中展示了各種圖表以及在不同內建主題下的外觀。
2、使用到的類
QChart:圖表抽象類,繼承自QGraphicsWidget
QChartView:顯示圖表視窗,繼承自QGraphicsView
QLineSeries:折線圖
QAreaSeries:面積圖
QStackedBarSeries:分段條狀圖
QScatterSeries:散點圖
QSplineSeries:曲線圖
QPieSeries:餅圖
3、各種圖表建立demo
QChart *ThemeWidget::createAreaChart() const
{
QChart *chart = new QChart();
chart->setTitle("Area chart");
// The lower series initialized to zero values
QLineSeries *lowerSeries = 0;
QString name("Series ");
int nameIndex = 0;
for (int i(0); i < m_dataTable.count(); i++) {
QLineSeries *upperSeries = new QLineSeries(chart);
for (int j(0); j < m_dataTable[i].count(); j++) {
Data data = m_dataTable[i].at(j);
if (lowerSeries) {
const QVector<QPointF>& points = lowerSeries->pointsVector();
upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
} else {
upperSeries->append(QPointF(j, data.first.y()));
}
}
QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
area->setName(name + QString::number(nameIndex));
nameIndex++;
chart->addSeries(area);
lowerSeries = upperSeries;
}
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, m_valueCount - 1);
chart->axes(Qt::Vertical).first()->setRange(0, m_valueMax);
// Add space to label to add space between labels and axis
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f ");
return chart;
}
QChart *ThemeWidget::createBarChart(int valueCount) const
{
Q_UNUSED(valueCount);
QChart *chart = new QChart();
chart->setTitle("Bar chart");
QStackedBarSeries *series = new QStackedBarSeries(chart);
for (int i(0); i < m_dataTable.count(); i++) {
QBarSet *set = new QBarSet("Bar set " + QString::number(i));
for (const Data &data : m_dataTable[i])
*set << data.first.y();
series->append(set);
}
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).first()->setRange(0, m_valueMax * 2);
// Add space to label to add space between labels and axis
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f ");
return chart;
}
QChart *ThemeWidget::createLineChart() const
{
QChart *chart = new QChart();
chart->setTitle("Line chart");
QString name("Series ");
int nameIndex = 0;
for (const DataList &list : m_dataTable) {
QLineSeries *series = new QLineSeries(chart);
for (const Data &data : list)
series->append(data.first);
series->setName(name + QString::number(nameIndex));
nameIndex++;
chart->addSeries(series);
}
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);
chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);
// Add space to label to add space between labels and axis
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f ");
return chart;
}
QChart *ThemeWidget::createPieChart() const
{
QChart *chart = new QChart();
chart->setTitle("Pie chart");
QPieSeries *series = new QPieSeries(chart);
for (const Data &data : m_dataTable[0]) {
QPieSlice *slice = series->append(data.second, data.first.y());
if (data == m_dataTable[0].first()) {
// Show the first slice exploded with label
slice->setLabelVisible();
slice->setExploded();
slice->setExplodeDistanceFactor(0.5);
}
}
series->setPieSize(0.4);
chart->addSeries(series);
return chart;
}
QChart *ThemeWidget::createSplineChart() const
{
QChart *chart = new QChart();
chart->setTitle("Spline chart");
QString name("Series ");
int nameIndex = 0;
for (const DataList &list : m_dataTable) {
QSplineSeries *series = new QSplineSeries(chart);
for (const Data &data : list)
series->append(data.first);
series->setName(name + QString::number(nameIndex));
nameIndex++;
chart->addSeries(series);
}
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);
chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);
// Add space to label to add space between labels and axis
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f ");
return chart;
}
QChart *ThemeWidget::createScatterChart() const
{
// scatter chart
QChart *chart = new QChart();
chart->setTitle("Scatter chart");
QString name("Series ");
int nameIndex = 0;
for (const DataList &list : m_dataTable) {
QScatterSeries *series = new QScatterSeries(chart);
for (const Data &data : list)
series->append(data.first);
series->setName(name + QString::number(nameIndex));
nameIndex++;
chart->addSeries(series);
}
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);
chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);
// Add space to label to add space between labels and axis
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f ");
return chart;
}
相關文章
- Qt QChart 建立圖表QT
- Qt學習2QT
- Qt學習之XMLQTXML
- Qt學習之路2QT
- QT外掛學習QT
- Qt 事件機制 學習QT事件
- QT常用函式QT函式
- QT學習:給label新增圖片資源QT
- Qt學習第二篇(基本小元件的使用)QT元件
- Qt 學習筆記 - 第二章 - 新增圖片、佈局、介面切換QT筆記
- QT學習筆記4(動畫)QT筆記動畫
- QT5學習 QFileSystemModelQT
- QT學習記錄總結QT
- QT 學習錯誤總結QT
- Qt學習--Qt Plugin建立及呼叫2(外掛管理器)QTPlugin
- Qt學習- (掃雷專案初學)QT
- QT5學習 QStringListModelQT
- Qt學習之路(50): QString薦QT
- Qt DLL總結【二】-建立及呼叫QT的 DLLQT
- QT declarative UI 初探(二)QTUI
- 什麼是qt,QT Creator, QT SDK, QT DesignerQT
- QT學習 實時顯示時間QT
- Qt學習筆記(五)QString 字串QT筆記字串
- Qt開發Activex筆記(二):Qt呼叫Qt開發的Activex控制元件QT筆記控制元件
- QT學習(1):QT5 7+VS2013開發環境搭建QT開發環境
- qt樣式表未生效QT
- QT學習筆記(三)——QT中的座標系統及視窗位置,大小配置QT筆記
- Qt學習之---Qt+VS2010整合開發環境(IDE)搭建QT開發環境IDE
- 頁表的再學習
- Qt的5種常用佈局搭建QT
- QT 正規表示式 通常用法QT
- Qt5 中常用的模組列表:QT
- qt呼叫js,js呼叫qtQTJS
- Qt:QT右鍵選單QT
- QT父子與QT物件deleteQT物件delete
- QT&Opencv練習(一)QTOpenCV
- qt 佈局---表單佈局QT
- (持續更新)Qt3D 學習資源QT3D