1
private void openChart(){

    double[] data = {0.2, 1.11, 100 };


    // Creating an  XYSeries for Income
    XYSeries expenseSeries = new XYSeries("");
    // Adding data to Income and Expense Series
    for(int i=0;i<data.length;i++){
        expenseSeries.add(i,data[i]);
    }

    // Creating a dataset to hold each series
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    // Adding Expense Series to dataset
    dataset.addSeries(expenseSeries);  

    // Creating XYSeriesRenderer to customize expenseSeries
    XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
    expenseRenderer.setColor(Color.RED);
    expenseRenderer.setFillPoints(false);
    expenseRenderer.setLineWidth(2);


    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setYAxisMin(0);
    multiRenderer.setYAxisMax(500);
    multiRenderer.setXTitle("Annualized");
    multiRenderer.setYTitle("Rate");           
    multiRenderer.setBarSpacing(1.0);
    multiRenderer.setXLabels(0);
    multiRenderer.setMargins(new int[] { 20 ,20,  20, 30 });
    multiRenderer.setZoomRate(0.2f); 
    multiRenderer.setFitLegend(true);
    multiRenderer.setShowGrid(true);
    multiRenderer.setZoomEnabled(false);
    multiRenderer.setPanEnabled(false,false);

    for(int i=0; i< data.length;i++){
        multiRenderer.addXTextLabel(i, mMonth[i]);          
    }    

    multiRenderer.addSeriesRenderer(expenseRenderer);

    // Creating an intent to plot bar chart using dataset and multipleRenderer      
    Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);

    // Start Activity
    startActivity(intent);
    }   

在此处输入图像描述

我想从左侧边距开始。它隐藏在左侧,所以我看不到第一张图。它怎么可能?我附上屏幕截图以及制作图表的方法。我还想删除底部的标签图像?并将白色背景放入整个条形图中?我试过但没用?

4

1 回答 1

5

好的。您正在从第 0 个索引设置xTextLabel,在某些情况下它可能会导致此类问题。可能的解决方法可能是从 1 个索引而不是 0 开始您的图表。

我之前实现过同样的事情,并根据您的需要更改我的方法。您可以直接将此方法添加到您的代码中。

这是我的方法:

private void opennewChart() {

        // Integer xmax, ymax that will take care of boundary of X-Y axis
        int ymax = 0;
        float xmax = 0;
        // list of arraylist of values
        List<ArrayList<String>> values = new ArrayList<ArrayList<String>>();

        // your title
        String[] titles = new String[] { "Promise" };

        // put your values here
        ArrayList<String> value1 = new ArrayList<String>();
        value1.add("15");
        value1.add("54");
        value1.add("500");


        values.add(value1);

        // calculate xmax, ymax
        xmax = Float.valueOf(mMonth.length + 1.0f);
        for (int i = 0; i < value1.size(); i++) {

            if (ymax < Integer.parseInt(value1.get(i).toString())) {
                ymax = Integer.parseInt(value1.get(i).toString());

            }
        }

        int[] colors = new int[] { Color.parseColor("#4C4646") };
        XYMultipleSeriesRenderer renderer;
        renderer = buildBarRenderer(colors);
        renderer.setOrientation(Orientation.HORIZONTAL);
        renderer.setApplyBackgroundColor(false);
        renderer.setBackgroundColor(Color.parseColor("#E3E0E4"));
        renderer.setMarginsColor(Color.parseColor("#E3E0E4"));
        renderer.setBarSpacing(0.5);
        renderer.setZoomEnabled(true, true);
        renderer.setPanEnabled(false, false);
        renderer.setMargins(new int[] { 10, 65, 18, 10 });

        setChartSettings(renderer, "", "Month", "Values", 0.0, xmax, 0, ymax,
                Color.GREEN, Color.parseColor("#4C4646"));

        renderer.setXLabels(1);
        renderer.setXLabelsAlign(Align.CENTER);
        renderer.setXLabelsColor(Color.parseColor("#4C4646"));
        renderer.setYLabelsColor(0, Color.parseColor("#4C4646"));
        renderer.setChartValuesTextSize(16.0f);
        renderer.setYLabels(5);
        renderer.setYLabelsAlign(Align.CENTER);

        for (int i = 0; i < mMonth.length; i++) {
            // Start to put your Text Lables with 1 index
            renderer.addXTextLabel(i + 1, mMonth[i]);
        }

        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer seriesRenderer = renderer
                    .getSeriesRendererAt(i);
            seriesRenderer.setDisplayChartValues(true);
        }

        // Creating an intent to plot bar chart using dataset and
        // multipleRenderer
        Intent intent = ChartFactory.getBarChartIntent(getBaseContext(),
                buildBarDataset(titles, values), renderer, Type.STACKED);

        // Start Activity
        startActivity(intent);
    }

您还需要添加以下方法:

protected void setChartSettings(XYMultipleSeriesRenderer renderer,
            String title, String xTitle, String yTitle, double xMin,
            double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        renderer.setChartTitle(title);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

    protected XYMultipleSeriesDataset buildBarDataset(String[] titles,
            List<ArrayList<String>> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            CategorySeries series = new CategorySeries(titles[i]);
            ArrayList<String> v = values.get(i);
            int seriesLength = v.size();
            for (int k = 0; k < seriesLength; k++) {
                series.add(Double.parseDouble(v.get(k)));
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

        renderer.setAxisTitleTextSize(18);
        renderer.setChartTitleTextSize(18);
        renderer.setLabelsTextSize(18);
        renderer.setLegendTextSize(18);
        int length = colors.length;
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(colors[i]);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

输出 :

在此处输入图像描述

于 2013-09-13T10:20:57.127 回答