• cocos2d-x3.0 柱图


    画柱图就是多画几个矩形之后放在一起,这个实现了基本功能还不够完好假设要用到项目中须要自己改动。

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

    效果图:


    使用类:

    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();
        
        // 'layer' is an autorelease object
        auto layer = HelloWorld::create();
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool 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("hi放大师傅看到是非得失", "huawenxinsong", 20);
    //    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<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 == CC_PLATFORM_WP8) || (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柱图节点类

    //
    //  DJColumn.h
    //  ColumnChart
    //
    //  Created by 杜甲 on 14-5-15.
    //
    //
    
    #ifndef __ColumnChart__DJColumn__
    #define __ColumnChart__DJColumn__
    
    #include "cocos2d.h"
    #include "ui/CocosGUI.h"
    USING_NS_CC;
    
    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 杜甲 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柱图类

    //
    //  DJColumnChart.h
    //  ColumnChart
    //
    //  Created by 杜甲 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 ;  //y轴间距系数
        float leftRatioX;   //x轴左側间距系数
        int maxValue1;    //数据中的最大值
        
        float  layerHeight1 ;  //图离底部的距离
        
    };
    
    #endif /* defined(__ColumnChart__DJColumnChart__) */
    

    DJColumnChart.cpp

    //
    //  DJColumnChart.cpp
    //  ColumnChart
    //
    //  Created by 杜甲 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("名字", 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;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    



  • 相关阅读:
    nRF5 SDK for Mesh(二) Getting started 快速开始
    QT 简单 TCP 通信,发送数据到服务器
    Bluetooth® Low Energy Beacons
    CC2540 低功耗串口, POWER_SAVING 模式 下 串口 0 的使用
    LWIP network interface 网卡 初始化 以 STM32 为例子 后面会有 用 2G 或者4G 模块 用 PPP拨号的 形式 虚拟出网卡 所以先以 这个为 前提
    R 语言入门
    Django 框架
    Windows 下 Django 安装
    windows 下搭建 git 服务器 copssh+git
    python Pystaller 将python文件打包成exe
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10709068.html
Copyright © 2020-2023  润新知