0

我正在尝试通过从我的 sqlite 数据库中获取数据来创建饼图。我希望饼图显示朋友姓名和朋友支出。饼图应该根据朋友的消费来划分。我在这里做错了什么?请指导我。谢谢。

这是我的饼图代码:

public class PieChart {
public Intent getIntent(Context context,String eventId){
    EventController controller = new EventController(context);
  // this is my data of performance; data is collected in array.
    HashMap<String, String> queryValues = new HashMap<String, String>();
    queryValues.put("eventId", eventId);
    ArrayList <HashMap<String,String>> getData = controller.getAllFriends(queryValues);
     CategorySeries series = new CategorySeries("pie"); // adding series to charts.
     int colorcode = -13959039;
     ArrayList<String> coloring = new ArrayList<String>();
    for (int a=0; a<getData.size();a++){
        String friendName = null;
        double friendSpend = 0;
        HashMap<String, String> ValexpenseId = getData.get(a);
        for (Entry<String, String> entry : ValexpenseId.entrySet()) {
            String key = entry.getKey();
            if (key.equals("friendName")){
                String value = entry.getValue();
                friendName = value;
            }
            else if (key.equals("friendSpending")){
                String value = entry.getValue();
                double friendSpending = Double.parseDouble(value);
                friendSpend = friendSpending;
            }
            series.add(friendName,friendSpend);             
            coloring.add(String.valueOf(colorcode));
            colorcode = colorcode + 1000000;
        }
    }            
 // set style for series
        DefaultRenderer renderer = new DefaultRenderer();
        for(String color : coloring){
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            int colour = Integer.parseInt(color);
            r.setColor(colour);
            r.setDisplayBoundingPoints(true);
            r.setDisplayChartValuesDistance(5);
            r.setDisplayChartValues(true);
            r.setChartValuesTextSize(15);
            renderer.addSeriesRenderer(r);
        }
        renderer.isInScroll();
        renderer.setZoomButtonsVisible(true);   //set zoom button in Graph
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.BLACK); //set background color
        renderer.setChartTitle("Efforts");
        renderer.setChartTitleTextSize((float) 30);
        renderer.setShowLabels(true);  
        renderer.setLabelsTextSize(20);
        renderer.setLegendTextSize(25);
        renderer.setDisplayValues(true);
        return ChartFactory.getPieChartIntent(context, series, renderer, "PieChart");
    }

原木猫:

11-15 15:03:33.985: E/AndroidRuntime(24170): FATAL EXCEPTION: main 11-15 15:03:33.985: E/AndroidRuntime(24170): java.lang.NullPointerException 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.AbstractChart.getFitText(AbstractChart.java:405) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.AbstractChart.drawLabel(AbstractChart.java:483) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.PieChart.draw(PieChart.java:134) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13877) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12811) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12859) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13593) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.ViewGroup.drawChild(ViewGroup.java:2954) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2815) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13880) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.widget.FrameLayout.draw(FrameLayout.java:467) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12811)

4

1 回答 1

0

Code is so much complected. I would suggest you to divide in methods.

1) Prepare Data

private CategorySeries getDatSet(WorkExp exp) {
        CategorySeries mSeries = new CategorySeries("");
        int[] weightArr = exp.getWeight();
        String[] taskArr = exp.getTask();
        for (int taskCount = 0; taskCount < taskArr.length; taskCount++) {
            int weight = weightArr[taskCount];
            String task = taskArr[taskCount];
            mSeries.add(task, weight);
        }
        return mSeries;
    }

2) Generate Renderer

private DefaultRenderer getRenderer(WorkExp exp) {
        String[] colorArr = exp.getColor();
        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int taskCount = 0; taskCount < colorArr.length; taskCount++) {
            String color = colorArr[taskCount];
            SimpleSeriesRenderer simpleRenderer = new SimpleSeriesRenderer();
            simpleRenderer.setColor(Color.parseColor(color));
            defaultRenderer.addSeriesRenderer(simpleRenderer);
        }
        defaultRenderer.setShowLabels(true);
        defaultRenderer.setShowLegend(true);
        return defaultRenderer;
    }

3) And then generate chart

private GraphicalView generateChart(CategorySeries dataset,
            DefaultRenderer renderer) {
        return ChartFactory.getPieChartView(context, dataset, renderer);
    }

4) Finally call

WorkExp exp = expList.get(position);
            chartContainer.addView(generateChart(getDatSet(exp),
                    getRenderer(exp)));

I have achieved pie chart two days back using the same code.

Let me know if you do not understand any line. I will help you.

于 2013-11-15T07:17:17.783 回答