2016-03-21 4 views
2

私はiOSチャートフレームワークを使用してこのチャートをプロットしています。ラインのパス上またはライン上の小さな円上でタップまたはタッチを検出したいと思います。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

答えて

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を返し、高さは戻しません。あなたが強調表示されたデータを知っているので、

あなたがタップ

は、デリゲートメソッドが呼び出されます:

蛍光ペンなどあなたのコメントへの

更新BarChartHighlighter、ChartHighlighter、同じように、クラスです。あなたのコードはうまくいくようですが、条件コードは私にとってブラックボックスです。しかし、デリゲートが確実に呼び出されるので、あなたのロジックについて心配する必要があります。

+0

私はハイライトしたくありません。ユーザーがチャート内のドット(プロッ​​ティングポイント)をタップし、外部で発生するタップを無視すると、別のビューにプッシュする必要がありますポイント。 –

+0

あなたは「プロットされたポイントだけをクリックすることを許可する方法」を要求しています。これはハイライトに関連していますか?あなたはそのドットをタップして強調表示されます。デフォルトでは、これがサポートされています。デリゲート 'chartValueSelected'を使ってどのデータがハイライト表示されているかを知るだけです。回答が更新されました。 – Wingzero

+0

しかし、「最も近いデータセットとxインデックスを計算する」はどのように計算されますか?私は、コードを使用して、 'float xVal = [lineChartView getValueByTouchPointWithPt:[ジェスチャーlocationInView:lineChartView]軸:AxisDependencyLeft]というchartValueSelectedのタッチポイントを取得しました。x; ' もっと具体的に教えてください。どのように&どこで私は最大距離を定義する必要がありますか?私はできること全てを試しましたが、それを働かせることができませんでした、少し説明できますか? –

関連する問題