【问题标题】:iOS-Charts how to allow clicks only on plotted points?iOS-Charts 如何只允许点击绘制点?
【发布时间】:2016-03-21 13:02:10
【问题描述】:

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

我的问题是,

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

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

【问题讨论】:

    标签: ios uitapgesturerecognizer ios-charts


    【解决方案1】:

    我通过考虑@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!!!!
    
            }
            //-----------------------------------------------------------------------------------------------------------------------------------------
    }
    
    }
    

    }

    编辑:最初的解决方案只适用于折线图,现在如果条形图出现同样的情况,你可以用上面的代码自己处理。

    伙计,我已经跑了一段时间了,能获得积极的领先感觉真的很棒。这个问题还没有方向,希望对我这样的人有帮助,加油!

    附:我将此标记为答案只是为了确保它达到需要的:)。谢谢

    【讨论】:

      【解决方案2】:

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

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

      Highlighter 是一个类,如 BarChartHighlighter、ChartHighlighter 等。

      更新您的评论:

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

      【讨论】:

      • 对不起,我没看错?我不想突出显示,我只需要推送到另一个视图,如果用户点击图表中的点(绘制点)并忽略点外发生的抽头。
      • 您在问“如何只允许点击绘制点?”,这与突出显示有关,对吧?你点击了那个点,它就会突出显示。默认情况下它支持。您只需使用委托chartValueSelected 即可了解突出显示的数据。答案已更新。
      • 但是我将如何计算“计算最接近的数据集和 xIndex”?我通过使用代码float xVal = [lineChartView getValueByTouchPointWithPt:[gesture locationInView:lineChartView] axis:AxisDependencyLeft].x; 获得了chartValueSelected 中的接触点,您能更具体一点吗?关于我应该如何以及在哪里定义最大距离?我尽我所能,但无法让它工作,你能解释一下吗?
      • 解释太多了。我建议您浏览从 tapGestureHandler 到荧光笔的代码,您将看到它如何将像素点转换为数据点并计算最接近的点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多