Cocos2d-x3.0 bar chart

Source: Internet
Author: User

The bar chart is to draw several more rectangles and put them together. The basic functions are not complete enough. If you want to use it, you need to modify it yourself.

Address: http://blog.csdn.net/qqmcy/article/details/25984717

:


Usage class:

HelloWorldScene. cpp

# Include "HelloWorldScene. h "# include" DJColumn. h "# include" DJColumnChart. h "USING_NS_CC; Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize your instanceboo L HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance () -> getVisibleOrigin (); // auto top = Label: createWithSystemFont ); // top-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); // addChild (top); std: vector <float> temp; temp. push_back( 106); temp. push_back( 206); temp. push_back( 305); temp. push_back (152); std: vector <float> temp1; temp1.push _ back (73); temp1.push _ back (126); temp1.push _ back (55 ); temp1.push _ back (203); std: vector <float> vec; vec. push_back (temp); vec. push_back (temp1); std: vector <Color4B> colorVec; colorVec. push_back (Color4B: RED); colorVec. push_back (Color4B: ORANGE); DJColumnChart * djColumnChart = DJColumnChart: create (); djColumnChart-> setColumnValueVec (vec); djColumnChart-> setColumnColorVec (colorVec ); djColumnChart-> setSize (Size (700,700); djColumnChart-> setPosition (Point (200, 10); djColumnChart-> addDJColumnChart (); addChild (djColumnChart); return true ;} void HelloWorld: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = platform) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) MessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); return; # endif Director: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif}

DJColumn. h column chart node class

//// DJColumn. h // ColumnChart // Created by du jia on 14-5-15. /// # ifndef _ ColumnChart _ DJColumn __# define _ ColumnChart _ DJColumn __# include "cocos2d. h "# include" ui/CocosGUI. h "comment; class DJColumn: public ui: Layout {public: CREATE_FUNC (DJColumn); CC_SYNTHESIZE (Color4B, _ djColumnColor, DJColumnColor); CC_SYNTHESIZE (float, _ djColumnValue, DJColumnValue ); CC_SYNTHESIZE (Size, _ djColumnSize, DJColumnSize); virtual bool init (); void draw (cocos2d: Renderer * renderer, const kmMat4 & transform, bool transformUpdated ); void onDraw (const kmMat4 & transform, bool transformUpdated); CustomCommand _ customCommand ;}; # endif/* defined (_ ColumnChart _ DJColumn __)*/

DJColumn. cpp

//// DJColumn. cpp // ColumnChart // Created by du jia on 14-5-15. //// # include "DJColumn. h "bool DJColumn: init () {bool bRet = false; do {CC_BREAK_IF (! Ui: Layout: init (); bRet = true;} while (0); return bRet;} void DJColumn: draw (cocos2d: Renderer * renderer, const kmMat4 & transform, bool transformUpdated) {_ customCommand. init (_ globalZOrder); _ customCommand. func = CC_CALLBACK_0 (DJColumn: onDraw, this, transform, transformUpdated); renderer-> addCommand (& _ customCommand);} void DJColumn: onDraw (const kmMat4 & transform, bool transformUpdated) {kmGLPushMatrix (); kmGLLoadMatrix (& transform); CHECK_GL_ERROR_DEBUG (); DrawPrimitives: drawSolidRect (Point (0, 0), Point (getSize (). width, getSize (). height), Color4F (_ djColumnColor. r, _ djColumnColor. g, _ djColumnColor. b, _ djColumnColor. a); kmGLPopMatrix ();}

DJColumnChart. h bar chart class

//// DJColumnChart. h // ColumnChart // Created by du jia on 14-5-15. /// # ifndef _ ColumnChart _ DJColumnChart __# define _ ColumnChart _ DJColumnChart __# include "cocos2d. h "# include" ui/CocosGUI. h "USING_NS_CC; class DJColumnChart: public ui: Layout {public: CREATE_FUNC (DJColumnChart); virtual bool init (); CC_SYNTHESIZE (std: vector <std :: vector <float>, _ columnValueVec, ColumnValueVec); CC_SYNTHESIZE (std: vector <Color4B>, _ columnColorVec, ColumnColorVec); void addDJColumnChart (); std: string fontName; double getMaxValue (std: vector <float> vec); void draw (cocos2d: Renderer * renderer, const kmMat4 & transform, bool transformUpdated); void onDraw (const kmMat4 & transform, bool transformUpdated); CustomCommand _ customCommand; private: float spaceRatio; // float leftRatioX; // int maxValue1; // float layerHeight1; // distance from the bottom of the graph}; # endif/* defined (_ ColumnChart _ DJColumnChart __)*/

