GraphView 双y轴

int[] ProbeType = { 0, 0, 0, 0, 0, 0,};
float[] chartX = { 0, 0, 0, 0, 0, 0,};
GraphView graph,graph2;
    private LineGraphSeries mSeries0 = new LineGraphSeries();
    private LineGraphSeries mSeries1 = new LineGraphSeries();
    private LineGraphSeries mSeries2 = new LineGraphSeries();
    private LineGraphSeries mSeries3 = new LineGraphSeries();
    private LineGraphSeries mSeries4 = new LineGraphSeries();
    private LineGraphSeries mSeries5 = new LineGraphSeries();
DisplayMetrics dm = getResources().getDisplayMetrics();

        graph = new GraphView(this);//获取图表画板
        graph.getViewport().setScalable(true);//设置缩放
        graph.getViewport().setScrollable(true);//设置滚动
        // set manual X bounds
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(10);//实验结束时一个界面显示出全部曲线。每隔1秒一个数据点,80个数据点需要80乘1=80秒

        // set manual Y bounds
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(0);//声波正弦曲线以y轴0值为基准线上下波动,大小为-127 到+128
        graph.getViewport().setMaxY(60);  //所有坐标轴的y值上下范围定为300 > 128
        graph.getViewport().setBackgroundColor(Color.LTGRAY);  
        //graph.getGridLabelRenderer().setHorizontalAxisTitle("时间");
        graph.getGridLabelRenderer().setVerticalAxisTitle("温度(℃)  压强(kPa)");
        graph.getGridLabelRenderer().setPadding(40);
        graph.getGridLabelRenderer().setVerticalLabelsColor(Color.BLUE);
        graph.getGridLabelRenderer().setHighlightZeroLines(false);//零线不高亮
        //graph.getGridLabelRenderer().setGridStyle(GridStyle.NONE);;//不显示格线
        graph.getGridLabelRenderer().setGridColor(Color.CYAN);
        //graph.setBackgroundResource(R.color.up_half_background_color);
        LayoutParams lPgraph = new LayoutParams(dm.widthPixels*2/3,LayoutParams.WRAP_CONTENT);
        lPgraph.setMargins(10, 0, 50, 0);
        graph.setLayoutParams(lPgraph);
        graph.setVisibility(View.VISIBLE);//VISIBLE:0 意思是可见的,INVISIBILITY:4 意思是不可见的,但还占着原来的空间,GONE:8 意思是不可见的,不占用原来的布局空间
        // legend 设置图例
        mSeries0.setTitle("温度");
        mSeries0.setColor(Color.BLUE);
        mSeries1.setTitle("压强");
        mSeries1.setColor(Color.RED);
        //把数据集放入画板
        graph.addSeries(mSeries0);
        graph.getLegendRenderer().setVisible(true);//图例可见
        graph.getLegendRenderer().setAlign(LegendAlign.TOP);//图例位置
        //graph.getGridLabelRenderer().setVerticalLabelsVisible(false);//隐藏y轴标签
        //graph.getGridLabelRenderer().setHorizontalLabelsVisible(false);//隐藏x轴标签

        // set second scale
        graph.getSecondScale().addSeries(mSeries1); //把数据集放入画板的第二刻度,相当于graph.addSeries(mSeries2); 不需要再graph.addSeries(mSeries2);
        // the y bounds are always manual for second scale
        graph.getSecondScale().setMinY(0);
        graph.getSecondScale().setMaxY(500);
        graph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(Color.RED);//第二坐标轴垂直标签颜色



        graph2 = new GraphView(this);//获取图表画板
        graph2.getViewport().setScalable(true);//设置缩放
        graph2.getViewport().setScrollable(true);//设置滚动
        // set manual X bounds
        graph2.getViewport().setXAxisBoundsManual(true);
        graph2.getViewport().setMinX(0);
        graph2.getViewport().setMaxX(10);//实验结束时一个界面显示出全部曲线。每隔1秒一个数据点,80个数据点需要80乘1=80秒

        // set manual Y bounds
        graph2.getViewport().setYAxisBoundsManual(true);
        graph2.getViewport().setMinY(0);//声波正弦曲线以y轴0值为基准线上下波动,大小为-127 到+128
        graph2.getViewport().setMaxY(50);  //所有坐标轴的y值上下范围定为300 > 128
        //graph2.getGridLabelRenderer().setHorizontalAxisTitle("时间");
        graph2.getGridLabelRenderer().setVerticalAxisTitle("氧气 、甲烷 、湿度 (%)    二氧化碳(ppm)");
        //graph2.getGridLabelRenderer().setLabelVerticalWidth(150); 
        //graph.getGridLabelRenderer().setVerticalLabelsAlign(Align.LEFT);  
        graph2.getGridLabelRenderer().setPadding(40);
        graph2.getGridLabelRenderer().setHighlightZeroLines(false);//零线不高亮
        //graph.getGridLabelRenderer().setGridStyle(GridStyle.NONE);;//不显示格线
        graph2.getGridLabelRenderer().setGridColor(Color.CYAN);
        //graph.setBackgroundResource(R.color.up_half_background_color);
        graph2.setLayoutParams(lPgraph);
        graph.setVisibility(View.VISIBLE);//VISIBLE:0 意思是可见的,INVISIBILITY:4 意思是不可见的,但还占着原来的空间,GONE:8 意思是不可见的,不占用原来的布局空间
        // legend 设置图例
        mSeries2.setTitle("氧气");
        mSeries2.setColor(Color.BLUE);
        mSeries3.setTitle("甲烷");
        mSeries3.setColor(Color.GREEN);
        mSeries4.setTitle("相对湿度");
        mSeries4.setColor(Color.YELLOW);
        mSeries5.setTitle("二氧化碳");
        mSeries5.setColor(Color.RED);

        //把数据集放入画板
        graph2.addSeries(mSeries2);
        graph2.addSeries(mSeries3);
        graph2.addSeries(mSeries4);
        graph2.getLegendRenderer().setVisible(true);//图例可见
        graph2.getLegendRenderer().setAlign(LegendAlign.TOP);//图例位置
        //graph.getGridLabelRenderer().setVerticalLabelsVisible(false);//隐藏y轴标签
        //graph.getGridLabelRenderer().setHorizontalLabelsVisible(false);//隐藏x轴标签

        // set second scale
        graph2.getSecondScale().addSeries(mSeries5);
        // the y bounds are always manual for second scale
        graph2.getSecondScale().setMinY(0);
        graph2.getSecondScale().setMaxY(500);
        graph2.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(Color.RED); //设置第二坐标尺垂直标签颜色
mSeries1.appendData(new DataPoint(chartX[index], fdat), true, 10);
chartX[index] += seq / 1000f;

GraphView 双y轴_第1张图片

你可能感兴趣的:(Android)