2017-04-11 4 views
2

enter image description here안드로이드 - 내가 setFillFormatter을 사용하고 MPAndroidChart

을 사용하여 두 선 사이의 색 채우기,하지만 막을 방법이 없기 때문에 그것은 저와에게, 두 번째 줄 (검은 색)을 교차setfillColor을() 도움이되지 것 두 번째 줄의 Y 값에서 첫 번째 줄 (노란색).

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 값을 찾을 수있는 방법이 있나요 : 나는 이런 식으로 뭔가를 구현하려면? 데이터 세트이 표시됩니다. dataProvider은 getFillLinePosition을 호출 할 때마다 고정 값을 반환합니다.

+1

나는 당신이 할 수 있다고 생각하지 않는 라이브러리에서 제공하는 방법을 사용. 이를 위해서는 사용자 정의 렌더러를 작성해야 할 것입니다. 'LineChartRenderer'를보세요. –

답변

3

고맙습니다. David Rawson이 나를 가리키며 LineChartRenderer입니다. 두 줄 사이의 영역을 채색 할 수 있습니다.

두 가지 주요 변경 사항을 적용해야합니다.

  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를 구현합니다. 인수로 다른 LineDataSet을 통과하는 LineDataSet 중 하나에 MyFillFormatter 설정 활동

    의 끝에서

    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. .

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

enter image description here

관련 문제