Use 2dx to draw a line chart to prepare for the development and application of 2dx in the future.
The usage method is recorded below
Auto lineView = DJLineChart: create (); std: vector
Vec; vec. push_back( 130); vec. push_back( 520); vec. push_back (60); vec. push_back (0); vec. push_back( 140); vec. push_back( 100); vec. push_back (30); vec. push_back (80); // set only the positive value to true and the negative value to false lineView-> m_zfBool = true; lineView-> setData (vec); lineView-> setPosition (Point (0, visibleRect: center (). y); lineView-> setContentSize (Size (VisibleRect: getVisibleRect (). size. width, VisibleRect: getVisibleRect (). size. height * 2/3); addChild (lineView );
Drawing Class
DJLineChart. h
//// DJLineChart. h // Test890 // Created by du jia on 14-4-28. /// # ifndef _ Test890 _ DJLineChart __# define _ Test890 _ DJLineChart __# include "cocos2d. h "# include" VisibleRect. h "using namespace cocos2d; using namespace std; class DJLineChart: public Layer {public: CREATE_FUNC (DJLineChart); virtual bool init (); CC_SYNTHESIZE (int, _ hInterval, HInterval ); CC_SYNTHESIZE (int, _ vInterval, VInterval); Vector
* FirstVec; void draw (cocos2d: Renderer * renderer, const kmMat4 & transform, bool transformUpdated); double getMaxValue (std: vector
Vec); std: vector
Pointvec; void drawLine (vector
Vec, Color4B lineColor, Color4B dotColor); void setData (std: vector
Data); float spaceRatio; // float leftRatioX; // int maxValue1; // float layerHeight1; // bool m_zfBool distance from the bottom of the graph; // determines whether a negative number exists. true indicates that only positive numbers exist. false indicates that positive and negative protected: void onDraw (const kmMat4 & transform, bool transformUpdated ); customCommand _ customCommand;}; # endif/* defined (_ Test890 _ DJLineChart __)*/
DJLineChart. cpp
//// DJLineChart. cpp // Test890 // Created by du jia on 14-4-28. //// # include "DJLineChart. h "bool DJLineChart: init () {bool bRet = false; do {CC_BREAK_IF (! Layer: init (); // auto layerColor = LayerColor: create (Color4B: GREEN); // addChild (layerColor); bRet = true ;} while (0); return bRet;} void DJLineChart: draw (cocos2d: Renderer * renderer, const kmMat4 & transform, bool transformUpdated) {_ customCommand. init (1); _ customCommand. func = CC_CALLBACK_0 (DJLineChart: onDraw, this, transform, transformUpdated); renderer-> addCommand (& _ customCommand);} void DJLineChart: onDraw (const kmMat4 & transform, bool transformUpdated) {kmGLPushMatrix (); kmGLLoadMatrix (& transform); int maxValue = getMaxValue (pointvec); int maxValue2 = roundf (maxValue/100) * 100; maxValue1 = maxValue2/10; spaceRatio = 0.06f; // y axis spacing coefficient leftRatioX = 0.1f; // int fontSize = 20; string fontName = StringUtils: format ("Thonburi "); size layerSize = this-> getContentSize (); layerHeight1 = 30; float layerHeight = layerHeight1; float layerWidth = layerSize. width; /********************** draw Y axis labels ************* * **********************/DrawPrimitives:: setDrawColor4B (0,255,255,255); for (int I = 0; I <11; I ++) {// use the relative value layerWidth * 0.05f for future adaptation Point bPoint = Point (layerWidth * leftRatioX, layerHeight); Point ePoint = Point (layerWidth * 0.95f, layerHeight ); label * label = nullptr; if (m_zfBool) {label = Label: createWithSystemFont (StringUtils: format ("% d", maxValue1 * I ). c_str (), fontName. c_str (), fontSize);} else {label = Label: createWithSystemFont (StringUtils: format ("% d", maxValue1 * 2 * I-maxValue2 ). c_str (), fontName. c_str (), fontSize);} label-> setPosition (Point (layerWidth * 0.05f, layerHeight); addChild (label); DrawPrimitives: drawLine (bPoint, ePoint ); layerHeight + = layerSize. height * spaceRatio ;} /********************** draw Y axis labels ************* * ******************** END **/drawLine (pointvec, color4B (0,255,255,255), Color4B (255, 0,255,255); CHECK_GL_ERROR_DEBUG (); // end draw kmGLPopMatrix ();} void DJLineChart: drawLine (vector
Vec, Color4B lineColor, Color4B dotColor) {Size layerSize = this-> getContentSize (); float layerWidth = layerSize. width; float tempWidth = layerSize. height * spaceRatio; float tempwidth1 = 0; float tempHeight1 = maxValue1; if (m_zfBool) {} else {tempwid22. height * spaceRatio * 5; tempHeight1 * = 2;} double ratio = tempWidth/tempHeight1; **************** * *****/std:: vector
: Iterator beforePoint; std: vector
: Iterator currentPoint; beforePoint = vec. begin (); DrawPrimitives: setDrawColor4B (lineColor. r, lineColor. g, lineColor. b, lineColor. a); for (currentPoint = vec. begin () + 1; currentPoint! = Vec. end (); currentPoint ++) {Point bPoint = * beforePoint; bPoint = Point (bPoint. x + layerWidth * leftRatioX, bPoint. y * ratio + layerHeight1 + tempwid2); Point ePoint = * currentPoint; ePoint = Point (ePoint. x + layerWidth * leftRatioX, ePoint. y * ratio + layerHeight1 + tempwid2); DrawPrimitives: drawLine (bPoint, ePoint); beforePoint = currentPoint ;} **************** ***** End ************ * **********************************/beforePoint = vec. begin (); DrawPrimitives: setDrawColor4B (dotColor. r, dotColor. g, dotColor. b, dotColor. a); Point bPoint = * beforePoint; bPoint = Point (bPoint. x + layerWidth * leftRatioX, bPoint. y * ratio + layerHeight1 + tempwid2); DrawPrimitives: drawSolidCircle (bPoint, 8, CC_DEGREES_TO_RADIANS (90), 50, 1.0f, 1.0f ); Auto labelX = Label: createWithSystemFont (StringUtils: format ("% d", 1 ). c_str (), "Thonburi", 20); labelX-> setPosition (Point (bPoint. x, 0); this-> addChild (labelX); int I = 2; for (currentPoint = vec. begin () + 1; currentPoint! = Vec. end (); currentPoint ++) {Point ePoint = * currentPoint; ePoint = Point (ePoint. x + layerWidth * leftRatioX, ePoint. y * ratio + layerHeight1 + tempwid2); DrawPrimitives: drawSolidCircle (ePoint, 8, Week (90), 50, 1.0f, 1.0f); auto labelX = Label: createWithSystemFont (StringUtils:: format ("% d", I ). c_str (), "Thonburi", 20); labelX-> setPosition (Point (ePoint. x, 0); this-> addChild (labelX); I ++ ;} *************** * END */} void DJLineChart:: setData (std: vector
Data) {std: vector
: Iterator it; int I = 0; for (it = data. begin (); it! = Data. end (); it ++) {float f = * it; pointvec. push_back (Point (50 * (I + 1), f); log ("% f", f); I ++ ;}} double DJLineChart: getMaxValue (std:: vector
Vec) {double maxY = 8; for (int I = 0; I <vec. size (); I ++) {float num = vec. at (I ). y; if (maxY <abs (num) {maxY = abs (num) ;}return maxY ;}
Effect 1
Effect 2:
Download: http://download.csdn.net/detail/qqmcy/7271261