Android:Achartengine中的条形图和饼图

时间:2012-10-16 07:13:30

标签: android achartengine

我正在使用achartengine库绘制条形图和饼图来表示其中的各种应用程序名称。

当我实例化CategorySeries对象时,它需要一个系列标题:

CategorySeries series = new CategorySeries("title") //IN BAR CHART 
CategorySeries series = new CategorySeries("title") //IN PIE CHART 

与饼图不同,应用程序名称不会在条形图的底部膨胀 比方说,如果饼图如下:

(1)申请1

(2)申请2

(3)申请......直至

(10)申请10.

但它不会出现在条形码中,而是显示为:

(1)申请数量---->>> (这是我的系列名称                                           作为参数传入                                           实例化CategorySeries的对象)


请告诉我要显示条形图的关键字,我已经附上了一个代码链接


if (c.getCount() > 0) {
                if (! mPieChart) {
                    series = new CategorySeries("NUMBER OF APPLICATIONS");
                    getActivity().startManagingCursor(c);
                    c.moveToPosition(-1);
                     //series.clear();
                    while (c.moveToNext()) {

                        recievedBytes = c.getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
                        sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
                        totalBytes = (recievedBytes + sentBytes) / (1024 * 1024);
                        if (totalBytes > 0) {
                            applicationInfo
                                    .add(new ApplicationDataUsage(
                                            c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
                                            Double.toString(totalBytes)));
                        }
                    }

                    int size = applicationInfo.size();
                    for (int i = 0; i < size; ++i) {
                        Log.v("BAR CHART", "data added are " + applicationInfo.get(i).mApplicationName);
                        double value = Double.parseDouble(applicationInfo
                                .get(i).mDataUsage);
                        series.add("BAR", value);

                    }

                    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

                    dataset.addSeries(series.toXYSeries());
                    XYSeriesRenderer renderer = new XYSeriesRenderer();
                    renderer.setDisplayChartValues(true);
                    renderer.setChartValuesSpacing((float) 0.5);
                    renderer.setColor(Color.CYAN);

                    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
                    mRenderer.setBarSpacing(0.5);
                    mRenderer.getBarSpacing();
                    mRenderer.setXTitle("Application Number");
                    mRenderer.setYTitle("Data Usage");
                    mRenderer.setAxesColor(Color.GREEN);
                    mRenderer.setZoomButtonsVisible(true);
                    mRenderer.setInScroll(true);
                    mRenderer.setLabelsColor(Color.RED);
                    mRenderer.addSeriesRenderer(renderer);

                    if (mChartViewBar == null) {
                        Log.v(LOG_TAG, "chart view is null");
                        mChartViewBar = ChartFactory
                                .getBarChartView(getActivity(), dataset,
                                        mRenderer, Type.DEFAULT);
                        mRenderer.setClickEnabled(true);
                        mRenderer.setSelectableBuffer(10);
                        mChartViewBar
                                .setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        SeriesSelection seriesSelection = mChartViewBar
                                                .getCurrentSeriesAndPoint();
                                        CategorySeries series22 =new CategorySeries("333");
                                        mChartViewBar.repaint();
                                        if (seriesSelection == null) {
                                            Toast.makeText(
                                                    getActivity(),
                                                    "No chart element was clicked",
                                                    Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(
                                                    getActivity(),
                                                    "APPLICATION NAME : "
                                                            + series22.getTitle()
                                                            + 1
                                                            + " was clicked"
                                                            + " DATA USAGE : "
                                                            + seriesSelection
                                                                    .getXValue(),
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                        mChartLayout.removeAllViews();
                        mChartLayout.addView(mChartViewBar, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
                    } else {
                        mChartLayout.removeAllViews();
                        mChartLayout.addView(mChartViewBar, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
                        mChartViewBar.repaint();
                    }
                } else if (mPieChart) {
                    CategorySeries seriesPie = new CategorySeries("Pie Graph");
                    getActivity().startManagingCursor(c);
                    c.moveToPosition(-1);
                    while (c.moveToNext()) {
                        long recievedBytes = c .getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
                        Long sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
                        totalBytes = (recievedBytes + sentBytes)
                                / (1024 * 1024);
                        if (totalBytes > 0) {
                                applicationInfo
                                        .add(new ApplicationDataUsage(
                                                c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
                                                Double.toString(totalBytes)));
                        }
                    }
                    int size = applicationInfo.size();
                    for (ApplicationDataUsage data_value : applicationInfo) {
                        Log.v("PIE CHART", "data added are " + data_value.mApplicationName);
                        double dataUsage = Double
                                .parseDouble(data_value.mDataUsage);
                        seriesPie.add(data_value.mApplicationName, dataUsage);
                    }

                    for (int i = 0; i < size; ++i) {
                        prepareColors();
                    }
                    DefaultRenderer renderer = new DefaultRenderer();
                    for (int fillColor : colorList) {
                        // Log.d("1111111111",
                        // "FILL COLOUR TILL SIZE OF COLORLIST");
                        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                        r.setColor(fillColor);
                        renderer.addSeriesRenderer(r);
                    }
                    renderer.setInScroll(true);
                    renderer.setChartTitle("APPLICATION WISE PIE CHART");
                    renderer.setChartTitleTextSize(20);
                    renderer.setApplyBackgroundColor(true);
                    renderer.setBackgroundColor(Color.BLACK);
                    renderer.setZoomButtonsVisible(true);

                    if (mChartViewPie == null) {
                        Log.v(LOG_TAG, "chat view is null");
                        // series.clear();
                        mChartViewPie = ChartFactory.getPieChartView(
                                getActivity(), seriesPie, renderer);
                        renderer.setClickEnabled(true);
                        renderer.setSelectableBuffer(10);
                        mChartViewPie
                                .setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        SeriesSelection seriesSelection = mChartViewPie
                                                .getCurrentSeriesAndPoint();
                                        mChartViewPie.repaint();
                                        if (seriesSelection == null) {
                                            ToastHelper.showToastMessage(
                                                    "No item was Clicked",
                                                    getActivity());
                                        } else {
                                            ToastHelper.showToastMessage(
                                                    "Application number "
                                                            + seriesSelection
                                                                    .getPointIndex()
                                                            + " was clicked and it's Data Usage is "
                                                            + seriesSelection
                                                                    .getValue(),
                                                    getActivity());
                                        }
                                    }
                                });
                        /*if (mChartViewBar != null) {
                            mChartLayout.removeView(mChartViewBar);
                        }
                        if (mChartViewPie != null) {
                            mChartLayout.removeView(mChartViewPie);
                        }*/
                        mChartLayout.addView(mChartViewPie, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
                    } else {
                    /*  if (mChartViewBar != null) {
                            mChartLayout.removeView(mChartViewBar);
                        }
                        if (mChartViewPie != null) {
                            mChartLayout.removeView(mChartViewPie);
                        }*/
                        mChartLayout.removeAllViews();
                        mChartLayout.addView(mChartViewPie, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
                        mChartViewPie.repaint();
                    }
                }

1 个答案:

答案 0 :(得分:1)

您将图例与X轴标签混淆。 BarChartXYChart,这意味着它同时获得了图例和x轴标签。 PieChart不是XYChart,因此没有获得X轴和Y轴。

如你所说,系列标题会转到图例。

在X轴中,系列值索引默认为。但是,您可以隐藏默认标签并添加自己的自定义标签:

renderer.setXLabels(0);
renderer.addXTextLabel(1, "label);
renderer.addXTextLabel(2, "label);
...