2016-03-21 19 views
2

我正在使用iOS图表框架来绘制此图表,我想检测点击或只触摸线条的路径或线条上的小圆圈。iOS-Charts如何仅允许点击绘制点?

我的问题是,

是否有任何默认代码块做到这一点?

我试着比较entry.value与绘制的数组(如下面的代码),但它没有锻炼。

-(void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight *)highlight{ 

     if ([arrayOfPlottedValues containsObject:[NSNumber numberWithInt:(int)entry.value]]) { 
      //Tapped on line path 
     } 
     else{ 
      //Tapped on empty area 
     } 
} 

任何见解将不胜感激。

eg : Line chart

回答

3

我通过考虑@ 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.我将此标记为答案,以确保它达到必要的:)。谢谢

1

它有一个默认的亮点逻辑,即,计算最接近的数据集,xIndex,所以我们知道要突出哪些数据。

您可以自定义此逻辑以限制允许的最小距离。例如定义最大允许距离为10,如果触点距离最近的点> 10,则返回false而不是highlgiht。

荧光笔是一类,像BarChartHighlighter,ChartHighlighter等

对您的评论更新:

当你挖,委托方法被调用,所以你知道哪些数据被突出显示。你的代码似乎很好,但条件代码是黑箱给我。但代表将被确认,所以你只需要担心你的逻辑。

+0

对不起,我不想突出显示,我只是需要推到另一个视图,如果用户点击图表中的点(绘制点),并忽略外部发生的敲击点。 –

+0

你在问'如何仅允许点击绘制点?',这与高光有关,对吧?您点击该点并突出​​显示。默认情况下它支持。您只需使用委托“chartValueSelected”来知道哪些数据突出显示。答案已更新。 – Wingzero

+0

但我将如何计算“计算最接近的数据集和xIndex”?我通过使用代码获取了chartValueSelected中的接触点,其中使用代码 'float xVal = [lineChartView getValueByTouchPointWithPt:[gesture locationInView:lineChartView] axis:AxisDependencyLeft]。x;' 你能更具体吗?关于如何以及在哪里我应该定义最大距离?我尽我所能,但无法让它工作,你能解释一下吗? –