Qcustomplot use Share (ii) Source code interpretation

Source: Internet
Author: User

I. Overview of the header file

Starting from this article, we will formally enter into the qcustomplot of practical learning, first we first to learn the Qcustomplot of the class diagram, If you download the Qcustomplot source of the students can go to the Qcustomplot directory documentation/ Qcustomplot search for a file named index.html, open it in the browser, and you can find the class diagram of the library. 1, is a possible constituent form of a Qcustomplot class diagram.

    1. A chart (Qcustomplot): contains one or more layers, one or more items (used to show some fixed elements, such as text, line segments, and so on), one or more elements that can be drawn, a layout
    2. One layer (Qcplayer): Contains the basic element (qcplayerable)
    3. A qcpabstractitem: Contains one or more location information
    4. An axis rectangle (qcpaxisrect): Contains multiple axes, one legend class (multiple legend items)

Figure 1 Chart composition

In a Qcustomplot class diagram, the most important and most useful is the qcplayerable element, which is inherited from almost all elements except Qcplayer.

    1. Qcpabstractplottable: Drawing elements, including: Line chart (qcpgraph), Curve diagram (Qcpcurve), histogram (Qcpbars), Qcpstatibox (box diagram), Qcpcolormap (Chromatogram), Qcpfinancial (Financial chart)
    2. Qcpabstractitem: Marked items, including: Line (Qcpitemstraightline), Segment (Qcpitemline), Curve (Qcpitemcurve), Rectangle (qcpitemrect), ellipse ( Qcpitemellipse), text (qcpitemtext), small ball (qcpitemtracer), picture (Qcpitempixmap)
    3. Layout items (qcplayoutelement): Layout items (qcpabstractlegenditem), axis rectangles (qcpaxisrect)
    4. Gridlines (Qcpgrid): One grid line for each axis
    5. Axis (Qcpaxis): An axis rectangle contains four axes, up or down four axes.

Figure 2 You can draw an element class diagram

Second a simple example

The following code is a simple candle chart code, source I was stripped down from the official website, just to let everyone have a preliminary understanding, it was English comments I replaced with Chinese, and then added my own personal some understanding, run results 3 shown

