我通过考虑@ Wingzero的建议找到了一种方法,但主要区别在于,我只是使用触点来找出它是在“标记”上还是在外部。我不知道,如果它的正确方法,但解决的办法是,
-(void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight *)highlight{
//-----------------------------------------------------getting recognizer value
UIGestureRecognizer *recognisedGesture = [chartView.gestureRecognizers objectAtIndex:0];
CGPoint poinOfTouch =[recognisedGesture locationInView:chartView];
CGPoint poinOfMarker =[chartView getMarkerPositionWithEntry:entry highlight:highlight];
if (check if the chartview is BarChartView and if true) {
//-----------------------------------------------------If you want to detect touch/tap only on barchartview's bars
if (poinOfTouch.y > poinOfMarker.y) {
NSLog(@"within the bar area!");
}
else{
NSLog(@"Outside the bar area!");
}
}
else
{
//-----------------------------------------------------If you want to detect touch/tap only on linechartView's markers
//-----------------------------------------------------creating two arrays of x and y points(possible nearby points of touch location)
NSMutableArray *containingXValue = [[NSMutableArray alloc]init];
NSMutableArray *containingYValue = [[NSMutableArray alloc]init];
for (int i =0 ; i<5; i++) {
int roundedX = (poinOfMarker.x + 0.5);
int sumXValuesPositive = roundedX+i;
[containingXValue addObject:[NSNumber numberWithInt:sumXValuesPositive]];
int sumXValuesNegative = roundedX-i;
[containingXValue addObject:[NSNumber numberWithInt:sumXValuesNegative]];
int roundedY = (poinOfMarker.y + 0.5);
int sumYValuesPositive = roundedY+i;
[containingYValue addObject:[NSNumber numberWithInt:sumYValuesPositive]];
int sumYValuesNegative = roundedY-i;
[containingYValue addObject:[NSNumber numberWithInt:sumYValuesNegative]];
}
//-----------------------------------------------------------------------------------------------------------------------------------------
int roundXPointTOuched = (poinOf.x + 0.5);
int roundYPointTOuched = (poinOf.y + 0.5);
//-----------------------------------------------------check if touchpoint exists in the arrays of possible points
if ([containingXValue containsObject:[NSNumber numberWithInt:roundXPointTOuched]] && [containingYValue containsObject:[NSNumber numberWithInt:roundYPointTOuched]])
{
// continue, the click is on marker!!!!
}
else
{
// stop, the click is not on marker!!!!
}
//-----------------------------------------------------------------------------------------------------------------------------------------
}
}
}
编辑:最初的解决方案是仅适用于折线图,现在,如果此相同的情况出现的条形图,你可以用上面的代码来处理它。
男人,我已经跑了一段时间了,感觉真的很棒,有积极的领导。这个问题没有方向,希望这对像我这样的人欢呼!
P.S.我将此标记为答案,以确保它达到必要的:)。谢谢
对不起,我不想突出显示,我只是需要推到另一个视图,如果用户点击图表中的点(绘制点),并忽略外部发生的敲击点。 –
你在问'如何仅允许点击绘制点?',这与高光有关,对吧?您点击该点并突出显示。默认情况下它支持。您只需使用委托“chartValueSelected”来知道哪些数据突出显示。答案已更新。 – Wingzero
但我将如何计算“计算最接近的数据集和xIndex”?我通过使用代码获取了chartValueSelected中的接触点,其中使用代码 'float xVal = [lineChartView getValueByTouchPointWithPt:[gesture locationInView:lineChartView] axis:AxisDependencyLeft]。x;' 你能更具体吗?关于如何以及在哪里我应该定义最大距离?我尽我所能,但无法让它工作,你能解释一下吗? –