饼图未显示在片段中(Android)
Pie Chart not displaying in a fragment(Android)
我正在开发一个 andorid 应用程序,在这个应用程序中,我试图在一个带选项卡的片段中显示一个饼图。但是它显示"No chart data available"。当我尝试在 activity 中做同样的事情时,它显示得很完美。我使用 "MPAndroidChart" 作为参考。请帮忙。
提前致谢!
这是我的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" />
实际上你的声明是错误的PiChart.Declare它是这样的;
View view = inflater.inflate(R.layout.overview, container, false);
PieChart pieChart = (PieChart)view.findViewById(R.id.chart);
因为你初始化错了。像这样
PieChart pieChart = (PieChart) findViewById(R.id.chart);
将此行更改为
PieChart pieChart = (PieChart) view.findViewById(R.id.chart);
我正在开发一个 andorid 应用程序,在这个应用程序中,我试图在一个带选项卡的片段中显示一个饼图。但是它显示"No chart data available"。当我尝试在 activity 中做同样的事情时,它显示得很完美。我使用 "MPAndroidChart" 作为参考。请帮忙。 提前致谢!
这是我的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" />
实际上你的声明是错误的PiChart.Declare它是这样的;
View view = inflater.inflate(R.layout.overview, container, false);
PieChart pieChart = (PieChart)view.findViewById(R.id.chart);
因为你初始化错了。像这样
PieChart pieChart = (PieChart) findViewById(R.id.chart);
将此行更改为
PieChart pieChart = (PieChart) view.findViewById(R.id.chart);