1Customplot->legend->setvisible (true);2  3 //generate 2 Random candlestick data, the first one is the candlestick data, the second one is the US line data4 intn = -;5qvector<Double>Time (n), value1 (n), value2 (n);6Qdatetime start = Qdatetime (Qdate ( the,6, One));7 Start.settimespec (QT::UTC);8 DoubleStartTime =start.totime_t ();9 DoubleBinsize =3600* -;//1 days of dataTentime[0] =StartTime; Onevalue1[0] = -; Avalue2[0] = -; -Qsrand (9)///Generate random numbers to the specified seed, then the generated random number is the same, so the results are unchanged after each run -  for(intI=1; i<n; ++i) the { -Time[i] = StartTime +3600*i; -Value1[i] = value1[i-1] + (Qrand ()/(Double) rand_max-0.5)*Ten; -Value2[i] = value2[i-1] + (Qrand ()/(Double) rand_max-0.5)*3; + } -   + //Initialize a candlestick pointer: AQcpfinancial *candlesticks =NewQcpfinancial (Customplot->xaxis, customplot->YAxis); atCandlesticks->setname ("Candlestick"); -Candlesticks->Setchartstyle (Qcpfinancial::cscandlestick);//Set  the chart type to candlestick chart -Candlesticks->data ()Set(QCPFINANCIAL::TIMESERIESTOOHLC (Time, value1, Binsize, StartTime));//Set Data -Candlesticks->setwidth (binsize*0.9);//Set the drawing width of each data item -Candlesticks->settwocolored (true);//Set whether to display two colors -Candlesticks->setbrushpositive (Qcolor (245,245,245) );//Set up > Draw Brush inCandlesticks->setbrushnegative (Qcolor ( +, +, +)); //Set up Collection < draw brush  -Candlesticks->setpenpositive (Qpen (Qcolor (0,0,0))); //Set collection > Open brushes  toCandlesticks->setpennegative (Qpen (Qcolor (0,0,0))); //Set collection > Open brushes  +   - //Initialize a US line chart pointer: theQcpfinancial *OHLC =NewQcpfinancial (Customplot->xaxis, customplot->YAxis); *Ohlc->setname ("OHLC"); $Ohlc->Setchartstyle (QCPFINANCIAL::CSOHLC); //Set chart type to US line  Panax NotoginsengOhlc->data ()Set(QCPFINANCIAL::TIMESERIESTOOHLC (Time, value2, binsize/3.0, StartTime)); In order to differentiate betweenCandle ChartShow -Ohlc->setwidth (binsize*0.2); theOhlc->settwocolored (true); +   A //Create an axis rectangle theQcpaxisrect *volumeaxisrect =NewQcpaxisrect (customplot); +Customplot->plotlayout ()->addelement (1,0, volumeaxisrect); -Volumeaxisrect->setmaximumsize (Qsize (Qwidgetsize_max, -)); $Volumeaxisrect->axis (Qcpaxis::atbottom)->setlayer ("axes"); $Volumeaxisrect->axis (Qcpaxis::atbottom)->grid ()->setlayer ("Grid"); - //Set the axis rectangle property of your own construction -Customplot->plotlayout ()->setrowspacing (0); theVolumeaxisrect->setautomargins (qcp::msleft| Qcp::msright|qcp::msbottom); -Volumeaxisrect->setmargins (Qmargins (0,0,0,0));Wuyi //Create a histogram of two colors theCustomplot->setautoaddplottabletolegend (false);//Whether the legend is automatically generated -Qcpbars *volumepos =NewQcpbars (Volumeaxisrect->axis (qcpaxis::atbottom), volumeaxisrect->axis (qcpaxis::atleft)); WuQcpbars *volumeneg =NewQcpbars (Volumeaxisrect->axis (qcpaxis::atbottom), volumeaxisrect->axis (qcpaxis::atleft)); -  for(intI=0; i<n/5; ++i) About { $   intv = qrand ()%20000+qrand ()%20000+qrand ()%20000-10000*3; -(V <0? Volumeneg:volumepos)->adddata (starttime+3600*5.0*i, Qabs (v));//Constructing random Data - } -Volumepos->setwidth (3600*4); AVolumepos->Setpen (qt::nopen); +Volumepos->setbrush (Qcolor ( -, the, the)); theVolumeneg->setwidth (3600*4); -Volumeneg->Setpen (qt::nopen); $Volumeneg->setbrush (Qcolor ( the, -, -)); the   the //Set the x axis of the axis rectangle that you construct and the axis rectangle in Qcustomplot (the default is to generate one) x-axis synchronization, and the two axes always show the same coordinate range theConnect (Customplot->xaxis, SIGNAL (rangechanged (Qcprange)), volumeaxisrect->axis (qcpaxis::atbottom), SLOT (SetRange (Qcprange))); theConnect (Volumeaxisrect->axis (qcpaxis::atbottom), SIGNAL (rangechanged (Qcprange)), customplot->Xaxis, SLOT (SetRange (Qcprange))); - //constructs a new axis scale calculation class inQsharedpointer<qcpaxistickerdatetime> Datetimeticker (Newqcpaxistickerdatetime); theDatetimeticker->Setdatetimespec (QT::UTC); theDatetimeticker->setdatetimeformat ("DD. MMMM"); AboutVolumeaxisrect->axis (Qcpaxis::atbottom)Setticker (Datetimeticker);//Give yourself a new scale calculation class for the x-axis of the axis rectangle you construct theVolumeaxisrect->axis (Qcpaxis::atbottom)->setticklabelrotation ( the); theCustomplot->xaxis->Setbasepen (qt::nopen); theCustomplot->xaxis->setticklabels (false);//Do not display axis text +Customplot->xaxis->setticks (false);//do not display axes (this interface is not friendly to implement, I will be specific to the next article)  -Customplot->xaxis->Setticker (Datetimeticker); //Assign the default axis rectangle to the x-axis a new scale calculation class  theCustomplot->rescaleaxes ();BayiCustomplot->xaxis->scalerange (1.025, customplot->xaxis->range (). Center ()); theCustomplot->yaxis->scalerange (1.1, customplot->yaxis->range (). Center ()); the   - //set two axis rectangles to align right and left -Qcpmargingroup *group =NewQcpmargingroup (customplot); theCustomplot->axisrect ()->setmargingroup (qcp::msleft|qcp::msright, group); theVolumeaxisrect->setmargingroup (qcp::msleft| Qcp::msright, group);

Figure 3 Candlestick Run

Third, sample download

About Qcustomplot series of explanations, I may be divided into 7 articles to introduce, respectively, is Qcustomplot use Share (ii) source interpretation, Qcustomplot use Share (three) chart polyline, parametric curve, candlestick chart, histogram, area chart, Qcustomplot use Share (iv) Qcpabstractitem, qcustomplot use Share (v) layout, qcustomplot use Share (vi) axis grid lines and Qcustomplot use Share (vii) layer. When the layer is finished, I will release a final demo for everyone to download ...

Iv. related articles

Qcustomplot use Share (a) what can be done

Qcustomplot use Share (ii) Source code interpretation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.