首页 > 代码库 > Cocos2d3.0 画折线图

Cocos2d3.0 画折线图

实现用2dx画折线图,为以后用2dx开发应用做准备

下面记录下使用方法

 auto lineView = DJLineChart::create();
    
    std::vector<float> 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);
    
    
    //设置只有正数为true   有负数为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);

画图类

DJLineChart.h

//
//  DJLineChart.h
//  Test890
//
//  Created by 杜甲 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<float>* firstVec;
    void draw(cocos2d::Renderer *renderer, const kmMat4 &transform, bool transformUpdated);
    double  getMaxValue(std::vector<Point>  vec);
    
    std::vector<Point> pointvec;
    void drawLine(vector<Point> vec,Color4B lineColor,Color4B dotColor);
    
    
    void setData(std::vector<float> data);
    
    
    float spaceRatio ;  //y轴间距系数
    float leftRatioX;   //x轴左侧间距系数
    int maxValue1;    //数据中的最大值
    
    float  layerHeight1 ;  //图离底部的距离
    
    
    
    
    bool m_zfBool;  //是否有负数的判断  true 为只有正数  false 为有正有负
protected:
    void onDraw(const kmMat4 &transform, bool transformUpdated);
    CustomCommand _customCommand;
    
    
};

#endif /* defined(__Test890__DJLineChart__) */

DJLineChart.cpp

//
//  DJLineChart.cpp
//  Test890
//
//  Created by 杜甲 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 = http://www.mamicode.com/getMaxValue(pointvec);>
效果1


效果2:



例子下载:http://download.csdn.net/detail/qqmcy/7271261

Cocos2d3.0 画折线图