iOS-图表如何只允许点击标绘点?

iOS-Charts how to allow clicks only on plotted points?

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

我的问题是,

Is there any default code block to do this?

我尝试将 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

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

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

Highlighter 是 class,如 BarChartHighlighter、ChartHighlighter 等

更新您的评论:

当您点击时,将调用委托方法,因此您知道突出显示了哪些数据。您的代码看起来不错,但是条件代码对我来说是黑盒子。但是委托肯定会被调用,所以你只需要担心你的逻辑。

我通过考虑@Wingzero 的建议找到了一种方法,但主要区别在于,我只是使用触摸点来确定它是在 "marker" 上还是在"marker" 之外。我不确定它是否正确,但解决方案是,

-(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。我将其标记为答案只是为了确保它达到了需要的:)。谢谢