DJColumnChart. cpp

//// DJColumnChart. cpp // ColumnChart // Created by du jia on 14-5-15. //// # include "DJColumnChart. h "# include" DJColumn. h "bool DJColumnChart: init () {bool bRet = false; do {CC_BREAK_IF (! Ui: Layout: init (); fontName = "huawenxinsong"; bRet = true;} while (0); return bRet;} void DJColumnChart: draw (cocos2d :: renderer * renderer, const kmMat4 & transform, bool transformUpdated) {_ customCommand. init (_ globalZOrder); _ customCommand. func = CC_CALLBACK_0 (DJColumnChart: onDraw, this, transform, transformUpdated); renderer-> addCommand (& _ customCommand);} void DJColumnChart: onDraw (const kmMat4 & transform, bool transformUpdated) {kmGLPushMatrix (); kmGLLoadMatrix (& transform); spaceRatio = 0.06f; leftRatioX = 0.1f; std: vector <float> v0 = _ columnValueVec. at (0); std: vector <float> v1 = _ columnValueVec. at (1); double max1 = getMaxValue (v0); double max2 = getMaxValue (v1); double max = max2; if (max1> max2) {max = max1 ;} int maxValue2 = roundf (max/100) * 100; int maxNum = maxValue2/10; DrawPrimitives: setDrawColor4B (0,255,255,255); float layoutHeight = getSize (). height; float layoutWidth = getSize (). width; float betweenDistance = layoutHeight/13; log ("AAAAA % f", betweenDistance); float tempbetweenDistance = betweenDistance; for (int I = 0; I <11; I ++) {Point bPoint = Point (44, tempbetweenDistance); Point ePoint = Point (layoutWidth-30, tempbetweenDistance); DrawPrimitives: drawLine (bPoint, ePoint); ui :: text * text = ui: Text: create (StringUtils: format ("% d", maxNum * I), fontName, 20 ); text-> setPosition (Point (18, tempbetweenDistance); text-> setTextHorizontalAlignment (TextHAlignment: CENTER); text-> setSize (Size (layoutWidth * 0.08, 20 )); addChild (text); // Label * labelY = Label: createWithSystemFont (StringUtils: format ("% d", I), "huawenxinsong", 20 ); // labelY-> setAlignment (TextHAlignment: RIGHT); // labelY-> setPosition (Point (2, tempbetweenDistance); // addChild (labelY ); tempbetweenDistance + = betweenDistance;} kmGLPopMatrix ();} void DJColumnChart: addDJColumnChart () {float betweenDistance = getSize (). height/12; log ("betweenDistance = % f", betweenDistance); std: vector <float> temp = _ columnValueVec. at (0); std: vector <float> temp1 = _ columnValueVec. at (1); Color4B c1 = _ columnColorVec. at (0); Color4B c2 = _ columnColorVec. at (1); for (int I = 0; I <temp1.size (); I ++) {DJColumn * djColumn = DJColumn: create (); djColumn-> setSize (Size (20, temp1.at (I) * 53.846153/30); djColumn-> setDJColumnColor (c1); // djColumn-> setAnchorPoint (Point :: ANCHOR_MIDDLE_BOTTOM); djColumn-> setPosition (Point (60 * I + getSize (). width * 0.1 + 20, betweenDistance); addChild (djColumn) ;}for (int I = 0; I <temp. size (); I ++) {DJColumn * djColumn = DJColumn: create (); djColumn-> setSize (Size (20, temp. at (I) * 53.846153/30); djColumn-> setDJColumnColor (c2); // djColumn-> setAnchorPoint (Point: ANCHOR_MIDDLE_BOTTOM ); djColumn-> setPosition (Point (60 * I + getSize (). width * 0.1, betweenDistance); addChild (djColumn); ui: Text * nameText = ui: Text: create ("name", fontName, 20 ); nameText-> setPosition (Point (60 * I + getSize (). width * 0.1 + 25, 40); addChild (nameText) ;}} double DJColumnChart: getMaxValue (std: vector <float> vec) {double maxY = 8; for (int I = 0; I <vec. size (); I ++) {float num = vec. at (I); if (maxY <abs (num) {maxY = abs (num) ;}return maxY ;}



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.