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;
}
cocos2d-x3.0 柱图的更多相关文章
- Echarts-柱状图柱图宽度设置
先看两张图 图中柱图只需要设置series中的坐标系属性barWidth就可以, 这种图柱状图,折叠柱状图都适应 eg: /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组 ...
- echarts柱图自定义为硬币堆叠的形式
看这标题,可能会有一些人不太明白,那么直接上图,就是柱图展示形式如下图(兼容IE8) 要想实现这样展示效果.我们想用echarts直接实现不行的,即使是纹理填充也不可行的,但是我们可以借助echart ...
- echarts - 特殊需求实现代码汇总之【柱图】篇
其实包括饼图.线图在内,和柱图都一样的感觉,他们的配置项基本也是对应的那几个,所以想实现某些相似的效果,只要找到对应的属性就可以了. 1.柱图渐变色设置 还记得上篇线图中的实现是在areaStyle的 ...
- d3.js 实现立体柱图
前言 随着大数据时代的来临,数据可视化的重要性也越来越凸显,那么今天就基于d3.js今天给大家带来可视化基础图表柱图进阶:立体柱图 关于d3.js d3.js是一个操作svg的图表库,d3封装了图表的 ...
- BootStrap母版页布局.子页面布局.BootstrapTable.模态框.警告框.html导出tabl生成Excel.HTML生成柱图.饼图.时间控件中文版
如上就是很多后台管理系统的母版页布局. 左边一列模板.上面一列系统标识. 空白处充填子页面 以ASP.NET MVC为基础 引入bootstrap.js.bootstrap.css body: < ...
- echarts 中 柱图 、折线图、柱图层叠
app.title = '折柱混合'; option = { tooltip: { trigger: 'axis', axisPointer: { type: 'cross', crossStyle: ...
- Echarts设置y轴值间隔 以及设置 barWidth : 30,//柱图宽度
需求:如图,y轴之间的距离太小,这样就太过于拥挤了,现在要修改echarts里面的属性,设置y轴值间隔让图表看上去舒服一些. 其实很多问题,真的只是因为自己没有好好的看文档,很多文档上面都写的 ...
- dev控件chart简单实现多图例,双曲线,双柱图,曲线与柱图
1.效果图 2.数据源代码: ; i < ; i++) { == ) { dt1.Rows.Add( * i); dt2.Rows.Add( * i+); } else { dt1.Rows.A ...
- 如何在Cocos2D 1.0 中掩饰一个精灵(六)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...
随机推荐
- 算法:QQ等级换算成皇冠太阳星星月亮
/// <summary> /// 等级换算成图标分布 /// 以QQ的形式计算 /// 2^(2*0) /1 /// 2^(2*1) /4 /// 2^(2*2) /16 ...
- Code Signal_练习题_almostIncreasingSequence
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly incr ...
- 洛谷P3437 [POI2006]TET-Tetris 3D(二维线段树 标记永久化)
题意 题目链接 Sol 二维线段树空间复杂度是多少啊qwqqq 为啥这题全网空间都是\(n^2\)还有人硬要说是\(nlog^2n\)呀.. 对于这题来说,因为有修改操作,我们需要在外层线段树上也打标 ...
- HDU4336 Card Collector(期望 状压 MinMax容斥)
题意 题目链接 \(N\)个物品,每次得到第\(i\)个物品的概率为\(p_i\),而且有可能什么也得不到,问期望多少次能收集到全部\(N\)个物品 Sol 最直观的做法是直接状压,设\(f[sta] ...
- 学web前端一定要这样学,不然学完找不到工作哭都来不及!
因为工作原因,经常关注有关互联网行业的最新动态.这不,刚送走了高考,又迎来了每年的毕业季,看到好多人都说今年的前端工作不好找,很多童鞋简历投了一大堆,也没有回应,发现连实习的机会都没有,好不容易去面试 ...
- windows域渗透实战
测试环境 域控: 192.168.211.130 已经控制的机器: 192.168.211.133 获取网络信息 查看机器的网络信息 ipconfig /all # 查看 网卡信息,获取dns 服务器 ...
- 3.CSS使用基础(2)
目录 一.CSS 链接 二.CSS 列表样式(ul) 三.CSS Table(表格) 四.盒子模型 五.CSS Border(边框) 六.CSS 轮廓(outline)属性 七.CSS Margin( ...
- Jenkins操作,实现增删改查
Jenkins的版本是:Jenkins2.138.1 实现的操作接口: using System; using System.Collections.Generic; namespace iHRPub ...
- Flask配置文件和 路由系统
debug = True开启debug模式 当你的代码在界面增减之后不用刷新界面自动更新 app.logger.error("这是error模式") app.logger.info ...
- django 板块动态切换
需求:在同一页面的不同板块上可以实现动态切换,使用一个view实现,具体如下图所示,点击phy显示物理机列表,点击vm显示虚机列表,phy.vm对应的url均是动态生成: ...