我的应用程序中有achartengine饼图有问题。一切都很好,但是当我尝试使用一些过滤后的数据图表更新图表时,图例变为灰色,点击不会标记选定的图表切片。
片段开始时:
点击刷新时:
我的代码:
公共类StatistikaIslaidosFragment扩展了Fragment {
[3, 28, 27, 21, 5, 35, 46, 34, 40, 14]
}
刷新按钮点击代码:
private DBhelper db;
private HashMap<String, Double> chartData;
private View mRootView;
private static int[] COLORS = new int[] { Color.RED, Color.BLUE, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE, Color.DKGRAY };
private CategorySeries mSeries = new CategorySeries("");
private DefaultRenderer mRenderer = new DefaultRenderer();
private GraphicalView mChartView;
private Button firstDate, secondDate;
private ImageButton refresh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_statistika_islaidos, container, false);
db = new DBhelper(mRootView.getContext());
chartData = new HashMap<String, Double>();
firstDate = (Button) mRootView.findViewById(R.id.buttonDate1);
secondDate = (Button) mRootView.findViewById(R.id.buttonDate2);
refresh = (ImageButton) mRootView.findViewById(R.id.buttonRefresh);
dateButtonsClick();
collectChartData();
fillPieChart();
sliceClick();
return mRootView;
}
private void setDayToToday(){
Calendar c = Calendar.getInstance();
c.roll(Calendar.MONTH, false);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
firstDate.setText(formattedDate);
c = Calendar.getInstance();
formattedDate = sdf.format(c.getTime());
secondDate.setText(formattedDate);
}
private void dateButtonsClick(){
setDayToToday();
firstDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragmentStatistic1();
picker.show(getActivity().getFragmentManager(), "datePicker");
}
});
secondDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragmentStatistic2();
picker.show(getActivity().getFragmentManager(), "datePicker");
}
});
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSeries.clear();
mRenderer = new DefaultRenderer();
chartData.clear();
collectChartData();
fillPieChart();
}
});
}
private void sliceClick(){
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
showMessage("No chart element was clicked");
} else {
for (int i = 0; i < mSeries.getItemCount(); i++) {
mRenderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView.repaint();
showMessage("Chart element data point index " + seriesSelection.getPointIndex()
+ " was clicked" + " point value=" + seriesSelection.getValue());
}
}
});
}
private void showMessage(String text){
Toast.makeText(mRootView.getContext(), text, Toast.LENGTH_LONG).show();
}
private void chartSetings(){
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(getResources().getColor(R.color.theme_color));
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(40);
mRenderer.setShowLabels(false);
mRenderer.setLegendTextSize(40);
mRenderer.setMargins(new int[]{20, 30, 15, 0});
mRenderer.setZoomButtonsVisible(false);
mRenderer.setStartAngle(90);
if (mChartView == null) {
LinearLayout layout = (LinearLayout) mRootView.findViewById(R.id.chart);
mChartView = ChartFactory.getPieChartView(mRootView.getContext(), mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
layout.addView(mChartView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
} else {
mChartView.repaint();
}
}
private void fillPieChart(){
chartSetings();
for(String key: chartData.keySet())
{
mSeries.add(key.toString(), chartData.get(key));
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
if (mChartView != null)
mChartView.repaint();
}
}
private void collectChartData(){
List<Operation> data = db.getAllOperations();
for (Operation op:data){
Date start = strToDate(firstDate.getText().toString());
Date finish = new Date();
Date opDate = strToDate(op.getDate());
if(op.getName().equals(getResources().getString(R.string.islaidos))) {
if(opDate.after(start) && opDate.before(finish)) {
if (chartData.containsKey(op.getCategory())) {
chartData.put(op.getCategory(), chartData.get(op.getCategory()) + Double.parseDouble(op.getMoney()));
} else {
chartData.put(op.getCategory(), Double.parseDouble(op.getMoney()));
}
}
}
}
}
private Date strToDate(String str){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
请帮帮我
答案 0 :(得分:0)
问题是mRenderer = new DefaultRenderer();
我刚刚将这行代码更改为:
mRenderer.removeAllRenderers();
现在效果很好:)