1

我试图在一个带有两个 y 轴和 1 个 x 轴的折线图中绘制两条线。第一行是正确的,但第二行不适合 x 值。

它目前看起来像这样:

在此处输入图像描述

这是代码:

bmiSeries =  new XYSeries("BMI",0);
    bmiSeries2 =  new XYSeries("BMI Trefethen",1);

    dataset = new XYMultipleSeriesDataset();

    bmiRenderer = new XYSeriesRenderer();
    bmiRenderer.setColor(getResources().getColor(R.color.green));
    bmiRenderer.setPointStyle(PointStyle.CIRCLE);
    bmiRenderer.setFillPoints(true);
    bmiRenderer.setLineWidth(2);
    bmiRenderer.setDisplayChartValues(true);

    bmiRenderer2 = new XYSeriesRenderer();
    bmiRenderer2.setColor(getResources().getColor(R.color.red));
    bmiRenderer2.setPointStyle(PointStyle.CIRCLE);
    bmiRenderer2.setFillPoints(true);
    bmiRenderer2.setLineWidth(2);
    bmiRenderer2.setDisplayChartValues(true);



    multiRenderer = new XYMultipleSeriesRenderer(2);
    multiRenderer.setXLabels(0);
    multiRenderer.setZoomButtonsVisible(true);
    multiRenderer.setMargins(new int[] {0, 25 ,25,25});

    for(int i=0; i<bmiList.size();i++){
        bmiSeries.add(i, bmiList.get(i).getBmi_old());
        bmiSeries2.add(i, bmiList.get(i).getBmi_new());
        Date dat = bmiList.get(i).getDate();
        Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = formatter.format(dat);
        multiRenderer.addXTextLabel(i, date);
    }


    dataset.addSeries(0, bmiSeries);
    dataset.addSeries(1, bmiSeries2);

    multiRenderer.addSeriesRenderer(bmiRenderer);
    multiRenderer.addSeriesRenderer(bmiRenderer2);
    multiRenderer.setBarSpacing(4);
    multiRenderer.setApplyBackgroundColor(true);
    multiRenderer.setBackgroundColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    multiRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    multiRenderer.setYLabelsColor(0, Color.BLACK);
    multiRenderer.setXLabelsColor(Color.BLACK);
    multiRenderer.setShowGrid(true);
    multiRenderer.setZoomEnabled(true, false);
    multiRenderer.setXTitle("Date");
    multiRenderer.setYTitle("BMI");
    multiRenderer.setLabelsTextSize(15);
    multiRenderer.setXAxisMin(-1);
    multiRenderer.setXAxisMax(2);
    multiRenderer.setYAxisMin(bmiSeries.getMinY()-5,0);
    multiRenderer.setYAxisMax(bmiSeries.getMaxY()+3,0);
    multiRenderer.setYAxisMin(bmiSeries2.getMinY()-5,1);
    multiRenderer.setYAxisMax(bmiSeries2.getMaxY()+3,1);
    multiRenderer.setYAxisAlign(Align.RIGHT, 0);
    multiRenderer.setPanEnabled(true, false);
    multiRenderer.setChartValuesTextSize(10);
    multiRenderer.setZoomLimits(new double[] {-2,10,0,10});
    multiRenderer.setPanLimits(new double[] { bmiSeries.getMinX()-1, bmiSeries.getMaxX()+10, bmiSeries.getMinY(), bmiSeries.getMaxY()  });

    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
    ZoomListener listener = new ZoomListener() {
            @Override
            public void zoomReset() {
                zoomR = 1;
                multiRenderer.setChartValuesTextSize(10*zoomR);
                multiRenderer.setLabelsTextSize(15);

            }

            @Override
            public void zoomApplied(ZoomEvent e) {
                if(e.isZoomIn()){
                    zoomR += 0.3;
                }
                else{
                    zoomR -= 0.3;
                }
                multiRenderer.setChartValuesTextSize(10*zoomR);
                multiRenderer.setLabelsTextSize(15*zoomR);

            }
    };

    mChart = (GraphicalView) ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
    mChart.addZoomListener(listener, true, true);
    chartContainer.addView(mChart);

我希望有人能帮助我

4

1 回答 1

0

问题是您只为第一个比例设置 X 轴范围。因此,您要么删除setXAxis*呼叫,要么为您拥有的第二个规模添加类似的呼叫:

multiRenderer.setXAxisMin(-1, 1);
multiRenderer.setXAxisMax(2, 1);
于 2013-05-02T17:56:24.773 回答