2017-01-04 112 views
2

YAxisのラベルをギャップをあけて持ち上げるにはどうすればいいですか?つまり、下の写真のような値から始めますか?私がオフセットを使用しようとすると、Y軸のラベル値がY軸データに対して誤ってプロットされます。ここでMPAndroidchartのY軸ラベルの間隔を変更するにはどうすればよいですか?

a stock price line chart with the YAxis labels starting from $412.66

私のコードは、これまでのところです:

public void setChartProperties() { 
     YAxis rightAxis = chart.getAxisRight(); 
     YAxis leftAxis = chart.getAxisLeft(); 
     XAxis xAxis = chart.getXAxis(); 
     chart.getLegend().setEnabled(false); 
     chart.getDescription().setEnabled(false); 
     chart.setDrawBorders(false); 
     chart.setPinchZoom(false); 
     chart.setAutoScaleMinMaxEnabled(true); 
     chart.setExtraOffsets(0, 0, 0, 0); 
     xAxis.setLabelCount(6, true); 
     xAxis.setGranularity(1f); 
     xAxis.setDrawGridLines(false); 
     xAxis.setPosition(XAxisPosition.BOTTOM); 
     xAxis.setAvoidFirstLastClipping(true); 
     leftAxis.setPosition(YAxisLabelPosition.INSIDE_CHART); 
     leftAxis.setDrawLabels(true); 
     leftAxis.setSpaceBottom(60); 
     leftAxis.setDrawGridLines(true); 
     leftAxis.setLabelCount(3, true); 
     leftAxis.setCenterAxisLabels(true); 
     leftAxis.setDrawGridLines(false); 
     rightAxis.setEnabled(false); 
     xAxis.setAvoidFirstLastClipping(true); 
     dataSet.setColor(R.color.graphLineColor); 
    } 

そして、ここでは私のチャートがどのように見えるかのスクリーンショットです。

a chart with the YAxis labels starting from the correct value but with incorrect yValues

+0

は、これまで任意のコードをお持ちですか? – tar

+0

上記のコードを追加しました。ありがとう! –

+1

特定のレベルを超えるyValuesのラベルを表示したいだけですか?すべてのラベルを表示しないようにします。 –

答えて

1

これは私がすべての値を保つだけのラベルを変更したかったので、IAxisValueFormatterの実施を通じて達成されました:

public class MyValueFormatter implements IAxisValueFormatter { 

    private final float cutoff; 
    private final DecimalFormat format; 

    public MyValueFormatter(float cutoff) { 
     this.cutoff = cutoff; 
     this.format = new DecimalFormat("###,###,###,##0.00"); 
    } 

    @Override 
    public String getFormattedValue(float value, AxisBase axis) { 
     if (value < cutoff) { 
      return ""; 
     } 

     return "$" + format.format(value); 
    } 
} 

そして、私はそれを使用して消費:

leftAxis.setValueFormatter(new MyValueFormatter(yMin)); 

yMinは、先に定義したとおりである。

その後、3210
private float yMin = 0; 

とグラフの最小yValueが渡された割り当てられた。

a chart with the YAxis labels starting from the minimum yValue as per OP's requirement

+0

良いスポッティング!よくやった –

関連する問題