2016-09-29 29 views
3

私はバーチャートでバーを選択できるようにしたいのですが、バーを選択するとバーの色が変わってしまいますが、軸ラベル。これを行う方法はありますか?誰かが私を助けてくれますか?MPAndroidChart:x軸ラベルに異なる色を設定できますか?

+0

これら[#139](https://github.com/PhilJay/MPAndroidChart/issues/319)と[#387](HTTPSを参照してください。ここでの出力例です。 com/danielgindi/Charts/issues/387)の問題です。また、これらの質問をここで見てください:http://stackoverflow.com/questions/28632489/mpandroidchart-how-to-set-label-colorここで:http://stackoverflow.com/questions/29888850/mpandroidchart-set- y軸値に基づく異なる色から棒グラフへの棒グラフの表示を可能にする。私はそれらの組み合わせがあなたがそれを得るのを助けてくれることを期待しています(本当に可能かどうかはわかりませんが、そのデータでいくつかの回避策をとることができます)。 –

+0

@ Error404これらのリンクのどれも質問にお答えできません。 –

答えて

4

はいxAxisラベルに異なる色を設定することは可能です。あなたは、以下のようなものをカスタムレンダラを使用する必要があります:colorsが解決色のList<Integer>ある

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

(IDSリソースではない)と同じサイズのように:

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); 
    } 
} 

はこのようにそれを消費しますIDataSetのエントリ数。ハイライト上のバーの色を変更する方法はすでに知っているので、その部分はあなた次第です。通常と同じ方法でcolorsを操作してください。 // githubの:

bar chart with colors matching the bars

+0

おそらく関連http://stackoverflow.com/q/43443787 –

+0

非常に役に立ちました!それを検証するのに長い時間がかかりました。 – rohan

+0

@rohanありがとう!私はあなたに役立ってうれしいです –

関連する問題