2017-05-13 1 views
0

私はライブラリをアンドロイドスタジオのチャートに使用しています。グラフはアクティビティでうまく表示されていますが、断片的に使用することはできません。別のチャートを使ってみましたが、私は問題を解決することができません。私はグラフにデータを与えているのに、 "No chart data available"というメッセージが表示されます。だから基本的に私は3つのフラグメントMathsFragment、PhysicsFragment、ChemistryFragmentを持つExamPerformanceActivityと呼ばれるアクティビティ(下部のナビゲーションバーを持つ)があります。私は問題に直面しているMathsFragmentに棒グラフを持っています。MPAndroidChartがフラグメントに表示されない

activity_exam_performance.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
android:id="@+id/activity_main" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.peepstake.vijay.statlog.ExamPerfoActivity"> 

<FrameLayout 
    android:id="@+id/frame_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/navigation" 
    android:animateLayoutChanges="true"> 
</FrameLayout> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="@color/colorPrimary" 
    app:itemIconTint="#d5d6d9" 
    app:itemTextColor="#ffffff" 
    app:menu="@menu/bottombar_student_items"/> 
    </RelativeLayout> 

ExamPerformanceActivity.java

public class ExamPerformanceActivity extends AppCompatActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_exam_performance); 

    BottomNavigationView bottomNavigationView = (BottomNavigationView) 
      findViewById(R.id.navigation); 

    bottomNavigationView.setOnNavigationItemSelectedListener 
      (new BottomNavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
        Fragment selectedFragment = null; 
        switch (item.getItemId()) { 
         case R.id.maths: 
          selectedFragment = MathsFragment.newInstance(); 
          break; 
         case R.id.physics: 
          selectedFragment = PhysicsFragment.newInstance(); 
          break; 
         case R.id.chemistry: 
          selectedFragment = ChemistryFragment.newInstance(); 
          break; 
        } 
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
        transaction.replace(R.id.frame_layout, selectedFragment); 
        transaction.commit(); 
        return true; 
       } 
      }); 

    //Manually displaying the first fragment - one time only 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.frame_layout, MathsFragment.newInstance()); 
    transaction.commit(); 

    //Used to select an item programmatically 
    //bottomNavigationView.getMenu().getItem(2).setChecked(true); 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     // Respond to the action bar's Up/Home button 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
public void goTopicwise(View v){ 
    Intent intent = new Intent(ExamPerformanceActivity.this, TopicwiseActivity.class); 
    startActivity(intent); 
} 

} 

fragment_maths.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.peepstake.vijay.statlog.Common.MathsFragment"> 


<com.github.mikephil.charting.charts.BarChart 
    android:id="@+id/bar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</com.github.mikephil.charting.charts.BarChart> 

</FrameLayout> 

MathsFragment.java

public class MathsFragment extends Fragment { 
BarChart bar; 

public static MathsFragment newInstance() { 
    MathsFragment fragment = new MathsFragment(); 
    return fragment; 
} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

public MathsFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View view =inflater.inflate(R.layout.fragment_maths, container, false); 

    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ProductSans.ttf"); 

    bar = (BarChart)view.findViewById(R.id.bar); 

    List<BarEntry> entries = new ArrayList<>(); 
    entries.add(new BarEntry(0f, 100f,"Total")); 
    entries.add(new BarEntry(1f, 82f,"Obtained")); 
    entries.add(new BarEntry(2f, 95f,"Highest")); 
    entries.add(new BarEntry(3f, 69f,"Average")); 



    BarDataSet bSet = new BarDataSet(entries, "Marks"); 
    bSet.setColors(ColorTemplate.VORDIPLOM_COLORS); 

    ArrayList<String> barFactors = new ArrayList<>(); 
    barFactors.add("Total"); 
    barFactors.add("Obtained"); 
    barFactors.add("Highest"); 
    barFactors.add("Average"); 


    XAxis xAxis = bar.getXAxis(); 
    xAxis.setGranularity(1f); 
    xAxis.setGranularityEnabled(true); 
    BarData data = new BarData(bSet); 
    data.setBarWidth(0.9f); // set custom bar width 
    data.setValueTextSize(12); 
    Description description = new Description(); 
    description.setTextColor(R.color.colorPrimary); 
    description.setText("All values in marks"); 
    bar.setDescription(description); 
    bar.setData(data); 
    bar.setFitBars(true); // make the x-axis fit exactly all bars 
    bar.invalidate(); // refresh 
    bar.getXAxis().setValueFormatter(new IndexAxisValueFormatter(barFactors)); 

    Legend l = bar.getLegend(); 
    l.setFormSize(10f); // set the size of the legend forms/shapes 
    l.setForm(Legend.LegendForm.CIRCLE); // set what type of form/shape should be used 
    l.setTypeface(font); 
    l.setTextSize(12f); 
    l.setTextColor(Color.BLACK); 
    List<LegendEntry> lentries = new ArrayList<>(); 
    for (int i = 0; i < barFactors.size(); i++) { 
     LegendEntry entry = new LegendEntry(); 
     entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i]; 
     entry.label = barFactors.get(i); 
     lentries.add(entry); 
    } 
    l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis 
    l.setYEntrySpace(5f); 
    l.setCustom(lentries); 

    return inflater.inflate(R.layout.fragment_maths, container, false); 
} 

} 

My Output screen

答えて

0

変更し、以下にごonCreateView方法の最後の行:

l.setCustom(lentries); 
//return inflater.inflate(R.layout.fragment_maths, container, false); 
return view; 

チャートが正しく表示されます。

a bar chart

なぜですか?その方法の最初のライン:

View view = inflater.inflate(R.layout.fragment_maths, container, false); 

そのフラグメントに対するcontentViewあるViewオブジェクトを膨張させます。

その後、呼び出し:

bar = (BarChart)view.findViewById(R.id.bar); 

を同じViewオブジェクトにBarChartにハンドルを取得し、それにデータを追加します。

ただし、メソッドの最後の元のコードでは、新しいcontentViewを展開し、データを追加したビューとBarChartを破棄します。つまり、元のメソッドはチャートを設定して破棄し、ビューを拡張した単純な結果である空のBarChartに置き換えます。

+0

ありがとうございました!あなたはとても役に立ちます。私はそれを非常に感謝します。出来た。 –

関連する問題