2016-05-18 6 views
1

対応するX軸とY軸のグラフをプロットしました。android mpchartのY軸ラベルを取得するには?

Y軸ラベルを取得し、Y軸に別のラベルを比較して配置する方法を説明します。例えばのために

:私が得た

Y軸ラベルが1,2,3,4,5というようにです。現在、対応するラベルに割り当てる方法は、

2の場合は低くなります。 10の場合は、高いです。 15の場合、非常に高いです。 、それ以上はすべてが最高です。

私の質問は、Y軸のラベルを取得し、それを変更して割り当てる方法です。 y軸に文字列を使用することはできますか?例えばのために

enter image description here

左右のy軸で

代わりの2、私はY軸上のような低表示する必要があります。表示されたグラフの数値を削除する方法もありますか?すなわち、2.00

ここにコードがあります。

List<BarEntry> entries = new ArrayList<>(); 
    ArrayList<String> labels = new ArrayList<String>(); 
    mDetectiveGraphExecutor = new DetectiveGraphExecutor(this); 
    barChart = (BarChart) findViewById(R.id.chart); 
    YAxis leftAxis = barChart.getAxisRight(); 
    leftAxis.removeAllLimitLines(); 
    barChart.setDescription(""); 
    leftAxis.setEnabled(false); 
    ViewPortHandler handlers = barChart.getViewPortHandler(); 
    handlers.setMaximumScaleX(10); 
entries.add(new BarEntry(activities.size(), ++columnIndex)); //list of datas displaying.. 
     labels.add("Time"); 
     BarDataSet dataset = new BarDataSet(entries, "# Counts"); 
     BarData data = new BarData(labels, dataset); 
     barChart.setData(data); 
+0

Y軸ラベルを数字から文字列に変更するか、数値と文字列をy軸で結合したいのですか? – NezSpencer

+0

その番号を文字列に変更したい。なぜ私が求めているのは、例えば:y軸が5を指している場合、左右のy軸ラベルが低く表示される必要があります。どのようにy軸に "低"を表示するか。 – Shadow

答えて

1

Y軸レンダラーをオーバーライドしようとします。 はここに例を示します

setRendererRightYAxis(new YAxisRenderer(mViewPortHandler, mAxisRight, mRightAxisTransformer) 
    { 
     @Override 
     protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) 
     { 
      ArrayList<String> yLabels = new ArrayList<>(); 
      yLabels.add("10 " + getResources().getString(R.string.low)); 
      yLabels.add("20 " + getResources().getString(R.string.medium)); 
      yLabels.add("30 " + getResources().getString(R.string.high)); 

      for (int i = 0; i < _yLabels.size(); i++) 
      { 
       c.drawText(yLabels.get(i), fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint); 
      } 
     } 
    }); 

別のオプションはちょうどあなたが、Y軸の値を変更が必要な場合は、このコードは

を使用し、あなたの例 -

getAxisRight().setValueFormatter(new YAxisValueFormatter() 
    { 
     @Override 
     public String getFormattedValue(float value, YAxis yAxis) 
     { 
      if(value == 0) 
      { 
       return getResources().getString(R.string.low)); 
      } 
      else if(value == 1) 
      { 
       return getResources().getString(R.string.medium)); 
      } 
      else 
      { 
       return getResources().getString(R.string.high)); 
      } 
     } 
    }); 
0

のためのように、値フォーマッタをオーバーライドすることですYAxis yAxis = water_graph.getAxisLeft(); //左側値の変更の場合

YAxis yAxis = water_graph.getAxisRight(); //右側の値の変更の場合

yAxis.setGranularityEnabled(true); 
yAxis.setValueFormatter(new IAxisValueFormatter() { 
     @Override 
     public String getFormattedValue(float value, AxisBase axis) { 
      String str=""; 
      if(value<5) 
       str="Low"; 
      else if(value<10) 
       str="Medium"; 
      else if(value==10) 
       str="high"; 
      else 
       str="very high"; 
      return str; 
     } 
    }); 
関連する問題