2015-07-11 38 views

回答

3

我沒有想與圖書館混亂,所以我不得不延長几類,並覆蓋一些方法來滿足我的要求。我所做的是如下:

擴展X軸類並覆蓋:

private int mXAxisLabelBackgroundColor = Color.argb(200, 140, 234, 255); 
/** 
* boolen used to enable or disable label background, default is enabled 
*/ 
private boolean mEnableLabelBackground = false; 


public CustomXAxis(){ 
    super(); 
} 

public void setLabelBackgroundColor(int color){ 
    mXAxisLabelBackgroundColor = color; 
} 

public void setLabelBackgroundColor(String colorHex){ 
    mXAxisLabelBackgroundColor = Color.parseColor(colorHex); 
} 

public int getXAxisLabelBackgroundColor(){ 
    return mXAxisLabelBackgroundColor; 
} 

/** 
* Enable/disable X Axis label background, default is disabled. 
* @param enable 
*/ 
public void setDrawLabelBackground(boolean enable){ 
    mEnableLabelBackground = enable; 
} 

/** 
* 
* @return boolean true if drawing label background is enabled otherwise false 
*/ 
public boolean isDrawLabelBackgroundEnabled(){ 
    return mEnableLabelBackground; 
} 

擴展XAxisRenderer並覆蓋drawLabels(帆布C,浮動POS)。在繪製標籤之前繪製一個矩形。

 if (((CustomXAxis)mXAxis).isDrawLabelBackgroundEnabled()) { 
      drawLabelBackground(c); 
     } 

擴展YAXIS和只需添加一個存取和mutator設置高於或低於柵格線的Y軸標籤。

private float mYLabelPosition = 0f; 

public CustomYAxisRight(){ 
    super(AxisDependency.RIGHT); 
} 

/** 
* Sets the label position above or below the gridline. 
* <p>use negative number to set label position above gridline.</p> 
* @param position 
*/ 
public void setYLabelPosition(float position){ 
    mYLabelPosition = position; 
} 

public float getYLabelPosition(){ 
    return mYLabelPosition; 
} 

擴展YAxisRenderer並重寫drawYLabels(帆布C,浮fixedPosition,浮[]的位置的浮式偏移)

c.drawText(text, fixedPosition, positions[i * 2 + 1] + (offset+((CustomYAxisRight)mYAxis).getYLabelPosition()), mAxisLabelPaint); 

我也想顯示網格線爲Y軸標籤0當圖表開始從0.

覆蓋renderGridlines(Canvas c)並在繪製其他線條之前繪製一條線。

if (mYAxis.isStartAtZeroEnabled()) { 
     mTrans.pointValuesToPixel(position); 
     gridLinePath.moveTo(mViewPortHandler.offsetLeft(), mViewPortHandler.contentBottom() - 1f); 
     gridLinePath.lineTo(mViewPortHandler.contentRight(), mViewPortHandler.contentBottom() - 1f); 
     // draw a path because lines don't support dashing on lower android versions 
     c.drawPath(gridLinePath, mGridPaint); 
     gridLinePath.reset(); 
    } 

最後延長線型圖並重寫:

@Override 
protected void init() { 
    super.init(); 
    mAxisRight = new CustomYAxisRight(); 
    mXAxis = new CustomXAxis(); 
    mAxisRendererRight = new CustomYAxisRenderer(mViewPortHandler,mAxisRight,mRightAxisTransformer); 
    mXAxisRenderer = new CustomXAxisRenderer(mViewPortHandler, mXAxis, mLeftAxisTransformer); 
} 

@Override 
public void setViewPortOffsets(final float left, final float top, final float right, final float bottom) { 
    mCustomViewPortEnabled = true; 
    if (((CustomXAxis)mXAxis).isDrawLabelBackgroundEnabled()){ 
     if (bottom == 0){ 
      //we have to leave a space for the labels to draw 
      _bottomOffset = 80f; 
     } 
    } 
    post(new Runnable() { 

     @Override 
     public void run() { 

      mViewPortHandler.restrainViewPort(left, top, right, _bottomOffset); 
      prepareOffsetMatrix(); 
      prepareValuePxMatrix(); 
     } 
    }); 
} 

End result

相關問題