使用AchartEngine库在Bargraph中添加多个条不适用于Android

时间:2013-03-28 11:11:59

标签: android bar-chart achartengine

在我的代码中,Y2是年份列表, a2是包含值的String的arrayList。我需要在条形图中显示与年份相对应的值,不知道我哪里出错了,请帮助我, 这是我的代码

if(yearSpinner.equals("All")){
        titles = new String[] { "Amount","sasa"};
    values = new ArrayList<double[]>();
    Double[] doubleLat = new Double[a2.size()];
        for(int i = 0; i < a2.size(); i++)
        {
            doubleLat[i] = a2.get(i);
     System.out.println("doubleee=="+doubleLat[i]);
          values.add(new double[] {  doubleLat[i]});
        System.out.println("val=="+values);
        }


        int[] colors = new int[] { Color.parseColor("#77c4d3")};
         renderer = buildBarRenderer(colors);
        renderer.setOrientation(Orientation.HORIZONTAL);
        setChartSettings(renderer, "Yearly Details", "Year", "Earnings", 0.5,
                12.5, 0, yaxis, Color.BLACK, Color.BLACK);
        renderer.setXLabels(1);
        renderer.setYLabels(10);
        for(int i =0;i<y2.size();i++){
            renderer.addXTextLabel(i+1, y2.get(i));
        }
        //renderer.addXTextLabel(1, "yearly");


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

        }
    }

        final GraphicalView grfv = ChartFactory.getBarChartView(getActivity(), buildBarDataset(titles, values), renderer,Type.DEFAULT);
        return grfv;
}
    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setBarWidth(30);
        renderer.setPanEnabled(false, false);
        renderer.setMarginsColor(Color.parseColor("#EEEDED"));
        renderer.setXLabelsColor(Color.BLACK);
        renderer.setYLabelsColor(0,Color.BLACK);
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
        int length = a2.size();
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(Color.parseColor("#77c4d3"));
            //r.setChartvalueAngle(-90);
            r.setChartValuesSpacing(15);

            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }
    protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = values.size();
        for (int i = 0; i < length; i++) {
            CategorySeries series = new CategorySeries("Amoount");
            double[] v = values.get(i);
            System.out.println("values = ="+values);
            System.out.println("v = ="+v);
            int seriesLength = v.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(v[k]);
                System.out.println("v [k]= ="+v[k]);
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    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.setYLabelsAlign(Align.RIGHT);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);     
        renderer.setMargins(new int[] { 10, 65, 10, 15 });
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

问题是由于您为每个数据项创建一个CategorySeries实例。 CategorySeries可以在同一类别下包含多个值。如果系列中只有一个项目,则条形图显示为精简。