Application background
For some measurement systems, sometimes it is not very intuitive to directly look at the data. If the data is converted into images for display, it will be more intuitive. At the same time, it can also lead to new laws according to images or curves.
When changing the data into image display, some can also verify whether the measurement of the whole system is correct. Next, an easy-to-use QT based curve control QCustomPlot is introduced.
introduce
QCustomPlot is a QT C + + component visualized by drawing and data. It is not based on other dependencies and is well documented. This library can be used for good-looking, publishable 2D drawings, graphics and charts.
It also has high performance for real-time curve rendering. QCustomPlot can export various formats, such as PDF,PNG,JPG,BMP, etc. QCustomPlot is also an open source GPL based protocol. Users are free to view its source code.
You can go to its official website QCustomPlot View detailed instructions.
Demo
Windows operating environment
- First from the official website Download code , select one of the versions
- Copy the downloaded source file to the project directory and add it to the project. Add the printsupport option to the project
As shown in the figure below
- Add a widget control and promote to QCustomPlot
- Directly use the name of the widget to call the QCustomPlot API to achieve the desired results
// add two new graphs and set their look: customPlot->addGraph(); customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue customPlot->addGraph(); customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph // generate some points of data (y0 for first, y1 for second graph): QVector<double> x(251), y0(251), y1(251); for (int i=0; i<251; ++i) { x[i] = i; y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine y1[i] = qExp(-i/150.0); // exponential envelope } // configure right and top axis to show ticks but no labels: // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this) customPlot->xAxis2->setVisible(true); customPlot->xAxis2->setTickLabels(false); customPlot->yAxis2->setVisible(true); customPlot->yAxis2->setTickLabels(false); // make left and bottom axes always transfer their ranges to right and top axes: connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange))); connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange))); // pass data points to graphs: customPlot->graph(0)->setData(x, y0); customPlot->graph(1)->setData(x, y1); // let the ranges scale themselves so graph 0 fits perfectly in the visible area: customPlot->graph(0)->rescaleAxes(); // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0): customPlot->graph(1)->rescaleAxes(true); // Note: we could have also just called customPlot->rescaleAxes(); instead // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking: customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
Basic API notes
addgraph
To add a curve, you must first add a graph, and then add it based on the graph
customPlot->graph(0)->setData(x,y0);
setData
The two-dimensional data is divided into key and value. If the two are packaged into VECTOR, setData sets the data of the whole graph
addData
Data used to add a point
replot
Used to redraw the current graphic display
You can basically display a graph with the above APIs. Generally speaking, QCustomPlot is better at simple curve display. Of course, if you want to achieve good results, you'd better know more about API documents and experiments.
On the whole, QCustomPlot is still very powerful and can realize many complex applications. The official also provides a good example.