From d2c273799c9f403ed74c0324c8a1241f742c02b8 Mon Sep 17 00:00:00 2001 From: xiwang wang Date: Tue, 10 Oct 2017 14:48:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B0=8F=E7=BB=86?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHChartDemo/JHChart/JHTableChart.m | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/JHChartDemo/JHChart/JHTableChart.m b/JHChartDemo/JHChart/JHTableChart.m index 1cb37ef..da8a644 100644 --- a/JHChartDemo/JHChart/JHTableChart.m +++ b/JHChartDemo/JHChart/JHTableChart.m @@ -114,8 +114,10 @@ -(void)drawRect:(CGRect)rect{ [self addSubview:header]; } } - CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(_tableWidth, _tableChartTitleItemsHeight) textFont:_tableTitleFont.pointSize aimString:_tableTitleString]; + if (drawText) { + //需要绘制文字时再计算宽度 + CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(_tableWidth, _tableChartTitleItemsHeight) textFont:_tableTitleFont.pointSize aimString:_tableTitleString]; [self drawText:_tableTitleString context:context atPoint:CGRectMake(CGRectGetWidth(self.frame)/2.0 - size.width / 2, _beginSpace + _tableChartTitleItemsHeight/2 - size.height / 2.0, _tableWidth, _tableChartTitleItemsHeight) WithColor:_tableTitleColor font:_tableTitleFont]; } @@ -146,10 +148,8 @@ -(void)drawRect:(CGRect)rect{ NSLog(@"第%ld列 宽度 为 %f\n",i,wid); - CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, self.colTitleHeight) textFont:self.colTitleFont.pointSize aimString:_colTitleArr[i]]; - BOOL drawText = true; - + // 上层调用代理传进自定义的view if ([_delegate respondsToSelector:@selector(tableChart:viewForPropertyAtColumn:contentSize:)]) { UIView *proView = [_delegate tableChart:self viewForPropertyAtColumn:i contentSize:CGSizeMake(wid-2, self.colTitleHeight-2)]; if (proView) { @@ -167,10 +167,13 @@ -(void)drawRect:(CGRect)rect{ //绘制列分割线 [self drawLineWithContext:context andStarPoint:P_M(lastX, _lastY) andEndPoint:P_M(lastX, _lastY + _bodyHeight) andIsDottedLine:NO andColor:_lineColor]; } + //跳出本次循环,下面绘制文字代码不执行 continue; } - if (i==0) { - + + CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, self.colTitleHeight) textFont:self.colTitleFont.pointSize aimString:_colTitleArr[i]]; + + if (i == 0) { NSArray *firArr = [_colTitleArr[0] componentsSeparatedByString:@"|"]; if (firArr.count>=2) { [self drawLineWithContext:context andStarPoint:P_M(lastX, _lastY) andEndPoint:P_M(lastX + wid, _lastY + self.colTitleHeight) andIsDottedLine:NO andColor:_lineColor]; @@ -191,7 +194,7 @@ -(void)drawRect:(CGRect)rect{ [self drawText:_colTitleArr[i] context:context atPoint:CGRectMake(lastX + wid / 2.0 - size.width / 2, _lastY + self.colTitleHeight / 2.0 -size.height / 2.0, wid, self.colTitleHeight) WithColor:[self colorForColTitle:i] font:self.colTitleFont]; } lastX += wid; - if (i==_colTitleArr.count - 1) { + if (i == _colTitleArr.count - 1) { }else{ [self drawLineWithContext:context andStarPoint:P_M(lastX, _lastY) andEndPoint:P_M(lastX, _lastY + _bodyHeight) andIsDottedLine:NO andColor:_lineColor]; @@ -240,8 +243,6 @@ -(void)drawRect:(CGRect)rect{ for (NSInteger n = 0; n<[rowItems count]; n++) { [self drawLineWithContext:context andStarPoint:P_M(lastX, _lastY + (n+1) * perItemsHeightByMaxCount) andEndPoint:P_M(lastX + wid, _lastY + (n+1) * perItemsHeightByMaxCount) andIsDottedLine:NO andColor:_lineColor]; - - CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, perItemsHeightByMaxCount) textFont:_bodyTextFont.pointSize aimString:rowItems[n]]; BOOL drawText = true; if ([_delegate respondsToSelector:@selector(tableChart:viewForContentAtRow:column:subRow:contentSize:)]) { CGSize contentSize = CGSizeMake(wid - 2, _minHeightItems*model.maxCount/[rowItems count] - 2); @@ -255,6 +256,7 @@ -(void)drawRect:(CGRect)rect{ } if (drawText) { + CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, perItemsHeightByMaxCount) textFont:_bodyTextFont.pointSize aimString:rowItems[n]]; [self drawText:rowItems[n] context:context atPoint:CGRectMake(lastX + wid / 2 - size.width / 2.0, _lastY + (n+1) * perItemsHeightByMaxCount - perItemsHeightByMaxCount / 2.0 - size.height / 2.0, size.width, size.height) WithColor:_bodyTextColor font:_bodyTextFont]; } From 9db56276066d45321a7206d4aa6d26030937041d Mon Sep 17 00:00:00 2001 From: xiwang wang Date: Tue, 10 Oct 2017 16:56:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?JHTableChart=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E7=82=B9=E5=87=BB=E4=BA=8B=E4=BB=B6=E5=8F=8A=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHChartDemo/JHChart/JHTableChart.h | 4 + JHChartDemo/JHChart/JHTableChart.m | 114 ++++++++++++++++++++++++++--- 2 files changed, 107 insertions(+), 11 deletions(-) diff --git a/JHChartDemo/JHChart/JHTableChart.h b/JHChartDemo/JHChart/JHTableChart.h index 501636d..8ead77f 100644 --- a/JHChartDemo/JHChart/JHTableChart.h +++ b/JHChartDemo/JHChart/JHTableChart.h @@ -22,6 +22,10 @@ - (UIView *)tableChart:(JHTableChart *)chart viewForContentAtRow:(NSInteger)row column:(NSInteger)column subRow:(NSInteger)subRow contentSize:(CGSize)contentSize; - (UIView *)tableChart:(JHTableChart *)chart viewForPropertyAtColumn:(NSInteger)column contentSize:(CGSize)contentSize; - (UIView *)tableChart:(JHTableChart *)chart viewForTableHeaderWithContentSize:(CGSize)contentSize; + +///单击事件 +- (void)didClickedTableChart:(JHTableChart *)chart content:(NSString *)content indexString:(NSString *)indexString; + @end @interface JHTableChart : JHChart /** diff --git a/JHChartDemo/JHChart/JHTableChart.m b/JHChartDemo/JHChart/JHTableChart.m index da8a644..c7758a2 100644 --- a/JHChartDemo/JHChart/JHTableChart.m +++ b/JHChartDemo/JHChart/JHTableChart.m @@ -10,11 +10,18 @@ #import "JHTableDataRowModel.h" @interface JHTableChart () -@property (nonatomic,assign)CGFloat tableWidth; +@property (nonatomic,assign) CGFloat tableWidth; @property (nonatomic,assign) CGFloat tableHeight; @property (nonatomic,assign) CGFloat lastY; @property (nonatomic,assign) CGFloat bodyHeight; -@property (nonatomic,strong)NSMutableArray * dataModelArr; +@property (nonatomic,strong) NSMutableArray * dataModelArr; + +//用于保存每个field的frame +//表头和属性头 +@property (nonatomic,strong) NSMutableArray * titleFieldFrameArr; +//数据内容 +@property (nonatomic,strong) NSMutableArray * dataFieldFrameArr; + @end @implementation JHTableChart @@ -81,6 +88,9 @@ -(void)setDataArr:(NSArray *)dataArr{ * @param rect */ -(void)drawRect:(CGRect)rect{ + //绘制前初始化数组 + _dataFieldFrameArr = [NSMutableArray array]; + _titleFieldFrameArr = [NSMutableArray array]; CGContextRef context = UIGraphicsGetCurrentContext(); /* 表格四周线条 */ @@ -121,6 +131,8 @@ -(void)drawRect:(CGRect)rect{ [self drawText:_tableTitleString context:context atPoint:CGRectMake(CGRectGetWidth(self.frame)/2.0 - size.width / 2, _beginSpace + _tableChartTitleItemsHeight/2 - size.height / 2.0, _tableWidth, _tableChartTitleItemsHeight) WithColor:_tableTitleColor font:_tableTitleFont]; } + // 将表头frame添加进去 + [_titleFieldFrameArr addObject:NSStringFromCGRect(CGRectMake(_beginSpace+1, _beginSpace+1, _tableWidth-2, _tableChartTitleItemsHeight-2))]; _lastY = _beginSpace + _tableChartTitleItemsHeight; } @@ -141,13 +153,12 @@ -(void)drawRect:(CGRect)rect{ CGFloat lastX = _beginSpace; //属性头绘制 for (NSInteger i = 0; i<_colTitleArr.count; i++) { - - - CGFloat wid = (hasSetColWidth?[_colWidthArr[i] floatValue]:_tableWidth / _colTitleArr.count); NSLog(@"第%ld列 宽度 为 %f\n",i,wid); + //将属性头frame添加进去 + [_titleFieldFrameArr addObject:NSStringFromCGRect(CGRectMake(lastX+1, _lastY+1, wid-2, self.colTitleHeight-2))]; BOOL drawText = true; // 上层调用代理传进自定义的view if ([_delegate respondsToSelector:@selector(tableChart:viewForPropertyAtColumn:contentSize:)]) { @@ -231,6 +242,8 @@ -(void)drawRect:(CGRect)rect{ CGFloat lastX = _beginSpace; + // 新建data中每行数组 + NSMutableArray *rowFrameArr = [NSMutableArray array]; for (NSInteger j = 0; j< model.dataArr.count; j++) { id rowItems = model.dataArr[j]; CGFloat wid = (hasSetColWidth?[_colWidthArr[j] floatValue]:_tableWidth / _colTitleArr.count); @@ -238,15 +251,17 @@ -(void)drawRect:(CGRect)rect{ //绘制列数组元素 if ([rowItems isKindOfClass:[NSArray class]]) {//列元素为数组时 + // 一行中某个有多个分行,用于存储该行所有分行frame + NSMutableArray *rowItemsFrameArr = [NSMutableArray array]; + CGFloat perItemsHeightByMaxCount = model.maxCount * _minHeightItems / [rowItems count]; /* 具体某一列有多个元素时 */ for (NSInteger n = 0; n<[rowItems count]; n++) { [self drawLineWithContext:context andStarPoint:P_M(lastX, _lastY + (n+1) * perItemsHeightByMaxCount) andEndPoint:P_M(lastX + wid, _lastY + (n+1) * perItemsHeightByMaxCount) andIsDottedLine:NO andColor:_lineColor]; + CGSize contentSize = CGSizeMake(wid - 2, _minHeightItems*model.maxCount/[rowItems count] - 2); BOOL drawText = true; if ([_delegate respondsToSelector:@selector(tableChart:viewForContentAtRow:column:subRow:contentSize:)]) { - CGSize contentSize = CGSizeMake(wid - 2, _minHeightItems*model.maxCount/[rowItems count] - 2); - UIView *cacheView = [_delegate tableChart:self viewForContentAtRow:i column:j subRow:n contentSize:contentSize]; if (cacheView) { cacheView.frame = CGRectMake(lastX+1, _lastY+2 + n * _minHeightItems*model.maxCount/[rowItems count] , contentSize.width, contentSize.height); @@ -259,16 +274,17 @@ -(void)drawRect:(CGRect)rect{ CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, perItemsHeightByMaxCount) textFont:_bodyTextFont.pointSize aimString:rowItems[n]]; [self drawText:rowItems[n] context:context atPoint:CGRectMake(lastX + wid / 2 - size.width / 2.0, _lastY + (n+1) * perItemsHeightByMaxCount - perItemsHeightByMaxCount / 2.0 - size.height / 2.0, size.width, size.height) WithColor:_bodyTextColor font:_bodyTextFont]; } - + //将一行中某个多分行frame添加进去 + [rowItemsFrameArr addObject:NSStringFromCGRect(CGRectMake(lastX+1, _lastY+2 + n * _minHeightItems*model.maxCount/[rowItems count] , contentSize.width, contentSize.height))]; } - + [rowFrameArr addObject:rowItemsFrameArr]; }else{//绘制列元素 非数组 CGSize size = [self sizeOfStringWithMaxSize:CGSizeMake(wid, model.maxCount * _minHeightItems) textFont:_bodyTextFont.pointSize aimString:rowItems]; - + CGSize contentSize = CGSizeMake(wid - 2, _minHeightItems * model.maxCount - 2); BOOL drawText = true; if ([_delegate respondsToSelector:@selector(tableChart:viewForContentAtRow:column:subRow:contentSize:)]) { - CGSize contentSize = CGSizeMake(wid - 2, _minHeightItems * model.maxCount - 2); + UIView *cacheView = [_delegate tableChart:self viewForContentAtRow:i column:j subRow:0 contentSize:contentSize]; if (cacheView) { cacheView.frame = CGRectMake(lastX+1, _lastY+1, contentSize.width, contentSize.height); @@ -280,9 +296,14 @@ -(void)drawRect:(CGRect)rect{ if (drawText) { [self drawText:rowItems context:context atPoint:CGRectMake(lastX + wid / 2 - size.width / 2.0, _lastY + model.maxCount * _minHeightItems - model.maxCount * _minHeightItems / 2.0 - size.height / 2.0, size.width, size.height) WithColor:_bodyTextColor font:_bodyTextFont]; } + //将一行中没有分行的frame添加进去 + [rowFrameArr addObject:NSStringFromCGRect(CGRectMake(lastX+1, _lastY+1, contentSize.width, contentSize.height))]; } lastX += wid; } + + //将每行中的所有frame添加进去 + [_dataFieldFrameArr addObject:rowFrameArr]; _lastY += model.maxCount * _minHeightItems; @@ -292,6 +313,77 @@ -(void)drawRect:(CGRect)rect{ } +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ + UITouch *touch = touches.anyObject; + CGPoint p = [touch locationInView:self]; + BOOL contained = false; + //int x = -1, y = -1, z = -1; + NSLog(@"point %@",NSStringFromCGPoint(p)); + for (int i = 0; i < _titleFieldFrameArr.count; i++) { + CGRect rect = CGRectFromString(_titleFieldFrameArr[i]); + //NSLog(@"rect %@",_titleFieldFrameArr[i]); + if (CGRectContainsPoint(rect, p)) { + NSLog(@"title contained %d",i); + contained = true; + if ([_delegate respondsToSelector:@selector(didClickedTableChart:content:indexString:)]) { + [_delegate didClickedTableChart:self content:(i == 0? _tableTitleString: _colTitleArr[i - 1]) indexString:(i == 0? nil: [NSString stringWithFormat:@"{%d}",i - 1])]; + } + return; + } + } + + if (!contained) { + for (int i = 0; i < _dataFieldFrameArr.count; i++) { + id item = _dataFieldFrameArr[i]; + if ([item isKindOfClass:[NSString class]]) { + CGRect rect = CGRectFromString(item); + if (CGRectContainsPoint(rect, p)) { + NSLog(@"data contained %d",i); + contained = true; + if ([_delegate respondsToSelector:@selector(didClickedTableChart:content:indexString:)]) { + [_delegate didClickedTableChart:self content:_dataArr[i] indexString:[NSString stringWithFormat:@"{%d}",i]]; + } + return; + } + } else if ([item isKindOfClass:[NSArray class]]) { + NSArray *items = (NSArray *)item; + for (int j = 0; j < items.count; j++) { + id subItem = items[j]; + if ([subItem isKindOfClass:[NSString class]]) { + CGRect rect = CGRectFromString(items[j]); + if (CGRectContainsPoint(rect, p)) { + NSLog(@" data contained %d %d",i,j); + contained = true; + if ([_delegate respondsToSelector:@selector(didClickedTableChart:content:indexString:)]) { + [_delegate didClickedTableChart:self content:_dataArr[i][j] indexString:[NSString stringWithFormat:@"{%d,%d}",i,j]]; + } + return; + } + } else if ([subItem isKindOfClass:[NSArray class]]) { + NSArray *subItems = (NSArray *)subItem; + for (int k = 0; k < subItems.count; k++) { + CGRect rect = CGRectFromString(subItems[k]); + if (CGRectContainsPoint(rect, p)) { + NSLog(@" data contained %d %d %d",i,j,k); + contained = true; + if ([_delegate respondsToSelector:@selector(didClickedTableChart:content:indexString:)]) { + [_delegate didClickedTableChart:self content:_dataArr[i][j][k] indexString:[NSString stringWithFormat:@"{%d,%d,%d}",i,j,k]]; + } + return; + } + } + } + } + } + } + } + + if (!contained) { + NSLog(@"not contained"); + } + +} + /** * 绘图前数据构建 */ From 6b5d9e81e0c1dca379ff13ca71b76eab38956b94 Mon Sep 17 00:00:00 2001 From: xiwang wang Date: Tue, 10 Oct 2017 16:56:22 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHChartDemo/JHShowController.m | 69 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/JHChartDemo/JHShowController.m b/JHChartDemo/JHShowController.m index 41eae8e..0832625 100644 --- a/JHChartDemo/JHShowController.m +++ b/JHChartDemo/JHShowController.m @@ -375,8 +375,19 @@ - (void)showTableView{ table.backgroundColor = [UIColor whiteColor]; /* Data source array, in accordance with the data from top to bottom that each line of data, if one of the rows of a column in a number of cells, can be stored in an array of */ table.dataArr = @[ - @[@"2.4L优越版",@"2016皓白标准漆蓝棕",@[@"鸽子白",@"鹅黄",@"炫彩绿"],@[@"4"],@"价格十分优惠,相信市场会非常好"], - @[@"2.4专业版",@[@"2016皓白标准漆蓝棕",@"2016晶黑珠光漆黑",@"2016流沙金珠光漆蓝棕"],@[@"鸽子白",@"鹅黄",@"炫彩绿",@"彩虹多样色"],@[@"4",@"5",@"3"],@"性价比还不错,内部配置较为不错,值得入手"] ]; + @[ + @"2.4L优越版", + @"2016皓白标准漆蓝棕", + @[@"鸽子白",@"鹅黄",@"炫彩绿"], + @[@"4"], + @"价格十分优惠,相信市场会非常好"], + @[ + @"2.4专业版", + @[@"2016皓白标准漆蓝棕",@"2016晶黑珠光漆黑",@"2016流沙金珠光漆蓝棕"], + @[@"鸽子白",@"鹅黄",@"炫彩绿",@"彩虹多样色"], + @[@"4",@"5",@"3"], + @"性价比还不错,内部配置较为不错,值得入手" + ] ]; table.delegate = self; /* show */ [table showAnimation]; @@ -523,31 +534,35 @@ -(void)columnChart:(JHColumnChart*)chart columnItem:(JHColumnItem *)item didClic NSLog(@"%@",indexPath); } --(UIView *)tableChart:(JHTableChart*)chart viewForContentAtRow:(NSInteger)row column:(NSInteger)column subRow:(NSInteger)subRow contentSize:(CGSize)contentSize{ - - if (1) { - UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; - vi.backgroundColor = [UIColor greenColor]; - return vi; - } - return nil; -} - --(UIView *)tableChart:(JHTableChart*)chart viewForPropertyAtColumn:(NSInteger)column contentSize:(CGSize)contentSize{ - if (1) { - UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; - vi.backgroundColor = [UIColor greenColor]; - return vi; - } - return nil; -} - --(UIView *)tableChart:(JHTableChart*)chart viewForTableHeaderWithContentSize:(CGSize)contentSize{ - +//-(UIView *)tableChart:(JHTableChart*)chart viewForContentAtRow:(NSInteger)row column:(NSInteger)column subRow:(NSInteger)subRow contentSize:(CGSize)contentSize{ +// +// if (1) { +// UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; +// vi.backgroundColor = [UIColor greenColor]; +// return vi; +// } +// return nil; +//} +// +//-(UIView *)tableChart:(JHTableChart*)chart viewForPropertyAtColumn:(NSInteger)column contentSize:(CGSize)contentSize{ +// if (1) { +// UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; +// vi.backgroundColor = [UIColor greenColor]; +// return vi; +// } // return nil; - UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; - vi.backgroundColor = [UIColor greenColor]; - return vi; +//} +// +//-(UIView *)tableChart:(JHTableChart*)chart viewForTableHeaderWithContentSize:(CGSize)contentSize{ +// +//// return nil; +// UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)]; +// vi.backgroundColor = [UIColor greenColor]; +// return vi; +//} + +- (void)didClickedTableChart:(JHTableChart *)chart content:(NSString *)content indexString:(NSString *)indexString{ + NSLog(@"content: %@ index:%@",content,indexString); } @@ -558,6 +573,4 @@ -(UIView *)tableChart:(JHTableChart*)chart viewForTableHeaderWithContentSize:(CG - - @end