Search code examples
androidandroid-graphview

How to set Legend on graphview for particular line?


i using GraphView in my app.. that graphview have Legend option.. it's fine .. but the legend will show all the draw line in my graphview.. my code..

   GraphViewSeries g1 = new GraphViewSeries("", new GraphViewSeriesStyle(Color.WHITE, 1/3),new GraphViewData[] {
            new GraphViewData(-5,0d),
            new GraphViewData(5,0d),

        });
 GraphViewSeries g2 = new GraphViewSeries("", new GraphViewSeriesStyle(Color.WHITE, 1/3),new GraphViewData[] {
            new GraphViewData(0,-5d),
            new GraphViewData(0,5d),

        });
     GraphViewSeries red = new GraphViewSeries("Red", new GraphViewSeriesStyle(Color.RED, 3),new GraphViewData[] {
        new GraphViewData(-4,-4d),
        new GraphViewData(4,4d),

    });

 GraphViewSeries green = new GraphViewSeries("Green", new GraphViewSeriesStyle(Color.GREEN, 3),new GraphViewData[] {
        new GraphViewData(-4,-3d),
        new GraphViewData(-4,0d),
        new GraphViewData(0.6,0d),
        new GraphViewData(0.6,4d),

    });

   GraphView graphView = new LineGraphView(this.getActivity(), " "); 

   graphView.setShowLegend(true);
    graphView.setLegendAlign(LegendAlign.BOTTOM);
    graphView.setLegendWidth(250);

this code shows all the four lines to Legend..

but i want only add two particular line(red & Green) into Legend.. how it's done.. kindly tell easiest way to done this..

thanks in advance..


Solution

  • There is no build-in option to do this. But you can modify the source code of graphview.

    The drawing of the legend is done at the drawLegend method in the GraphView.java file: https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/GraphView.java#L463

    In Line 491 is the loop where the series names were drawn:

    for (int i=0; i<graphSeries.size(); i++) {
    

    If you do not want to modify the source code or you don't have the opportunity, you could override this method (it is protected).