我正在使用MPAndroidChart ver 2.2.3 lib来创建PieChart 也许我做错了但是我真的没弄明白。 只是触摸时突出显示的第一个条目(值),而其他切片不是, 可能是什么问题? 谢谢!
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.alfred.alfredapplication.R;
import com.alfred.alfredapplication.bl.paymentsdb.PaymentsDBManager;
import com.alfred.alfredapplication.bl.secondlevelparse.PaymentsManager;
import com.alfred.alfredapplication.ui.items.PieItem;
import com.github.mikephil.charting.charts.PieChart;
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.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.mail.MessagingException;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Home_Companies.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Home_Companies#newInstance} factory method to
* create an instance of this fragment.
*/
public class Home_Companies extends Fragment implements OnChartValueSelectedListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public Home_Companies() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Home_Companies.
*/
// TODO: Rename and change types and number of parameters
public static Home_Companies newInstance(String param1, String param2) {
Home_Companies fragment = new Home_Companies();
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) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_combiend_list, container, false);
final PieChart mContentView = (PieChart) view.findViewById(R.id.pie_chart_all_home);
PieItem pieItem = createPieForView(mContentView);
TextView mTotalTextView = (TextView) view.findViewById(R.id.txt_total);
mTotalTextView.setText(String.valueOf(pieItem.totalAmount) + " ILS");
TextView mLastScanTextView = (TextView) view.findViewById(R.id.txt_lastscan);
TextView mUserTextView = (TextView) view.findViewById(R.id.txt_user);
TextView mNotesTextView = (TextView) view.findViewById(R.id.txt_notes);
//mContentView.getLegend().setEnabled(false);
//mContentView.animateY(3000, Easing.EasingOption.EaseInOutQuad);
return view;
}
private PieItem createPieForView(final PieChart mContentView) {
PieItem pieItem = getdata();
mContentView.setDescription("");
mContentView.setHoleRadius(80f);
mContentView.setRotationEnabled(false);
mContentView.setCenterTextSize(35f);
mContentView.setUsePercentValues(true);
mContentView.setTouchEnabled(true);
mContentView.setClickable(true);
mContentView.setHovered(true);
mContentView.setHighlightPerTapEnabled(true);
mContentView.setDrawSliceText(false);
mContentView.getLegend().setEnabled(false);
mContentView.setOnChartValueSelectedListener(this);
pieItem.entries.add(new Entry(3000, 3));
pieItem.labels.add("Temp");
PieDataSet dataset = new PieDataSet(pieItem.entries, "");
dataset.setSliceSpace(3f);
dataset.setValueTextSize(12f);
dataset.setValueTextColor(Color.WHITE);
//dataset.setHighlightEnabled(true);
dataset.setColors(ColorTemplate.JOYFUL_COLORS);
PieData data = new PieData(pieItem.labels, dataset);
//data.setHighlightEnabled(true);
mContentView.setData(data);
mContentView.highlightValues(null);
mContentView.invalidate();
return pieItem;
}
private PieItem getData() {
PieItem pieItem = null;
List<Entry> entries = new ArrayList<>();
List<String> labels = new ArrayList<>();
float totalAmount = 0;
PaymentsDBManager paymentsDBManager = new PaymentsDBManager(getContext());
for (int index = 0; index < PaymentsManager.PaymentsTypes.values().length; index++) {
String currPayment = PaymentsManager.PaymentsTypes.values()[index].name();
try {
BigDecimal bigDecimal = paymentsDBManager.getPaymentTotalAmountByName(currPayment);
float currAmountPayment = bigDecimal.floatValue();
if (currAmountPayment != 0) {
totalAmount = totalAmount + currAmountPayment;
labels.add(currPayment);
entries.add(new Entry(currAmountPayment, index));
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
pieItem = new PieItem(entries, "Total", totalAmount, labels);
return pieItem;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onValueSelected(Entry entry, int i, Highlight highlight) {
}
@Override
public void onNothingSelected() {
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:0)
创建新条目时,请为其指定唯一索引(从0开始)。