One, can achieve the diagram
Compared to other third-party libraries that draw charts, Qcustomplot is relatively lightweight, not only for functions, but also for two of times to develop easily. Let's go through the details of what he can achieve
- Qcpgraph: Line chart, LineStyle enumeration can set the style of line drawing, the main difference is the way between two points, the normal way is direct connection, other also L-type, z-type, etc., 1, are line chart, just set up a different polyline parameters The Qcpscatterstyle enumeration is used to set the node type, including: circles, triangles, etc., as shown in 2.
- Qcpbars: Histogram, figure 3
- Qcpfinancial: Candlestick chart, mainly used to display the stock K-line chart, shown in 4.
- Other charts include: Chromatogram (Qcpcolormap), Statistics Box (qcpstatisticalbox), parametric curve (qcpcurve), these pictures I do not separate map description, want to see the effect of the students can go to qcustomplot use Share (a) Can do what to see.
Second, the effect preview
I'm going to stick it out here alone. Qcustomplot can be implemented in a chart, or click Qcustomplot to share (a) what can be done, this article has a GIF, the diagram contains all the figures.
Figure 10 Percent Line chart
Figure 20 percent Line chart
Figure 3 Bar chart
Figure 4 Candle Chart
Third, data storage
Because this article is based on the Qcustomplot2.0.0beta version analysis, if there are students found and their own source of discrepancies, first look at the source version is consistent, if the source version consistent, but and I said the discrepancy, welcome correction.
A key template class, Qcpdatacontainer, provides some basic methods of data manipulation, such as setting up data, adding data, removing keystrokes, emptying, sorting, and finding, and the reason for this class is that graphs and graphs are just different types of data stored. But they are basically the same interface for these data operations, and the function of this class is to extract the public interfaces, if the chart requires such data storage, only need to use this class to store, class template parameters indicate the specific storage data type.
Because the different tables are only the data storage format is not the same, and their interface operation on the data has been abstracted to the Qcpdatacontainer class, such as: Line graph Data storage class Qcpgraphdata, histogram data storage class Qcpbarsdata, etc. When they use data storage, they declare objects like this qcpdatacontainer<qcpgraphdata> and qcpdatacontainer<qcpbarsdata>.
At this point, the abstraction is not over, because all the drawing classes simply provide different interface parameters, just as the structure of the final stored data is different, such as a line chart may be like AddData (Qcpgraphdata), the histogram may be like adddata (Qcpbarsdata) , but the operation of the implementation of these interfaces is exactly the same, and this time there is an abstract interface class QCPPLOTTABLEINTERFACE1D, the class implementation class is QCPABSTRACTPLOTTABLE1D, It contains a qcpdatacontainer pointer that stores specific chart type data, and his statement might look like the one below
1Template <classdatatype>//The class initializes the parameter when it is inherited, and is typically set to the data type stored by the chart2 classQcp_lib_decl qcpabstractplottable1d: PublicQcpabstractplottable, Publicqcpplottableinterface1d3 {4 //NO Q_object macro due to template class5 6 Public:7QCPABSTRACTPLOTTABLE1D (Qcpaxis *keyaxis, Qcpaxis *Valueaxis);8 Virtual~qcpabstractplottable1d ();9 Ten //virtual methods of 1d Plottable interface: One Virtual intDatacount ()Const; A Virtual DoubleDatamainkey (intIndexConst; - Virtual DoubleDatasortkey (intIndexConst; - Virtual DoubleDatamainvalue (intIndexConst; the VirtualQcprange Datavaluerange (intIndexConst; - Virtualqpointf Datapixelposition (intIndexConst; - Virtual BOOLSortkeyismainkey ()Const; - VirtualQcpdataselection Selecttestrect (ConstQRECTF &rect,BOOLonlyselectable)Const; + Virtual intFindbegin (DoubleSortKey,BOOLExpandedrange =true)Const; - Virtual intFindend (DoubleSortKey,BOOLExpandedrange =true)Const; + A //Virtual methods: at Virtual DoubleSelecttest (Constqpointf &pos,BOOLOnlyselectable, Qvariant *details =0)Const; - VirtualQcpplottableinterface1d *interface1d () {return This; } - - protected: - //Members : -qsharedpointer<qcpdatacontainer<datatype> >mdatacontainer;//data storage container, which provides most of the operations in - //helpers for subclasses: to voidGetdatasegments (qlist<qcpdatarange> &selectedsegments, Qlist<qcpdatarange> & unselectedsegments)Const; + voidDrawPolyline (Qcppainter *painter,ConstQvector<qpointf> &linedata)Const; - the Private: * q_disable_copy (qcpabstractplottable1d) $};
When it comes to this, we may still be in a daze, so next we will add the data interface code of the previous Qcpgraph class, and we will see it at a glance. Although the Qcpdatacontainer container class gives us a lot of interfaces, some of the interface's return values are not available, such as the Begine and end pointer addresses, but the Qcpplottableinterface1d class helps us get it, Because all of our chart classes are subclasses of that class, we can use the method in which we can see the implementation class of the Qcpplottableinterface1d interface class by ourselves qcpabstractplottable1d
1 voidQcpgraph::adddata (Constqvector<Double> &keys,Constqvector<Double> &values,BOOLalreadysorted)2 {3 if(Keys.size ()! =values.size ())4Qdebug () << q_func_info <<"keys and values have different sizes:"<< keys.size () <<values.size ();5 Const intn =qmin (Keys.size (), values.size ());6Qvector<qcpgraphdata>tempdata (n);7Qvector<qcpgraphdata>::iterator it =Tempdata.begin ();8 ConstQvector<qcpgraphdata>::iterator Itend =tempdata.end ();9 inti =0;Ten while(It! =itend) One { AIt->key =Keys[i]; -It->value =Values[i]; -++it; the++i; - } -Mdatacontainer->add (TempData, alreadysorted);//This add interface is actually provided by the Qcpdatacontainer class, -}
The above data storage abstraction is actually not available in the QCustomPlot1.3.2 release, but I think this part is pretty good, not only simplifying the code, but also being more maintainable.
四、一个 a simple example
Qcustomplot interface encapsulation or is relatively easy to understand, I will not be here to illustrate the use of each chart, the following is a simple usage of the line chart, corresponding to the figure 1
1Ui.widget_12->legend->setvisible (true);2Ui.widget_12->legend->setfont (Qfont ("Helvetica",9));3 Qpen pen;4 qstringlist linenames;//Set the text of the legend5Linenames <<"Lsnone"<<"Lsline"<<"Lsstepleft"<<"Lsstepright"<<"Lsstepcenter"<<"Lsimpulse";6 //add graphs with different line styles:7 for(inti = Qcpgraph::lsnone; I <= Qcpgraph::lsimpulse; ++i)8 {9Ui.widget_12->addgraph ();TenPen.setcolor (Qcolor (Qsin (i *1+1.2) * the+ the, Qsin (i*0.3+0) * the+ the, Qsin (i*0.3+1.5) * the+ the)); OneUi.widget_12->graph ()Setpen (pen); AUi.widget_12->graph ()->setname (linenames.at (i-qcpgraph::lsnone)); -Ui.widget_12->graph ()Setlinestyle ((qcpgraph::linestyle) i);//Set Linear -Ui.widget_12->graph ()->setscatterstyle (Qcpscatterstyle (Qcpscatterstyle::sscircle,5) ///Set each node data drawing style, default is empty, here is set as Hollow circle the //Generate Data: -qvector<Double> x ( the), Y ( the); - for(intj =0; J < the; ++j) - { +X[J] = j/15.0*5*3.14+0.01; -Y[J] =7* Qsin (X[j])/x[j]-(i-qcpgraph::lsnone) *5+ (qcpgraph::lsimpulse) *5+2; + } AUi.widget_12->graph ()setData (x, y); atUi.widget_12->graph ()->rescaleaxes (true);//Axis Adaptive - } - //Zoom out a bit: -Ui.widget_12->yaxis->scalerange (1.1, ui.widget_12->yaxis->range (). Center ()); -Ui.widget_12->xaxis->scalerange (1.1, ui.widget_12->xaxis->range (). Center ()); - //Set blank axis lines: inUi.widget_12->xaxis->setticks (false);//x axis does not display the scale -Ui.widget_12->yaxis->setticks (true);//y Axis Display scale toUi.widget_12->xaxis->setticklabels (false);//x axis does not display text +Ui.widget_12->yaxis->setticklabels (true);//y axis display text - //Make top right axes clones of bottom left axes: theUi.widget_12->axisrect ()->setupfullaxesbox (); A default axis rectangle configuration, including: The top axis follows the bottom axis synchronization, the right axis follows the left axis synchronization, not just the axis range following synchronization , including text precision, text formatting, axis type, whether to automatically generate ticks, scale spacing, and so on.
V. RELATED articles
Qcustomplot use Share (a) what can be done
Qcustomplot use Share (ii) Source code interpretation
Qcustomplot use Share (iii) figure