1 pie chart (pie chart)
1.1 Charts Module
The QT library consists of a number of modules, including the QT Charts, which contains a series of chart components, which are to be added in. Pro before use:
QT + = Charts
CPP file, you need to add the header file and the Declaration namespace
#include <QtCharts>usingnamespace qtcharts;
1.2 Code examples
This is the code that implements the pie chart in Qt 5.7, #1 ~ #5 declares the header file
1 #include <QtWidgets/QApplication>2 #include <QtWidgets/QMainWindow>3 # Include <QtCharts/QChartView>4 #include <QtCharts/QPieSeries>5 #include <qtcharts/ Qpieslice>67usingnamespace qtcharts;
In fact, two header files are also available instead of:
#include <QtWidgets><QtCharts>
#13 ~ #16 Create a new Qpieseries class object and divide it into three parts, 10%, 20%, and 70%, respectively, #17 set the label properties so that they are all visible.
9 intMainintargcChar*argv[])Ten { One qapplication A (argc, argv); A -Qpieseries *series =Newqpieseries (); -Series->append ("10%",1); theSeries->append ("20%",2); -Series->append ("70%",7); -Series->setlabelsvisible ();
#19 ~ #24 Set the color of each part: Red, green, blue.
+Qpieslice *slice0 = Series->slices (). at (0); -Qpieslice *slice1 = Series->slices (). at (1); +Qpieslice *slice2 = Series->slices (). at (2); ASlice0->setcolor (Qcolor (255,0,0,255)); atSlice1->setcolor (Qcolor (0,255,0,255)); -Slice2->setcolor (Qcolor (0,0,255,255));
#26 ~ #29 Create a new Qchart class pointer and add the series to the chart, then set the title and hide the legend;
#31 ~ #32 Create a new Qchartview class pointer, and then add the chart to ChartView, setting its Render property to "antialiasing" (qpainter::antialiasing);
#34 ~ #37 Create a new Qmainwindow class object and place the ChartView in the middle, then resize it and show it with Show ().
-Qchart *chart =NewQchart (); -Chart->addseries (series); -Chart->settitle ("Piechart Example"); inChart->legend ()hide (); - toQchartview *chartview =NewQchartview (chart); +Chartview->Setrenderhint (qpainter::antialiasing); - theQmainwindow window; *Window.setcentralwidget (chartview); $Window.resize (480, the);Panax Notoginsengwindow.show (); - the returna.exec (); +}
The program interface output is as follows:
2 Doughnut Chart (donut chart)
The #4 uses a namespace declaration in Qt, equivalent to using namespace Qtcharts
There is space in the middle of the doughnut chart, corresponding to the #11, set the size of the middle hole, in addition, #21 set the display theme of the doughnut chart, #22 set the font of the legend, the other code, the pie chart is similar.
1#include <QtWidgets>2#include <QtCharts>3 4 Qt_charts_use_namespace5 6 intMainintargcChar*argv[])7 {8 qapplication A (argc, argv);9 TenQpieseries *series =Newqpieseries ();Series->setholesize (0.35); ASeries->append ("Protein 4.2%",4.2); -Series->append ("Carbs 56.4%",56.4); -Series->append ("Other 23.8%",23.8); the -Qchartview *chartview =NewQchartview (); -Chartview->Setrenderhint (qpainter::antialiasing); -Chartview->chart ()->settitle ("Donutchart Example"); +Chartview->chart ()addseries (series); -Chartview->chart ()->legend ()setalignment (qt::alignbottom);Chartview->chart ()->settheme (Qchart::chartthemebluecerulean); Chartview->chart ()->legend () ->setfont (Qfont ("Arial", 7)); at - Qmainwindow window; - Window.setcentralwidget (chartview); -Window.resize ( -, -); - window.show (); - in returna.exec (); -}
The output interface is as follows:
Qt's pie chart