2017-01-03 12 views
0

私は4つのグラフを作成するためにMPAndroidChartを使用しています。私は初心者であり、コードを複製せずにグラフを作成する方法がわかりません。AndroidのJava、重複コード

例グラフ2

mChart2 = (LineChart)findViewById(R.id.resultGraph2); 
mChart2.setOnChartValueSelectedListener(this); 
mChart2.getDescription().setEnabled(false); 
mChart2.setTouchEnabled(true); 
mChart2.setDragDecelerationFrictionCoef(0.9f); 
mChart2.setDragEnabled(true); 
mChart2.setScaleEnabled(true); 
mChart2.setDrawGridBackground(true); 
mChart2.setHighlightPerDragEnabled(true); 
mChart2.setPinchZoom(true); 
mChart2.setBackgroundColor(Color.WHITE); 
setData2(); 
mChart2.animateX(2500); 
mChart2.getLegend().setEnabled(false); 
mChart2.setExtraLeftOffset(5); 
mChart2.setExtraBottomOffset(10); 

例グラフ4

mChart4 = (LineChart)findViewById(R.id.resultGraph4); 
mChart4.setOnChartValueSelectedListener(this); 
mChart4.getDescription().setEnabled(false); 
mChart4.setTouchEnabled(true); 
mChart4.setDragDecelerationFrictionCoef(0.9f); 
mChart4.setDragEnabled(true); 
mChart4.setScaleEnabled(true); 
mChart4.setDrawGridBackground(true); 
mChart4.setHighlightPerDragEnabled(true); 
mChart4.setPinchZoom(true); 
mChart4.setBackgroundColor(Color.WHITE); 
setData4(); 
mChart4.animateX(2500); 
mChart4.getLegend().setEnabled(false); 
mChart4.setExtraLeftOffset(5); 
mChart4.setExtraBottomOffset(10); 

どちらも同じ設定を有します。 コードを複製する必要なく、より良い書き方がありますか?例えば

+5

は、あなたがこれまでの機能を聞いたことがあります? –

+0

@JonathonReinhartユーザー初心者:p –

答えて

5

mChart2 = (LineChart)findViewById(R.id.resultGraph2); 
mChart4 = (LineChart)findViewById(R.id.resultGraph4); 
applySettings(mChart2); 
applySettings(mChart4); 

//... 

void applySettings(LineChart chart) 
{ 
    chart.setTouchEnabled(true); 
    chart.setScaleEnabled(true); 
    // etc... 
} 
+0

ありがとう、これが私を助けました。 –