2016-09-29 104 views
3

所以我希望能夠在條形圖中選擇一個條形,當我選擇一個條形時,它會改變條形的顏色(我知道該怎麼做),而且還要改變相應的x軸的顏色,軸標籤。有沒有辦法做到這一點,如果有的話,有人可以幫我嗎?MPAndroidChart:我可以爲X軸標籤設置不同的顏色嗎?

+0

請參閱這些[#139](https://github.com/PhilJay/MPAndroidChart/issues/319)和[#387](HTTPS:// github上。 com/danielgindi/Charts/issues/387)的問題。另外,看看這些問題在這裏:http://stackoverflow.com/questions/28632489/mpandroidchart-how-to-set-label-color和在這裏:http://stackoverflow.com/questions/29888850/mpandroidchart-set-不同顏色對杆在-A-柱形圖基於-ON-y軸值。我期望他們的組合能幫助你獲得它(我不知道它是否真的可行,但也許你可以用這些數據做一些解決方法)。 –

+0

@ Error404這些鏈接都不能幫助您提出問題 –

回答

4

是的,可以爲xAxis標籤設置不同的顏色。你將不得不使用一個自定義渲染,類似下面:

import android.graphics.Canvas; 

import com.github.mikephil.charting.components.XAxis; 
import com.github.mikephil.charting.renderer.XAxisRenderer; 
import com.github.mikephil.charting.utils.MPPointF; 
import com.github.mikephil.charting.utils.Transformer; 
import com.github.mikephil.charting.utils.Utils; 
import com.github.mikephil.charting.utils.ViewPortHandler; 

import java.util.Collections; 
import java.util.List; 

/** 
* Created by rawsond on 29/01/17. 
*/ 

public class ColoredLabelXAxisRenderer extends XAxisRenderer { 

    List<Integer> labelColors; 

    public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) { 
     super(viewPortHandler, xAxis, trans); 
     labelColors = Collections.EMPTY_LIST; 
    } 

    public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans, List<Integer> colors) { 
     super(viewPortHandler, xAxis, trans); 
     this.labelColors = colors; 
    } 

    @Override 
    protected void drawLabels(Canvas c, float pos, MPPointF anchor) { 
     final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); 
     boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled(); 

     float[] positions = new float[mXAxis.mEntryCount * 2]; 

     for (int i = 0; i < positions.length; i += 2) { 

      // only fill x values 
      if (centeringEnabled) { 
       positions[i] = mXAxis.mCenteredEntries[i/2]; 
      } else { 
       positions[i] = mXAxis.mEntries[i/2]; 
      } 
     } 

     mTrans.pointValuesToPixel(positions); 

     for (int i = 0; i < positions.length; i += 2) { 

      float x = positions[i]; 

      if (mViewPortHandler.isInBoundsX(x)) { 

       String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i/2], mXAxis); 
       int color = getColorForXValue(mXAxis.mEntries[i/2]); //added 

       mAxisLabelPaint.setColor(color); 

       if (mXAxis.isAvoidFirstLastClippingEnabled()) { 

        // avoid clipping of the last 
        if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) { 
         float width = Utils.calcTextWidth(mAxisLabelPaint, label); 

         if (width > mViewPortHandler.offsetRight() * 2 
           && x + width > mViewPortHandler.getChartWidth()) 
          x -= width/2; 

         // avoid clipping of the first 
        } else if (i == 0) { 

         float width = Utils.calcTextWidth(mAxisLabelPaint, label); 
         x += width/2; 
        } 
       } 

       drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees); 
      } 
     } 
    } 

    private int getColorForXValue(int index) { 
     if (index >= labelColors.size()) return mXAxis.getTextColor(); 

     if (index < 0) return mXAxis.getTextColor(); 

     return labelColors.get(index); 
    } 
} 

消耗它是這樣的:

mChart.setXAxisRenderer(new ColoredLabelXAxisRenderer(mChart.getViewPortHandler(), mChart.getXAxis(), mChart.getTransformer(AxisDependency.LEFT), colors)); 

其中colors是解決顏色的List<Integer>(不是資源ID)相同大小的IDataSet中的條目數。既然您已經知道如何更改突出顯示欄上的酒吧的顏色,該部分取決於您。就像你平常一樣操縱colors。下面是一個示例輸出:

bar chart with colors matching the bars

+0

可能相關http://stackoverflow.com/q/43443787 –

+0

非常有幫助!對不起,需要很長時間來驗證它。 – rohan

+0

@rohan謝謝!我很高興這對你很有幫助 –