绘制具有多个Y轴与X轴的折线图

时间:2012-10-03 08:15:20

标签: android achartengine

我想针对两个Y轴与X轴绘制数据。我调试并尽我所能,但没有解决。我想将两条线都绘制在相同的X轴上,不管怎么样。

我的源代码:

public LinearLayout execute(Context context) {
        List<double[]> x = new ArrayList<double[]>();

        cigLen = CommentsDataSource.cigList.size();

        double xDays [] = new double [cigLen];

        // It will runtime decide how many days(X)
        for (int i = 0; i < cigLen; i++) {
            xDays[i] = i;
        }

        double cig [] = new double [cigLen];
        double points [] = new double [cigLen];

        String[] titles = new String[] { "Ciggars"};

        for (int i = 0; i < titles.length; i++) {
          x.add(xDays);
          x.toArray();
        }

        xLen = x.get(0).length;

        for (int i = 0; i < xLen ; i++){
            cig [i] =  CommentsDataSource.cigList.get(i);
            points [i] =  CommentsDataSource.pointList.get(i);           
        }

        List<double[]> values = new ArrayList<double[]>();
        // Add cigars values
        values.add(cig);
        values.toArray();

        // Set color of lines
        int[] colors = new int[] { Color.BLUE, Color.YELLOW };
        PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND};

        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(2);
        setRenderer(renderer, colors, styles);
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
          ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }

        setChartSettings(renderer, "Average Cigeretes", "Days", "", 0, 7, 0, 50, Color.LTGRAY, Color.LTGRAY);
        // fragments on X and Y-axis to be divided
        renderer.setXLabels(7);
        renderer.setYLabels(10);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setZoomButtonsVisible(true);
        renderer.setZoomRate(1.05f);
        renderer.setLabelsColor(Color.WHITE);
        renderer.setXLabelsColor(Color.GREEN);

        renderer.setYLabelsColor(0, colors[0]);
        renderer.setYLabelsColor(1, colors[1]);

        //renderer.setYTitle("% Tar/Nicotine", 1);
        renderer.setYAxisAlign(Align.RIGHT, 1);
        renderer.setYLabelsAlign(Align.LEFT, 1);
        renderer.setYAxisAlign(Align.CENTER,1);
        renderer.setYAxisMin(0, 1);
        renderer.setYAxisMax(100, 1);
        //renderer.setXAxisMin(1);

        XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
        values.clear();

        // Add Tar/Nicotine values
        values.add(points);
        values.toArray();

        addXYSeries(dataset, new String[] { "% Tar/Nicotine" }, x, values, 1);

        // set background color
        renderer.setApplyBackgroundColor(true);     
        renderer.setBackgroundColor(Color.GRAY);
        renderer.setMarginsColor(Color.GRAY);

        GraphicalView gview = ChartFactory.getCubeLineChartView(context, dataset, renderer, 0.0f);

        LinearLayout lnr = null;
        lnr = new LinearLayout(context);
        lnr.addView(gview);

        return lnr;
  }

1 个答案:

答案 0 :(得分:0)

解决方法是不调用setChartSettings方法,因为它只设置第一个缩放轴的边界。