2017-04-11 2 views
2

enter image description hereアンドロイド - 私はsetFillFormatterを使用していますが、停止する方法がないように、第2ライン(黒​​)と交差</strong>私を助けてと、<strong>setfillColor()ないですMPAndroidChart

を使用して、2つのライン間の色を塗りつぶし第2行のY値で第1行(黄色)。 私はこのような何かを実装する:

dataSet.setFillFormatter(new IFillFormatter() { 

      @Override 
      public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) { 
       return //return Y value of the second line for current X of line being filled; 
      } 
     }); 

は、最初の行の各Xのための二行目のY値を検索する方法はありますか?私は両方とも参照してくださいdataSetdataProviderは、getFillLinePositionの各呼び出しで固定値を返します。

+1

私はあなたが図書館で提供された方法を使用していることを行うことができるとは思いません。これを行うにはカスタムレンダラーを作成する必要があります。 'LineChartRenderer'を見てください –

答えて

3

ありがとうDavid Rawsonが私をLineChartRendererに向けてくれました。私は2つの線の間に領域を塗りつぶすことができます。

2つの大きな変更を加える必要があります。

  1. 別の行のデータセットを返すカスタムFillFormatorを実装します。

    public class MyFillFormatter implements IFillFormatter { 
    private ILineDataSet boundaryDataSet; 
    
    public MyFillFormatter() { 
        this(null); 
    } 
    //Pass the dataset of other line in the Constructor 
    public MyFillFormatter(ILineDataSet boundaryDataSet) { 
        this.boundaryDataSet = boundaryDataSet; 
    } 
    
    @Override 
    public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) { 
        return 0; 
    } 
    
    //Define a new method which is used in the LineChartRenderer 
    public List<Entry> getFillLineBoundary() { 
        if(boundaryDataSet != null) { 
         return ((LineDataSet) boundaryDataSet).getValues(); 
        } 
        return null; 
    }} 
    
  2. 囲まれたパスを描画して埋めるためにカスタムLineChartRendererを実装します。活動

    の終わりに

    public class MyLineLegendRenderer extends LineChartRenderer { 
    
    public MyLineLegendRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) { 
        super(chart, animator, viewPortHandler); 
    } 
    
    //This method is same as it's parent implemntation 
    @Override 
    protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) { 
        final Path filled = mGenerateFilledPathBuffer; 
    
        final int startingIndex = bounds.min; 
        final int endingIndex = bounds.range + bounds.min; 
        final int indexInterval = 128; 
    
        int currentStartIndex = 0; 
        int currentEndIndex = indexInterval; 
        int iterations = 0; 
    
        // Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets. 
        do { 
         currentStartIndex = startingIndex + (iterations * indexInterval); 
         currentEndIndex = currentStartIndex + indexInterval; 
         currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex; 
    
         if (currentStartIndex <= currentEndIndex) { 
          generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled); 
    
          trans.pathValueToPixel(filled); 
    
          final Drawable drawable = dataSet.getFillDrawable(); 
          if (drawable != null) { 
    
           drawFilledPath(c, filled, drawable); 
          } else { 
    
           drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha()); 
          } 
         } 
    
         iterations++; 
    
        } while (currentStartIndex <= currentEndIndex); 
    } 
    
    //This is where we define the area to be filled. 
    private void generateFilledPath(final ILineDataSet dataSet, final int startIndex, final int endIndex, final Path outputPath) { 
    
        //Call the custom method to retrieve the dataset for other line 
        final List<Entry> boundaryEntry = ((MyFillFormatter)dataSet.getFillFormatter()).getFillLineBoundary(); 
    
        final float phaseY = mAnimator.getPhaseY();  
        final Path filled = outputPath; 
        filled.reset(); 
    
        final Entry entry = dataSet.getEntryForIndex(startIndex); 
    
        filled.moveTo(entry.getX(), boundaryEntry.get(0).getY()); 
        filled.lineTo(entry.getX(), entry.getY() * phaseY); 
    
        // create a new path 
        Entry currentEntry = null; 
        Entry previousEntry = null; 
        for (int x = startIndex + 1; x <= endIndex; x++) { 
    
         currentEntry = dataSet.getEntryForIndex(x); 
         filled.lineTo(currentEntry.getX(), currentEntry.getY() * phaseY); 
    
        } 
    
        // close up 
        if (currentEntry != null && previousEntry!= null) { 
         filled.lineTo(currentEntry.getX(), previousEntry.getY()); 
        } 
    
        //Draw the path towards the other line 
        for (int x = endIndex ; x > startIndex; x--) { 
         previousEntry = boundaryEntry.get(x); 
         filled.lineTo(previousEntry.getX(), previousEntry.getY() * phaseY); 
        } 
    
        filled.close(); 
    }} 
    
  3. 引数として別のLineDataSetを渡すLineDataSetの一つにMyFillFormatterを設定します。

    lineDataSet2.setFillFormatter(new MyFillFormatter(LineDataSet1)); 
    
    mChart.setRenderer(new MyLineLegendRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler())); 
    

enter image description here

関連する問題

 関連する問題