Qt implements 3D pie chart-3D pie chart

Source: Internet
Author: User
Tags constant definition
3D pie chartMethod 1: the current implementation effect is to use the drawpie function to draw two groups of slices with the same Upper and Lower spacing and the same startagl and spanagl (the color can be set according to the display). See the following: obviously, this is not enough. The position indicates the area to be repaired. You can use the drawpolygon or drawrect function to draw the area to fix it. The coordinates of each point in the specific repair area can be determined by the elliptic parameter equation x = Acost, y = bsint (a, B are the long and short radius, T is the centrifugal angle) and startagl and spanagl of each slice, as shown in the following figure: Static pie chartEffect. Part of the Code is as follows (for testing, it is messy to write :)): void myclass: paintevent (qpaintevent * event) {// test 3D pie qpainter painter; painter. begin (this); painter. setrenderhint (qpainter: antialiasing, true); // set the edge smooth const int irectx = 300; // left pie POS const int irecty = 300; const int irectw = 280; const int irecth = 200; const double Pi = 3.1415926; const int ispanred = 270; const int ispanaglgreen = 15; const int ispanag Lblue = 30; const int ispanaglyellow = 45; const int istartred = 60; const int istartaglgreen =-(optional); const int istartaglblue =-(-istartaglgreen-ispanaglgreen ); const int counter = istartaglblue + counter; // painter pie qrect rect (irectx, irecty, irectw, irecth); // sector qrectup (irectx, iRectY-20, irectw, irecth) in the lower left ); // qrect rectrt (irectx + 20, irecty, Irectw, irecth); // The Right Bottom slice qrect rectrtup (irectx + 20, iRectY-20, irectw, irecth); // The right top slice int startanglered = istartred * 16; int spananglered = ispanred * 16; int startanglegreen = istartaglgreen * 16; int spananglegreen = ispanaglgreen * 16; int startangleblue = Hangzhou * 16; int spanangleblue = ispanaglblue * 16; int startangleyellow = istartaglblue * 16; int spanangleyellow = ispanagly Ellow * 16; // used for patching // int X0 = irectx + irectw/2 at the bottom of the colored slice center; int Y0 = irecty + irecth/2; int posx = x0 + (INT) irectw * (COS (360-istartred-ispanred) * PI/180)/2; int posy = y0 + (INT) irecth * (sin (360-istartred-ispanred) * PI/180)/2; int posx_1 = x0 + (INT) irectw * (COS (-istartred * PI/180)/2; int posy_1 = y0 + (INT) irecth * (sin (-istartred * PI/180)/2; int posx_2 = x0 + (INT) irectw * (COS (-istartaglblue * PI/180)/2; int posy_2 = y0 + (INT) irecth * (sin (-istartaglblue * PI/180)/2; qpoint point_1 (irectx + irectw/2 + 20, irecty + irecth/2); // down qpoint point_2 (irectx + irectw/2 + 20, irecty + irecth/2-20); // up qpoint point_3 (posx + 20, Posy); qpoint point_4 (posx + 20, Posy-20 ); qpoint points [4] = {point_1, point_2, point_4, point_3}; // fix the position of qpoint point_5 (Pos X_2 + 20, posy_2); qpoint point_6 (posx_2 + 20, posy_2-20); qpoint pots [4] = {point_3, point_4, point_6, point_5 }; // patch the arc parallelogram // painter pie painter. setpen (QT: nopen); painter. setbrush (QT: darkred); painter. drawpie (rect, startanglered, spananglered); // slice in the lower left // slice painter in the lower right. setbrush (QT: darkblue); painter. drawpie (rectrt, startangleblue, spanangleblue); painter. setbrush (QT: darkyellow); Paint Er. drawpie (rectrt, startangleyellow, spanangleyellow); painter. setbrush (QT: darkgreen); painter. drawpie (rectrt, startanglegreen, spananglegreen); // painter. setbrush (qcolor (100,255, 0,255); painter. drawpolygon (points, 4); // patch plane parallelogram painter. setbrush (QT: darkgreen); painter. drawpolygon (Pots, 4); // patch the arc parallelogram painter. setbrush (QT: darkred); // patch the square painter. drawrect (posx-20, Posy-20, 20, 20 );// The four-character painter. drawrect (posx_1-20, posy_1-20, 20, 20); // four-sided shape above // sector painter at the top left. setpen (QT: Black); painter. setbrush (QT: Red); painter. drawpie (rectup, startanglered, spananglered); // sector painter in the upper right corner. setbrush (QT: Blue); painter. drawpie (rectrtup, startangleblue, spanangleblue); painter. setbrush (QT: yellow); painter. drawpie (rectrtup, startangleyellow, spanangleyellow); painter. setbrush (QT: Green); pain Ter. drawpie (rectrtup, startanglegreen, spananglegreen); painter. end () ;}the second method: Draw with QT drawpie. In QT, the painter event is drawn in bytes. If the thickness of the pie chart is set to 20 pixels, you can create 20 slices in 20 cycles, and the speed is very fast. In this way, you do not need to fix them. You can also change the ratio and number of slices of each slice. I have made a class with APIs, for a call, you only need to pass the corresponding parameters (of course, you can add the interface as needed !): Draw3dpiechart. hClass cdraw3dpiechart: Public qwidget {q_object public: cdraw3dpiechart (qwidget * parent = 0); Public :~ Cdraw3dpiechart (void); Public: void setpiepos (INT iposx, int iposy); void setpiesize (INT iwidth, int iheight); void setpiepercent (qlist <double> lstpercent ); void setchartdepth (INT idepth); void setchartdistance (INT idistence); // The distance of left and right ???? Void paintevent (qpaintevent * event); // override private: int m_iposx; int m_iposy; int m_iwidth; int m_iheight; int m_idepth; int m_idistence; qlist <double> m_lstpercent; qlist <int> m_lstspanagl; qlist <int> m_lststartagl ;}; Draw3dpiechart. cpp# Include "draw3dpiechart. H "# include <math. h>/* optional * // * -------------------------- constant definition failed * // * optional */const qcolor cstdownpiecolor [7] = {qcolor (QT: darkred), qcolor (QT:: darkblue), qcolor (QT: darkyellow), qcolor (QT: dark Green), qcolor (QT: darkcyan), qcolor (QT: darkgray), qcolor (QT: darkmagenta)}; const qcolor cstuppiecolor [7] = {qcolor (QT:: red), qcolor (QT: blue), qcolor (QT: yellow), qcolor (QT: green), qcolor (QT: Cyan), qcolor (QT:: Gray), qcolor (QT: magenta)}; // here I have defined seven colors, up to seven cdraw3dpiechart: cdraw3dpiechart (qwidget * parent ): qwidget (parent) {ifprintf ("cdraw3dpiechart: cdraw3dpiechart"); newobject ("cdraw3dpi Echart "); m_iposx = 0; m_iposy = 0; m_iwidth = 0; m_iheight = 0; m_idepth = 0; m_idistence = 0; m_lstpercent.clear (); percent (); m_lststartagl.clear (); setfixedsize (929,530); ofprintf ("cdraw3dpiechart: cdraw3dpiechart")} cdraw3dpiechart ::~ Cdraw3dpiechart (void) {ifprintf ("cdraw3dpiechart ::~ Cdraw3dpiechart "); delobject (" cdraw3dpiechart "); m_lstpercent.clear (); m_lstspanagl.clear (); m_lststartagl.clear (); ofprintf (" cdraw3dpiechart ::~ Authorization ");} void cdraw3dpiechart: setpiepos (INT iposx, int iposy) {ifprintf (" cdraw3dpiechart: setpiepos "); m_iposx = iposx; m_iposy = iposy; ofprintf ("Usage: setpiepos");} void cdraw3dpiechart: setpiesize (INT iwidth, int iheight) {ifprintf ("cdraw3dpiechart: setpiesize"); m_iwidth = iwidth; m_iheight = iheight; ofprintf ("cdraw3dpiechart: setpiesize");} void cdraw3dpiechart: SE Tchartdepth (INT idepth) {ifprintf ("Usage: setchartdepth"); m_idepth = idepth; ofprintf ("Usage: setchartdepth");} void metadata: setchartdistance (INT idistence) {ifprintf ("Usage: setchartdistance"); m_idistence = idistence; ofprintf ("Usage: setchartdistance");} void usage: setpiepercent (qlist <double> lstpercent) {ifprintf ("cdraw3dpiechar T: setpiepercent "); m_lstpercent = lstpercent; ofprintf (" percent: setpiepercent ");} void percent: paintevent (qpaintevent * event) {ifprintf (" percent :: paintevent "); q_unused (event); qpainter painter; painter. begin (this); m_lstspanagl.clear (); m_lststartagl.clear (); m_lststartagl.append (60); If (m_lstpercent.size () <= 0) {return;} For (INT I = 0; I <m_lstpercent.size (); I ++) {M_lstspanagl.append (Ceil (360 * m_lstpercent.at (I); if (I = 0) {continue;} m_lststartagl.append (m_lstspanagl.at (I-1) + m_lststartagl.at (I-1);} painter. setrenderhint (qpainter: antialiasing, true); // set smooth painter. setpen (QT: nopen); // lower part of the loop m_idepth times for (INT I = 0; I <m_idepth; I ++) {qrect rectdown (m_iposx, m_iPosY-i, m_iwidth, m_iheight); For (Int J = 0; j <m_lstspanagl.size (); j ++) {Pai NTER. setbrush (cstdownpiecolor [J]); painter. drawpie (rectdown, m_lststartagl.at (j) * 16, m_lstspanagl.at (j) * 16);} // top part painted once (different colors) qrect rectup (m_iposx, m_iPosY-m_iDepth, m_iwidth, m_iheight); For (INT I = 0; I <m_lstpercent.size (); I ++) {painter. setbrush (cstuppiecolor [I]); painter. drawpie (rectup, m_lststartagl.at (I) * 16, m_lstspanagl.at (I) * 16);} painter. end (); ofprintf ("cdraw3dpiechart: paintevent ") ;}For example, qlist <double> lstpercent; // Number of slice slices and percentage of each slice lstpercent. append (1, 0.1); lstpercent. append (1, 0.3); lstpercent. append (1, 0.2); lstpercent. append (0.4); optional * draw3dpie = new vertex (this); draw3dpie-> setpiepos (200,200); draw3dpie-> setpiesize (280,200); draw3dpie-> setpiepercent (lstpercent ); draw3dpie-> setchartdepth (20); effect: You can change the ratio and slice number based on the number and ratio of lstpercent. OK !!!!

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.