前言
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、靖州ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的靖州網(wǎng)站制作公司
在開發(fā)中,特別是銷售企業(yè)內(nèi)部使用的APP,可能會用到數(shù)據(jù)匯總,使用到圖表的功能!本文主要給大家介紹了關(guān)于iOS中PNChart與UITableView聯(lián)動的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧
效果圖
1.點擊chart,tableView對應(yīng)模塊高亮
PNChart提供了一個代理方法,用來處理用戶的點擊事件:
#pragma mark - PNChart Delegate - (void)userClickedOnPieIndexItem:(NSInteger)pieIndex { for (int i = 0; i < self.model.department_sale.count; i++) { CQSaleDetailDepartmentItemModel *model = self.model.department_sale[i]; model.selected = (i == pieIndex); } [self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pieIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; }
2.點擊cell,chart對應(yīng)模塊高亮
PNChart并未提供相應(yīng)方法讓某一模塊高亮,怎么辦?
思路:
雖然PNChart未直接提供讓某一模塊高亮的方法,但是我們可以從用戶點擊模塊高亮那部分代碼入手,看看用戶點擊到模塊高亮是怎樣一個過程。
1.在PNPieChart.m里面找到touchesBegan方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint touchLocation = [touch locationInView:_contentView]; [self didTouchAt:touchLocation]; } }
發(fā)現(xiàn)它調(diào)用了didTouchAt:方法。
2.分析didTouchAt:
- (void)didTouchAt:(CGPoint)touchLocation { CGPoint circleCenter = CGPointMake(_contentView.bounds.size.width/2, _contentView.bounds.size.height/2); CGFloat distanceFromCenter = sqrtf(powf((touchLocation.y - circleCenter.y),2) + powf((touchLocation.x - circleCenter.x),2)); if (distanceFromCenter < _innerCircleRadius) { if ([self.delegate respondsToSelector:@selector(didUnselectPieItem)]) { [self.delegate didUnselectPieItem]; } [self.sectorHighlight removeFromSuperlayer]; return; } CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation]; int index = 0; while (percentage > [self endPercentageForItemAtIndex:index]) { index ++; } if ([self.delegate respondsToSelector:@selector(userClickedOnPieIndexItem:)]) { [self.delegate userClickedOnPieIndexItem:index]; } if (self.shouldHighlightSectorOnTouch) { if (!self.enableMultipleSelection) { if (self.sectorHighlight) [self.sectorHighlight removeFromSuperlayer]; } PNPieChartDataItem *currentItem = [self dataItemForIndex:index]; CGFloat red,green,blue,alpha; UIColor *old = currentItem.color; [old getRed:&red green:&green blue:&blue alpha:&alpha]; alpha /= 2; UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; CGFloat startPercentage = [self startPercentageForItemAtIndex:index]; CGFloat endPercentage = [self endPercentageForItemAtIndex:index]; self.sectorHighlight = [self newCircleLayerWithRadius:_outerCircleRadius + 5 borderWidth:10 fillColor:[UIColor clearColor] borderColor:newColor startPercentage:startPercentage endPercentage:endPercentage]; if (self.enableMultipleSelection) { NSString *dictIndex = [NSString stringWithFormat:@"%d", index]; CAShapeLayer *indexShape = [self.selectedItems valueForKey:dictIndex]; if (indexShape) { [indexShape removeFromSuperlayer]; [self.selectedItems removeObjectForKey:dictIndex]; } else { [self.selectedItems setObject:self.sectorHighlight forKey:dictIndex]; [_contentView.layer addSublayer:self.sectorHighlight]; } } else { [_contentView.layer addSublayer:self.sectorHighlight]; } } }
通過源代碼我們可以發(fā)現(xiàn),用戶點擊chart的時候,將傳入的參數(shù)touchLocation轉(zhuǎn)換成了index,這個index正是代理方法userClickedOnPieIndexItem:所需要的參數(shù)。另外,chart的某一模塊高亮,實際上是addSublayer:,而這個sublayer的屬性也是由index決定的。所以,通過主動調(diào)用一個方法讓chart的某個模塊高亮,關(guān)鍵就是這個index。
這樣的話,就很簡單了。只需把didTouchAt :的后半段代碼提出來,就是我們需要的新方法了:
/** 某一模塊高亮 @param index 高亮模塊的index */ - (void)highlightItemWithIndex:(NSInteger)index { if (self.shouldHighlightSectorOnTouch) { if (!self.enableMultipleSelection) { if (self.sectorHighlight) [self.sectorHighlight removeFromSuperlayer]; } PNPieChartDataItem *currentItem = [self dataItemForIndex:index]; CGFloat red,green,blue,alpha; UIColor *old = currentItem.color; [old getRed:&red green:&green blue:&blue alpha:&alpha]; alpha /= 2; UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; CGFloat startPercentage = [self startPercentageForItemAtIndex:index]; CGFloat endPercentage = [self endPercentageForItemAtIndex:index]; self.sectorHighlight = [self newCircleLayerWithRadius:_outerCircleRadius + 5 borderWidth:10 fillColor:[UIColor clearColor] borderColor:newColor startPercentage:startPercentage endPercentage:endPercentage]; if (self.enableMultipleSelection) { NSString *dictIndex = [NSString stringWithFormat:@"%ld", (long)index]; CAShapeLayer *indexShape = [self.selectedItems valueForKey:dictIndex]; if (indexShape) { [indexShape removeFromSuperlayer]; [self.selectedItems removeObjectForKey:dictIndex]; } else { [self.selectedItems setObject:self.sectorHighlight forKey:dictIndex]; [_contentView.layer addSublayer:self.sectorHighlight]; } } else { [_contentView.layer addSublayer:self.sectorHighlight]; } } }
現(xiàn)在就可以實現(xiàn)點擊cell,chart對應(yīng)模塊高亮了:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { for (int i = 0; i < self.model.department_sale.count; i++) { CQSaleDetailDepartmentItemModel *model = self.model.department_sale[i]; model.selected = (i == indexPath.row); } [self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; // 對應(yīng)的模塊高亮 [self.pieChart highlightItemWithIndex:indexPath.row]; }
修改源碼注意事項
如果你的PNChart是手動拖進去的,修改源碼無所謂;
但如果是用CocoaPods管理的話,就要注意一下了:pod update的時候會覆蓋你寫的代碼。為避免這種事情發(fā)生,你可以指定庫的版本,如:
pod 'PNChart','0.8.9'
pod update的時候,若發(fā)現(xiàn)其版本是指定的版本,就不會更新了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。