Search code examples
androidandroid-studioandroid-graphview

How can i draw lines of ranges in a line graph using grahpview on android?


I'm new using the GraphView Library but i need to show a graph like this below

i'd been tried modifying some demos from GraphView but idk how to do it https://drive.google.com/file/d/1qi4HAPqBeewZ9bHfsRjuc0lmWhW9SbxU/view?usp=sharing


Solution

  • I code a workarround solution using the code below.

    i'm just hidding the default grid lines and drawing straight lines changing his colors

    @Override
    public void initGraph(GraphView graph) {
        DataPoint[] minPoints = new DataPoint[30];
        DataPoint[] maxPoints = new DataPoint[30];
        DataPoint[] teoricPoints = new DataPoint[30];
        DataPoint[] basePoint = new DataPoint[1];
        for (int i = 0; i < 30; i++) {
            //points[i] = new DataPoint(i, Math.sin(i*0.5) * 20*(Math.random()*10+1));
            minPoints[i] = new DataPoint(i, 20);
            maxPoints[i] = new DataPoint(i, 45);
            teoricPoints[i] = new DataPoint(i, 30);
        }
        basePoint[0] = new DataPoint(0, 0);
        LineGraphSeries<DataPoint> series = new LineGraphSeries<>(minPoints);
        LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(maxPoints);
        LineGraphSeries<DataPoint> series3 = new LineGraphSeries<>(teoricPoints);
        LineGraphSeries<DataPoint> series4 = new LineGraphSeries<>(basePoint);
    
        graph.getGridLabelRenderer().setGridColor(Color.RED);
        graph.getGridLabelRenderer().setHighlightZeroLines(false);
        graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.GREEN);
        graph.getGridLabelRenderer().setVerticalLabelsColor(Color.RED);
        graph.getGridLabelRenderer().setVerticalLabelsVAlign(GridLabelRenderer.VerticalLabelsVAlign.ABOVE);
        graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.NONE);
        graph.getGridLabelRenderer().reloadStyles();
    
    
        // styling viewport
        graph.getViewport().setBackgroundColor(Color.argb(255, 222, 222, 222));
        graph.getViewport().setDrawBorder(true);
        graph.getViewport().setBorderColor(Color.BLUE);
    
        // styling series
        series.setTitle("Random Curve 1");
        series.setColor(Color.GREEN);
    
        series.setThickness(8);
    
        series2.setTitle("Random Curve 2");
        series2.setColor(Color.GREEN);
    
        series2.setThickness(8);
    
        series3.setTitle("Random Curve 3");
        series3.setColor(Color.GREEN);
    
        series3.setThickness(8);
    
        series4.setTitle("Random Curve 4");
        series4.setColor(Color.GREEN);
    
        series4.setThickness(8);
    
        graph.getLegendRenderer().setFixedPosition(150, 0);
    
    
    
        graph.addSeries(series);
        graph.addSeries(series2);
        graph.addSeries(series3);
        graph.addSeries(series4);
    
    }