码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d3.0 画折线图

时间:2014-04-30 22:47:39      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   drawprimitives   折线   画图   drawsolidcircle   

实现用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 = getMaxValue(pointvec);
    
    int maxValue2 = roundf(maxValue / 100)* 100 ;
     maxValue1 = maxValue2  / 10;
    
    
     spaceRatio = 0.06f;  //y轴间距系数
     leftRatioX = 0.1f;   //x轴左侧间距系数
    
    
    int fontSize = 20;
    string fontName = StringUtils::format("Thonburi");
    
    Size layerSize = this->getContentSize();
    
    
      layerHeight1 = 30;
    float layerHeight = layerHeight1;
    float layerWidth = layerSize.width;
    
    /***********************画y轴标签*************************************/
     DrawPrimitives::setDrawColor4B(0, 255, 255, 255);
    for (int i = 0; i < 11; i++) {
        //采用相对数值layerWidth* 0.05f  为了以后适配
        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;
        
        
    }
     /***********************画y轴标签***********************************END**/
    
    
    
    drawLine(pointvec, Color4B(0, 255, 255, 255),Color4B(255, 0, 255, 255));
    
    
    
   
    
    CHECK_GL_ERROR_DEBUG();
    
    //end draw
    kmGLPopMatrix();
    
}

void DJLineChart::drawLine(vector<Point> vec,Color4B lineColor,Color4B dotColor)
{
    Size layerSize = this->getContentSize();
    
   
    float layerWidth = layerSize.width;
    
    
    
    float tempWidth = layerSize.height * spaceRatio;
    float tempWidth2 = 0;
    
    float tempHeight1 = maxValue1  ;
    
    if (m_zfBool) {
        
        
    }else
    {
        tempWidth2 = layerSize.height * spaceRatio * 5;
        tempHeight1 *= 2 ;
    }
    double  ratio = tempWidth/tempHeight1;
    
    
    /**********************画线**********************/
    std::vector<Point>::iterator beforePoint;
    std::vector<Point>::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 +tempWidth2);
        
        Point ePoint  = *currentPoint;
        ePoint = Point(ePoint.x + layerWidth* leftRatioX, ePoint.y * ratio + layerHeight1 +tempWidth2);
        
        DrawPrimitives::drawLine(bPoint, ePoint);
        
        beforePoint = currentPoint;
        
    }
     /**********************画线*********************end*/
    
    
    
    
    
    /********************画点和x轴标签***********************************************/
    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 +tempWidth2);
    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 + tempWidth2);
        
        DrawPrimitives::drawSolidCircle(ePoint, 8, CC_DEGREES_TO_RADIANS(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++;
    }

     /********************画点和x轴标签*********************************************END**/
    
}



void DJLineChart::setData(std::vector<float> data)
{
    std::vector<float>::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<Point> 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;
}

效果1

mamicode.com,码迷

效果2:

mamicode.com,码迷


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

Cocos2d3.0 画折线图

标签:cocos2d-x   drawprimitives   折线   画图   drawsolidcircle   

原文地址:http://blog.csdn.net/qqmcy/article/details/24723209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!