QT - QChart的簡單使用


Qt 一般使用圖標等一般是使用QChart (Qt5.7以后才有) 或者QCustomPlot (自定義圖標來繪制圖標) QWT第三方控件。

使用QChart 要在安裝Qt的選擇QChart 模塊 否則 不能使用!!!
我用的是Qt5.8的就跟多案例

 

 

餅狀圖
主要代碼:

m_chart = new QChart();
QPieSeries *series = new QPieSeries();    //連續的餅圖數據
series->append("水果:30%", 3);    //添加標簽"水果:30%" 和 百分值30%
series->append("零食:20%", 2);
series->append("主食:40%", 4);
series->append("其他:10%", 1);

series->setLabelsVisible(true);
series->setUseOpenGL(true);
series->slices().at(0)->setColor(QColor(13, 128, 217)); //設置顏色
series->slices().at(0)->setLabelColor(QColor(13, 128, 217));
series->slices().at(1)->setColor(QColor(69, 13, 217));
series->slices().at(1)->setLabelColor(QColor(69, 13, 217));
series->slices().at(2)->setColor(QColor(13, 217, 152));
series->slices().at(2)->setLabelColor(QColor(13, 217, 152));
series->slices().at(3)->setColor(QColor(13, 217, 110));
series->slices().at(3)->setLabelColor(QColor(13, 217, 110));

m_chart->setTheme(QChart::ChartThemeLight);//設置白色主題
m_chart->setDropShadowEnabled(true);//背景陰影
m_chart->addSeries(series);//添加系列到QChart上

m_chart->setTitleBrush(QBrush(QColor(0, 0, 255))); //設置標題Brush
m_chart->setTitleFont(QFont("微軟雅黑"));//設置標題字體
m_chart->setTitle("餅狀圖");

//修改說明樣式
m_chart->legend()->setVisible(true);
m_chart->legend()->setAlignment(Qt::AlignRight);//底部對齊
m_chart->legend()->setBackgroundVisible(true);//設置背景是否可視
m_chart->legend()->setAutoFillBackground(true);//設置背景自動填充
m_chart->legend()->setColor(QColor(222, 233, 251)); //設置顏色
m_chart->legend()->setLabelColor(QColor(0, 100, 255)); //設置標簽顏色
m_chart->legend()->setMaximumHeight(150);

QChartView *chartView = new QChartView(m_chart);
chartView->setRenderHint(QPainter::Antialiasing);

QVBoxLayout *pVLayout = new QVBoxLayout(this);
pVLayout->addWidget(chartView);

resize(960, 720);

 

 條形圖

m_chart = new QChart();
//創建3個條線數據
QBarSet *set0 = new QBarSet("零食");
QBarSet *set1 = new QBarSet("水果");
QBarSet *set2 = new QBarSet("主食");

*set0 << 158 << 685 << 458 << 260 << 354;    //向零食數據添加這4個月的銷售數據
*set1 << 350 << 725 << 602 << 523 << 458;
*set2 << 222 << 350 << 598 << 480 << 687;

set0->setLabelColor(QColor(0,0,255));       //設置條形數據顏色
set1->setLabelColor(QColor(0,0,255));
set2->setLabelColor(QColor(0,0,255));

QBarSeries *series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->setVisible(true);
series->setLabelsVisible(true);

m_chart->setTheme(QChart::ChartThemeLight);//設置白色主題
m_chart->setDropShadowEnabled(true);//背景陰影
m_chart->addSeries(series);//添加系列到QChart上

m_chart->setTitleBrush(QBrush(QColor(0,0,255)));//設置標題Brush
m_chart->setTitleFont(QFont("微軟雅黑"));//設置標題字體
m_chart->setTitle("超市銷售條形圖");

//創建X軸和Y軸
QBarCategoryAxis *axisX = new QBarCategoryAxis;
axisX->append("一月");
axisX->append("二月");
axisX->append("三月");
axisX->append("四月");
axisX->append("五月");
axisX->setLabelsColor(QColor(7,28,96));

QValueAxis *axisY = new QValueAxis;
axisY->setRange(0,1000);   //改為setRange(0,1);則表示坐標為動態計算大小的
axisY->setTitleText("價格");
axisY->setLabelFormat("%d$");

m_chart->setAxisX(axisX,series);
m_chart->setAxisY(axisY,series);

//修改說明樣式
m_chart->legend()->setVisible(true);
m_chart->legend()->setAlignment(Qt::AlignBottom);//底部對齊
m_chart->legend()->setBackgroundVisible(true);//設置背景是否可視
m_chart->legend()->setAutoFillBackground(true);//設置背景自動填充
m_chart->legend()->setColor(QColor(222,233,251));//設置顏色
m_chart->legend()->setLabelColor(QColor(0,100,255));//設置標簽顏色
m_chart->legend()->setMaximumHeight(50);

QChartView *chartView = new QChartView(m_chart);
chartView->setRenderHint(QPainter::Antialiasing);

QVBoxLayout *pVLayout = new QVBoxLayout(this);
pVLayout->addWidget(chartView);
resize(960, 720);

 

 曲線圖

m_chart = new QChart();
QSplineSeries *series1 = new QSplineSeries();//實例化一個QLineSeries對象
series1->setColor(QColor(0,100,255));
series1->append(QPointF(0,qrand()%200)) ;
series1->append(QPointF(30,qrand()%200)) ;
series1->append(QPointF(60,qrand()%200)) ;
series1->append(QPointF(90,qrand()%200)) ;
series1->append(QPointF(120,qrand()%200)) ;
series1->setName("線條1");

series1->setVisible(true);
series1->setPointLabelsFormat("(@xPoint,@yPoint)");
series1->setPointLabelsVisible(true);

m_chart->setTheme(QChart::ChartThemeLight);//設置白色主題
m_chart->setDropShadowEnabled(true);//背景陰影    m_chart->setAutoFillBackground(true);  //設置背景自動填充
m_chart->addSeries(series1);//添加系列到QChart上


m_chart->setTitleBrush(QBrush(QColor(0,0,255)));//設置標題Brush
m_chart->setTitleFont(QFont("微軟雅黑"));//設置標題字體
m_chart->setTitle("曲線圖");


//創建X軸和Y軸
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0,150);    //默認則坐標為動態計算大小的
axisX->setLabelFormat("%dS");
QValueAxis *axisY = new QValueAxis;
axisY->setRange(0,250);    //默認則坐標為動態計算大小的
axisY->setTitleText("value值");

m_chart->setAxisX(axisX,series1);
m_chart->setAxisY(axisY,series1);
//m_chart->createDefaultAxes();             //或者創建默認軸

//修改說明樣式
m_chart->legend()->setVisible(true);
m_chart->legend()->setAlignment(Qt::AlignBottom);//底部對齊
m_chart->legend()->setBackgroundVisible(true);//設置背景是否可視
m_chart->legend()->setAutoFillBackground(true);//設置背景自動填充
m_chart->legend()->setColor(QColor(222,233,251));//設置顏色
m_chart->legend()->setLabelColor(QColor(0,100,255));//設置標簽顏色
m_chart->legend()->setMaximumHeight(50);
QChartView *chartView = new QChartView(m_chart);
chartView->setRenderHint(QPainter::Antialiasing);

QVBoxLayout *pVLayout = new QVBoxLayout(this);
pVLayout->addWidget(chartView);

resize(960, 720);

 

 其他的都幾乎差不多,折線圖(Line Chart), 面積圖(Area Chart),散點圖(Scatter Chart)看Qt 自帶的案例就很好理解了。

 

另:

QChart的幾種坐標軸的詳細介紹及使用代碼示例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM