我正在開發一個andorid應用程序,並且在此應用程序中,我試圖通過選項卡片段之一顯示餅圖。但它顯示「無圖表數據」。當我嘗試在一個活動中做同樣的事情時,它完美地顯示出來。我使用「MPAndroidChart」作爲參考。請幫助。 在此先感謝!餅圖中不顯示餅圖(Android)
這是我fragment.class
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class overview extends android.support.v4.app.Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static View rootView;
private OnFragmentInteractionListener mListener;
private SeekBar mSeekBarX, mSeekBarY;
private TextView tvX, tvY;
private PieChart mChart;
private Typeface tf;
public overview() {
// Required empty public constructor
}
//String param1, String param2
public static overview newInstance() {
overview fragment = new overview();
Bundle args = new Bundle();
// args.putString(ARG_PARAM1, param1);
//args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.overview, container, false);
PieChart pieChart = (PieChart) findViewById(R.id.chart);
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(12f, 3));
entries.add(new Entry(18f, 4));
entries.add(new Entry(9f, 5));
PieDataSet dataset = new PieDataSet(entries, "# of Calls");
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
PieData data = new PieData(labels, dataset);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
pieChart.setDescription("Description");
pieChart.setData(data);
pieChart.animateY(5000);
pieChart.saveToGallery("/sd/mychart.jpg", 85); // 85 is the quality of
the image
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.overview, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return rootView;
}
private SpannableString generateCenterText() {
SpannableString s = new SpannableString("Revenues\nQuarters 2015");
s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0);
return s;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
這是我的xml文件:
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="300dp"
android:layout_height="300dp" />
感謝這個!但是在做出更改之後,我仍然在佈局中收到同樣的消息,說「沒有圖表數據」 –