iOS开发:一个可以实时加点的Chart

公司项目有需要用到胎心监测,设计的界面逻辑是需要一个可以表格,可以动态的在上面画上数据。有点类似散点图。找了很多demo发现基本都是一次性加完的,实时的相对比较少,于是就自己写了一个小demo。


胎心监测,动态添加点

demo比较简单,整体的逻辑是,首先建立一个view,后面那个蓝色的当做大的背景。然后传入横坐标和竖坐标数组。

    self.chartview = [[MChartMainView alloc]init];
    self.chartview.frame = CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 200);
    self.chartview.backgroundColor = [UIColor blueColor];
    self.chartview.horizontalArray = @[@" 0",@"",@"10",@"",@"20",@"",@"30",@"",@"40",@"",@"50",@"",@"60",@"",@"70",@"",@"80"];
    self.chartview.verticalArray = @[@"80",@"90",@"100",@"110",@"120",@"130",@"140",@"150",@"160",@"170",@"180",@"190",@"200"];
    [self.view addSubview:self.chartview];

在MChartMainView中,计算出每个小单元格的宽度和高度,因为要放横坐标和竖坐标的数值我这里预留了两个格子的空间。

- (void)drawRect:(CGRect)rect {
    self.chartView = [[MChartView alloc]init];
    self.chartView.horizontalArray = self.horizontalArray;
    self.chartView.verticalArray = self.verticalArray;
    
    //预留两格空位
    self.chartView.averageWidth = self.frame.size.width/(self.horizontalArray.count+1);
    self.chartView.averageHeight = self.frame.size.height/(self.verticalArray.count+1);
    self.chartView.frame = CGRectMake(self.chartView.averageWidth*1.5, self.chartView.averageHeight*0.5, self.frame.size.width-self.chartView.averageWidth*2, self.frame.size.height-self.chartView.averageHeight*2);
    self.chartView.layer.borderColor = [UIColor redColor].CGColor;
    self.chartView.layer.borderWidth = 1.0f;
    self.chartView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.chartView];
    [self addHorizontalLabel];
    [self addVerticalLabel];
}


- (void)addHorizontalLabel {
    for (int i = 0; i<self.horizontalArray.count; i++) {
        [self addSubview:[self label:CGRectMake(self.chartView.averageWidth+self.chartView.averageWidth*i, self.chartView.frame.size.height+self.chartView.frame.origin.y,self.chartView.averageWidth , self.chartView.averageHeight) title:self.horizontalArray[i]]];
    }
}

- (void)addVerticalLabel {
    for (int i = 0; i<self.verticalArray.count; i++) {
        [self addSubview:[self label:CGRectMake(self.chartView.averageWidth*0.2, self.frame.size.height-self.chartView.averageHeight*2-self.chartView.averageHeight*i,self.chartView.averageWidth , self.chartView.averageHeight) title:self.verticalArray[i]]];
    }
}

在确定好内部表格的大小之后,在MChartView中开始绘制线条。

 //画横向线条
    if (self.horizontalArray) {
        for (int i = 0; i<self.horizontalArray.count; i++) {
            double x = _averageWidth*i;
          [self drawLine:CGPointMake(x, self.frame.size.height) endPoint:CGPointMake(x, 0)];
        }
    }
    
    //画竖向线条
    if (self.verticalArray) {
        for (int i = 0; i<self.verticalArray.count; i++) {
            double y = _averageHeight*i;
            [self drawLine:CGPointMake(0, y) endPoint:CGPointMake(self.frame.size.width, y)];
        }
    }  

到这里表格和横竖坐标就画好了。下面是实现动态的添加数值。为了模拟胎心监测仪返回的数值,我这里写了一个定时器,0.25秒返回一次随机数值。

- (void)countDownTimer {
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        self.timer += 0.25;
        [self pointTimer];
        if (self.timer>=10) {
            dispatch_source_cancel(timer);
        }
    });
    dispatch_resume(timer);
}

- (void)pointTimer { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        __block int point = 80;
        point += arc4random() % 100;
        if (point>200) {
            point = 80;
        }
        //因为最终要分析数据,所以这里用了个字典保存。
        [self.pointDic setObject:[NSNumber numberWithInt:point] forKey:[NSString stringWithFormat:@"%.2f",self.timer]];
        //调用添加点的方法
        [self.chartview addPoint:self.timer heartbeat:point];
    });
}

然后在定时器中对数据进行保存的同时,调用添加点的方法,横向数值与纵向数值。

 [self.chartview addPoint:self.timer heartbeat:point];

经过传递后,在MChartView中对点所在位置进行计算。之后调用setNeedsDisplay进行绘制。

/计算点的坐标
- (void)setPoint:(double)time  heartbeat:(NSInteger)number {
    double length = (double)[[self.horizontalArray lastObject] integerValue] - [[self.horizontalArray firstObject] integerValue];
    CGFloat xPoint = time/length*self.frame.size.width;
    [self.xArray addObject:[NSNumber numberWithFloat:xPoint]];
    
    double height = (double)[[self.verticalArray lastObject]integerValue] - [[self.verticalArray firstObject]integerValue];
    double tempNumber = number - [[self.verticalArray firstObject] integerValue];
    CGFloat yPoint = (1-tempNumber/height)*self.frame.size.height;
    [self.yArray addObject:[NSNumber numberWithFloat:yPoint]];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self setNeedsDisplay];
    });
}

到这里就完成了整个demo。
DEMO地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,544评论 25 707
  • 早晨,去高老那里,吃他家传统的五子:发芽的蚕豆,煮熟的紫皮蒜,沾椒盐,自家包的粽子,还有土鸡蛋、香喷喷的包子,按...
    华颜Sharon阅读 419评论 1 5
  • 诗韵邳州同题诗 目录 2018年四月同题诗:鸟儿欢歌的理由 2018年3月同题诗:站台 2018新春贺岁特辑:我家...
    岠山剑客阅读 338评论 0 6
  • 话说在1985年左右吧,独生子女政策正在施行中。年近三十才靠妹妹换了个媳妇的贾平,已经有个五六岁的儿子了。看到附近...
    南良大维阅读 350评论 4 5