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

原文地址: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;
}

cocos2d-x3.0 柱图的更多相关文章

  1. Echarts-柱状图柱图宽度设置

    先看两张图 图中柱图只需要设置series中的坐标系属性barWidth就可以, 这种图柱状图,折叠柱状图都适应 eg: /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组 ...

  2. echarts柱图自定义为硬币堆叠的形式

    看这标题,可能会有一些人不太明白,那么直接上图,就是柱图展示形式如下图(兼容IE8) 要想实现这样展示效果.我们想用echarts直接实现不行的,即使是纹理填充也不可行的,但是我们可以借助echart ...

  3. echarts - 特殊需求实现代码汇总之【柱图】篇

    其实包括饼图.线图在内,和柱图都一样的感觉,他们的配置项基本也是对应的那几个,所以想实现某些相似的效果,只要找到对应的属性就可以了. 1.柱图渐变色设置 还记得上篇线图中的实现是在areaStyle的 ...

  4. d3.js 实现立体柱图

    前言 随着大数据时代的来临,数据可视化的重要性也越来越凸显,那么今天就基于d3.js今天给大家带来可视化基础图表柱图进阶:立体柱图 关于d3.js d3.js是一个操作svg的图表库,d3封装了图表的 ...

  5. BootStrap母版页布局.子页面布局.BootstrapTable.模态框.警告框.html导出tabl生成Excel.HTML生成柱图.饼图.时间控件中文版

    如上就是很多后台管理系统的母版页布局. 左边一列模板.上面一列系统标识. 空白处充填子页面 以ASP.NET MVC为基础 引入bootstrap.js.bootstrap.css body: < ...

  6. echarts 中 柱图 、折线图、柱图层叠

    app.title = '折柱混合'; option = { tooltip: { trigger: 'axis', axisPointer: { type: 'cross', crossStyle: ...

  7. Echarts设置y轴值间隔 以及设置 barWidth : 30,//柱图宽度

    需求:如图,y轴之间的距离太小,这样就太过于拥挤了,现在要修改echarts里面的属性,设置y轴值间隔让图表看上去舒服一些.     其实很多问题,真的只是因为自己没有好好的看文档,很多文档上面都写的 ...

  8. dev控件chart简单实现多图例,双曲线,双柱图,曲线与柱图

    1.效果图 2.数据源代码: ; i < ; i++) { == ) { dt1.Rows.Add( * i); dt2.Rows.Add( * i+); } else { dt1.Rows.A ...

  9. 如何在Cocos2D 1.0 中掩饰一个精灵(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...

随机推荐

  1. EventLog组件读写事件日志

    使用.Net中的EventLog控件使您可以访问或自定义Windows 事件日志,事件日志记录关于重要的软件或硬件事件的信息.通过 EventLog,可以读取现有日志,向日志中写入项,创建或删除事件源 ...

  2. [日常] Apache Order Deny,Allow的用法

    Order Deny,Allow的用法:影响最终判断结果的只有两点:1. order语句中allow.deny的先后顺序,最后的是最优先的:2. allow.deny语句中各自包含的范围. 常用:Or ...

  3. SpringBoot+JPA+cache入门

    在pom.xml中加入如下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  4. MySQL 命令登录

    本地登录 MySQL客户端:https://dev.mysql.com/downloads/installer/ 在本地安装好客户端,配置好环境变量,即可直接在命令提示符中登录 简单点: 需要根据提示 ...

  5. Tomcat启动项目两次

    网上一搜,给出的答案都一样,不外乎:1.删除 Host 标签配置的 appBase="webapps"2.删除 Context 配置 此处这样做:重新添加Tomcat,选择好自己的 ...

  6. 撩课-Java每天5道面试题第24天

    151.springMVC和struts2的区别有哪些? .springmvc的入口是一个servlet即前端控制器(DispatchServlet), 而struts2入口是一个filter过虑器( ...

  7. 【转发】【小程序】微信小程序日常开发中常遇到的错误代码

    还在为看不懂小程序错误状态码纠结吗?这里推荐一篇文章 重要的事情说三遍:原文链接  https://www.cnblogs.com/webonline/p/7528778.html 作者:玩世不恭. ...

  8. ExtJs 4.1.1 文件结构解析

  9. 4.HTML字符集

    1.HTML 中的预留字符必须被替换为字符实体. 一些在键盘上找不到的字符也可以使用字符实体来替换. 在 HTML 中,某些字符是预留的.您不能使用包含这些字符的文本. 在 HTML 中不能使用小于号 ...

  10. 本地sql大文件导入数据库

    mysql中配置my.ini interactive_timeout = 120 wait_timeout = 120 max_allowed_packet = 32M 导入sql运行命令 sourc ...