【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學習:給label新增圖片資源QT
- QT常用函式QT函式
- Qt 事件機制 學習QT事件
- Qt 學習筆記 - 第二章 - 新增圖片、佈局、介面切換QT筆記
- Qt學習第二篇(基本小元件的使用)QT元件
- Qt學習--Qt Plugin建立及呼叫2(外掛管理器)QTPlugin
- Qt學習筆記(五)QString 字串QT筆記字串
- QT學習筆記4(動畫)QT筆記動畫
- Qt開發Activex筆記(二):Qt呼叫Qt開發的Activex控制元件QT筆記控制元件
- QT學習 實時顯示時間QT
- qt樣式表未生效QT
- 如何自學qt(12)——簡單的繪圖QT繪圖
- Qt4.8.5 + VS2010 學習筆記QT筆記
- QT學習筆記(三)——QT中的座標系統及視窗位置,大小配置QT筆記
- qt 佈局---表單佈局QT
- QT&Opencv練習(一)QTOpenCV
- C/C++ Qt 資料庫與Chart實現歷史資料展示C++QT資料庫
- QTQT
- Qt 嵌入式圖形開發大全和QT開發手冊QT
- (持續更新)Qt3D 學習資源QT3D
- QT學習小demo之LightMD(MarkDown編輯器)QT
- 頁表的再學習
- BootstrapBlazor實戰 Chart 圖表使用(1)bootBlazor
- Qt cmake修改.exe程式圖示QT
- Qt5 中常用的模組列表:QT
- Qt Creator匯入不同Qt版本QT
- Qt類圖表元件QtitanChart釋出v5.2.0|附下載QT元件
- C++ Qt開發:Charts繪製各類圖表詳解C++QT
- Qt學習筆記-使用QScreen對螢幕進行截圖(可全屏,可部分)QT筆記
- Qt - Qt Creator下載與安裝QT
- 學習 Qt 程式設計的好書精品推薦!QT程式設計
- Qt Creator 原始碼學習筆記01,初識QTCQT原始碼筆記
- qt -- QString類及常用函式功能的使用QT函式
- QT 讓工作列圖示閃爍QT
- Qt繪圖淺析與例項QT繪圖