1

在我的代码中,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);
    }

谢谢你的帮助

4

1 回答 1

0

该问题是由于您CategorySeries为每个数据项创建一个实例而引起的。ACategorySeries可以包含同一类别下的多个值。当您在系列中有一个项目时,条形图会显示得很细。

于 2013-03-28T11:36:56.070 回答