首页 > 解决方案 > 如何使用图形视图的第二个比例在android中进行绘图

问题描述

我被一张图表困住了。下面是已经绘制了两条线(红色和蓝色)的图表的屏幕截图。问题是蓝色的。我想使用第二个刻度(即右侧的 Y 轴)和 X 轴来绘制这条线,目前它使用左侧的 Y 轴来绘制这条线,就像红色一样线。

我在 android 中使用 GRAPHVIEW 库。

在此处输入图像描述

 private void setBothSidedGraph() {
    //this is used to remove the default thickness on both axis
    graph.getGridLabelRenderer().setHighlightZeroLines(false);

    // this is used to remove the vertical lines from graph
    graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);

    // this will draw a border aroung graph and will also show both axis
    graph.getViewport().setDrawBorder(true);

    //setting the title for both the axis
    GridLabelRenderer gridLabel = graph.getGridLabelRenderer();
    // gridLabel.setHorizontalAxisTitle("Singles");
    // gridLabel.setVerticalAxisTitle("Volume (bbl)");


    // manunal bounds for left-Yaxis and setting manual bound will break it accordingly evenly

    graph.getViewport().setMinY(0);
    graph.getViewport().setMaxY(1600);
    graph.getViewport().setYAxisBoundsManual(true);


    arrDepth.add(0);
    arrDepth.add(1);
    arrDepth.add(2);
    arrDepth.add(3);
    arrDepth.add(4);
    arrDepth.add(5);


    arrSingles.add(650);
    arrSingles.add(650);
    arrSingles.add(650);
    arrSingles.add(650);
    arrSingles.add(650);
    arrSingles.add(650);






  /*  // *****Data for the lines to be drawn****//*/
    LineGraphSeries<DataPoint> line1 = new LineGraphSeries<>(new DataPoint[] {

            new DataPoint(0, 650),
            new DataPoint(1, 550),
            new DataPoint(2, 660),
            new DataPoint(3, 575),
            new DataPoint(4, 650)
    });
    */

    //adding DataPoints on Line to be shown
    for (int i = 0; i < arrTimeForGraph.size(); i++) {
        line1.appendData((new DataPoint(arrTimeForGraph.get(i), arrCirculatingPressureForGraph.get(i))), true, arrTimeForGraph.get(arrTimeForGraph.size() - 1).intValue());
        line2.appendData((new DataPoint(arrTimeForGraph.get(i), 150)), true, arrTimeForGraph.get(arrTimeForGraph.size() - 1).intValue(),false);

    }



    line1.setThickness(3);//Color of Line to be Drawn
    line1.setDataPointsRadius(7);
    line1.setDrawDataPoints(true);  //Lines to be drawn should hold points in it
    line1.setColor(Color.RED);      //Color of Line to be Drawn




    line2.setThickness(3);//Color of Line to be Drawn
    line2.setDataPointsRadius(7);
    line2.setDrawDataPoints(true);  //Lines to be drawn should hold points in it
    line2.setColor(Color.BLUE);      //Color of Line to be Drawn


    canvas = new Canvas();



    line2.draw(graph,canvas,true);


    //Lines added to graph to be graph
    graph.addSeries(line1);
    graph.addSeries(line2);

    graph.isLayoutDirectionResolved();
    // setting bounds for Right Yaxis and setNumverticalLabels(9) will break it in 9 parts.
    graph.getSecondScale().setMinY(0);
    graph.getSecondScale().setMaxY(240);
    graph.getGridLabelRenderer().setNumVerticalLabels(9);

    // graph.getGridLabelRenderer().setVerticalLabelsColor(Color.TRANSPARENT);

    // graph.setContentDescription("Trip Sheet Volume Vs. Depth");
    graph.getGridLabelRenderer().setPadding(50);
    graph.getGridLabelRenderer().setVerticalLabelsColor(Color.RED);
     graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.TRANSPARENT);

    graph.getViewport().setXAxisBoundsManual(true);
    graph.getViewport().setMinX(0);
    graph.getViewport().setMaxX(arrTimeForGraph.get(10));


    if (isTablet720) {
        gridLabel.setVerticalAxisTitleTextSize(40f);
        gridLabel.setHorizontalAxisTitleTextSize(40f);

        graph.setTitleTextSize(40);

    } else {
        gridLabel.setVerticalAxisTitleTextSize(30f);
        gridLabel.setHorizontalAxisTitleTextSize(30f);

        graph.setTitleTextSize(30);
    }


}

标签: android

解决方案


我相信当您执行以下操作时...

    graph.addSeries(line1);
    graph.addSeries(line2);

这些系列被添加到第一级。如果您想将第二个系列 ( line2) 添加到第二个刻度,那么您应该执行以下操作...

    graph.addSeries(line1);
    graph.getSecondScale().addSeries(line2);

推荐阅读