iOS-Charts如何仅允许点击绘图点?

时间:2016-03-21 13:02:10

标签: ios uitapgesturerecognizer ios-charts

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

我的问题是,

  

是否有任何默认代码块可以执行此操作?

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

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setStyle(new NotificationCompat.InboxStyle());
builder.setPriority(NotificationCompat.PRIORITY_HIGH);

Uri alarmsound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification); // Works fine
// Uri alarmsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Doesn't work

builder.setSound(alarmsound);
long[] pattern = {1000, 500, 500, 1000};
builder.setVibrate(pattern);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(1, notification);

任何见解都将受到赞赏。

eg : Line chart

2 个答案:

答案 0 :(得分: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 :(得分:1)

它有一个默认的高亮显示逻辑,即计算最接近的dataSet和xIndex,因此我们知道要突出显示哪些数据。

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

荧光笔是一个类,如BarChartHighlighter,ChartHighlighter等。

更新您的评论:

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