In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use the Qt QChart drawing component, which is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
QtCharts component is a module that provides chart drawing in QT. This module can easily draw conventional graphics. Qtcharts component is implemented based on GraphicsView mode, and its core is the secondary packaging version of QChartView and QChart.
When using the drawing module, you need to include QT + = charts in the pro file to introduce the drawing class library.
Then you need to define the QT_CHARTS_USE_ name macro in the header file so that you can use the drawing function normally.
Normally, we will add the following code snippet to the mainwindows.h header file.
# include # include QT_CHARTS_USE_NAMESPACE// solves the problem of garbled Chinese characters in the interface when compiling MSVC # if _ MSC_VER > = 1600#pragma execution_character_set ("utf-8") # endif
Since there is no separate drawing canvas in QT, we need to put a graphicsView component in the form before drawing.
And right-click on the component to promote it to QChartView
Enter the name of the component that needs to be promoted to promote it to a global drawing component.
Draw a line chart
Line charts are widely used. The following code first initializes the canvas with InitChart (), and then calls the SetData () implementation to populate the canvas with data. The complete code is as follows.
# include "mainwindow.h" # include "ui_mainwindow.h" / / initialize Chart chart void MainWindow::InitChart () {/ / create various parts of the chart QChart * chart = new QChart (); chart- > setTitle ("system performance Statistics"); / / add Chart to ChartView ui- > graphicsView- > setChart (chart); / / this- > setCentralWidget (ui- > graphicsView); ui- > graphicsView- > setRenderHint (QPainter::Antialiasing) / / set chart theme color ui- > graphicsView- > chart ()-> setTheme (QChart::ChartTheme (0)); / / create curve sequence QLineSeries * series0 = new QLineSeries (); QLineSeries * series1 = new QLineSeries (); series0- > setName ("one minute load"); series1- > setName ("five minute load"); / / add sequence to chart chart- > addSeries (series0); chart- > addSeries (series1) / / other additional parameters series0- > setPointsVisible (false); / / set data point visible series1- > setPointLabelsVisible (false); / / set data point value visible / / create axis QValueAxis * axisX = new QValueAxis; / / X axis axisX- > setRange (1,100); / / set axis range axisX- > setTitleText ("X axis title") / / title axisX- > setLabelFormat ("% d%"); / / format x-axis axisX- > setTickCount (3); / / set scale axisX- > setMinorTickCount (3); QValueAxis * axisY = new QValueAxis; / / Y-axis axisY- > setRange (0,100); / / Y-axis range (- 1-20) axisY- > setTitleText ("Y-axis title") / / title / / set X to Y data set chart- > setAxisX (axisX, series0); / / set axis chart- > setAxisY (axisY, series0) for sequence; chart- > setAxisX (axisX, series1); / / set axis chart- > setAxisY (axisY, series1) for sequence / / foreach (QLegendMarker* marker, chart- > legend ()-> markers ()) {QObject::disconnect (marker, SIGNAL (clicked ()), this, SLOT (on_LegendMarkerClicked (); QObject::connect (marker, SIGNAL (clicked ()), this, SLOT (on_LegendMarkerClicked () Qreal alpha = 1.0; if (! marker- > series ()-> isVisible () alpha = 0.5; QColor color; QBrush brush = marker- > labelBrush (); color = brush.color (); color.setAlphaF (alpha); brush.setColor (color); marker- > setLabelBrush (brush) Brush = marker- > brush (); color = brush.color (); color.setAlphaF (alpha); brush.setColor (color); marker- > setBrush (brush); QPen pen = marker- > pen (); color = pen.color (); color.setAlphaF (alpha); pen.setColor (color) Marker- > setPen (pen); break;} default: break;}}
The effect is as follows:
Draw a pie chart
The percentage of the set used for statistics in the pie chart is drawn in the same way as the line chart, and the code is as follows.
# include "mainwindow.h" # include "ui_mainwindow.h" / / pie chart Avoid MainWindow::printA () {/ / Construction data [CPU used 60%] [remaining CPU 40%] QPieSlice * slice_1 = new QPieSlice (QStringLiteral ("used"), 0.6, this); slice_1- > setLabelVisible (true); QPieSlice * slice_2 = new QPieSlice (QStringLiteral (available), 0.4, this); slice_2- > setLabelVisible (true) / / add two pie partitions to series QPieSeries * series = new QPieSeries (this); series- > append (slice_1); series- > append (slice_2); / / create Chart canvas QChart * chart = new QChart (); chart- > addSeries (series); chart- > setAnimationOptions (QChart::AllAnimations); / / set animation chart- > setTitle ("system CPU Utilization") / / set parameters to canvas ui- > graphicsView- > setChart (chart); ui- > graphicsView- > setRenderHint (QPainter::Antialiasing); ui- > graphicsView- > chart ()-> setTheme (QChart::ChartTheme (0));} / / Pie chart Bvoid MainWindow::printB () {/ / Construction data [C disk 20%] [D disk 30%] [E disk 50%] QPieSlice * slice_c = new QPieSlice (QStringLiteral ("C disk"), 0.2, this) Slice_c- > setLabelVisible (true); QPieSlice * slice_d = new QPieSlice (QStringLiteral ("D disk"), 0.3, this); slice_d- > setLabelVisible (true); QPieSlice * slice_e = new QPieSlice (QStringLiteral ("E disk"), 0.5 department this); slice_e- > setLabelVisible (true); / / add two pie partitions to series QPieSeries * series = new QPieSeries (this); series- > append (slice_c) Series- > append (slice_d); series- > append (slice_e); / / create Chart canvas QChart * chart = new QChart (); chart- > addSeries (series); chart- > setAnimationOptions (QChart::AllAnimations); / / set animation chart- > setTitle ("system disk Information"); / / set parameters to canvas ui- > graphicsView_2- > setChart (chart) Ui- > graphicsView_2- > setRenderHint (QPainter::Antialiasing); ui- > graphicsView_2- > chart ()-> setTheme (QChart::ChartTheme (3)); / / set different themes} / / upgrade the added widget controls to QChartView class MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); printA (); printB ();} MainWindow::~MainWindow () {delete ui;}
The effect is as follows:
Draw a bar chart
A bar chart can be used to display more than one user data at a time, which is roughly the same as the line chart. The code is as follows:
# include "mainwindow.h" # include "ui_mainwindow.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); / / creator name QBarSet * set0 = new QBarSet ("Zhang San"); QBarSet * set1 = new QBarSet ("Li Si"); QBarSet * set2 = new QBarSet ("Wang Wu"); QBarSet * set3 = new QBarSet ("Su San") QBarSet * set4 = new QBarSet ("pockmarked Liu"); / / add bu different datasets for different people * set0
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.