如何在graphview上为特定行设置图例?

时间:2014-09-17 11:16:50

标签: android android-graphview

我在我的应用程序中使用GraphView .. graphview有Legend选项..它很好..但是图例会显示我的graphview中的所有绘制线.. 我的代码..

   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);

此代码显示了Legend的所有四行。

但我只想在Legend中添加两个特定的行(红色和绿色)..它是如何完成的...请告诉最简单的方法来做到这一点..

提前感谢..

1 个答案:

答案 0 :(得分:0)

没有内置选项可以执行此操作。但是你可以修改graphview的源代码。

图例的绘制是在drawLegend文件中的GraphView.java方法完成的: https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/GraphView.java#L463

第491行是绘制系列名称的循环:

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

如果您不想修改源代码或者没有机会,可以覆盖此方法(受保护)。