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 = 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

效果2:

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

时间: 2024-08-02 13:55:17

Cocos2d3.0 画折线图的相关文章

python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指正. 一.最简单的基本框架如下:已知x,y,画出折线图并保存.此时x和y均为数字. 1 # -*- coding: utf-8 -*- 2 3 import matplotlib.pyplot as plt #引入matplotlib的pyplot子库,用于画简单的2D图 4 import random 5

IOS使用Core-Plot画折线图

关于Core-Plot的配置,大家可以参考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权所有,转载请注明原文转自:http://blog.csdn.net/wildcatlele/article/details/25483923 大家可以到:http://1.wildcat.sinaapp.com/?p=102观看本篇博客更友好的排版格式 或者你英语好也可以参考github上的wiki介绍:https://code.google.com/p/core-

Java画折线图

??? JFreeChart 是开放源代码站点SourceForge.net 上的一个 JAVA 项目,它主要用来各种各样的图表,这些图表包括:饼图.柱状图 ( 普通柱状图以及堆栈柱状图 ).线图.区域图.分布图.混合图.甘特图以及一些仪表盘等等. ??? 应用jfreechart来画图需要两个jar包:jfreechart.jar和jcommon.jar,直接去官网下载就成: https://sourceforge.net/projects/jfreechart/files/ ?? 下载完成后

Matplotlib学习---用matplotlib画折线图(line chart)

这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/births.csv 准备工作:先导入matplotlib和pandas,用pandas读取csv文件,然后创建一个图像和一个坐标轴 import pandas as pd from matplotlib import pyplot as plt birth=p

unity 画折线图,饼型图插件

在unity中画折线图,和饼型图等数据分析图是很困难 的一件事,幸好我找到了一个插件很方便的解决了这件事,效果如下图: 折线图,饼型图,等. 运行效果如下: 百度网盘下载地址:链接:https://pan.baidu.com/s/10oLG1Zmffv7ASWG0pvx05w 提取码:lub1 如果链接失效,请留言. 原文地址:https://blog.51cto.com/14058389/2425723

python的turtle模块画折线图

代码如下: import turtle yValues = [10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t = turtle.Turtle() t.hideturtle() drawLine(t,0,0,300,0) #画x轴 drawLine(t,0,0,0,175) #画y轴 #画折线 for i in range(6): drawLineWithDots(t,40 + (40 * i),15 * yValues[i],40 + (40 * (i+1

Android画折线图、柱状图、饼图(使用achartengine.jar)

自从用了画折线的jar包之后,就不想再用canvas画布去画了,编程就是要站在巨人的肩膀上. 所需要的jar包achartenginejar 折线代码布局文件就不上传了很简单 另一种线的渲染器 扇形图代码 柱状图代码 属性总结部分代码 新测试代码下载地址 所需要的jar包:achartengine.jar 下载地址:http://download.csdn.net/detail/zhengyikuangge/9460642 折线代码(布局文件就不上传了,很简单): package com.exa

gnuplot画折线图

之前尝试用jfreechart画自定义横坐标的折线图或时序图,发现很复杂,后来改用gnuplot了. gnuplot在网上一搜就能找到下载地址. 安装完成后,主要是命令行形式的交互界面,至少比jfreechart进步一些... set xrange [0:240]  #设置x轴范围 set yrange [1:1.5]  #设置y轴范围 set xtics ("0" 0, "60" 60, "120" 120, "180" 1

echarts入门基础,画折线图

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box{ transform: translate(50%,50%); -webkit-transform: translate(-50%,-50%); -moz-transform: transla