代码之家  ›  专栏  ›  技术社区  ›  olk04

android graphview使用setNumVerticalLabels显示错误的图形

  •  1
  • olk04  · 技术社区  · 12 年前

    我测试了android graphview库,发现了这种行为: 我使用了最新的GraphViewDemos和第一个SimpleGraph示例。它显示了一个具有正确数据的折线图。(y轴值为1,2,3)

    GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
                new GraphViewData(1, 2.0d)
                , new GraphViewData(2, 1.5d)
                , new GraphViewData(2.5, 3.0d) // another frequency
                , new GraphViewData(3, 2.5d)
                , new GraphViewData(4, 1.0d)
                , new GraphViewData(5, 3.0d)
        });
    

    最大值是三(对不起,我不能发布图像),所有其他坐标都是正确的。

    如果我加上这些行

    graphView.getGraphViewStyle().setNumVerticalLabels(5);
    graphView.setVerticalLabels( new String[]{"4","3","2","1","0"});
    

    之前

    LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
    layout.addView(graphView);
    

    在改变y轴的代码中,我得到了一个图,其中最大值不是三,而是四。所有其他坐标的y值都是错误的。

    为什么完整的图形会发生变化,而不仅仅是y轴?

    2 回复  |  直到 12 年前
        1
  •  0
  •   appsthatmatter    12 年前

    使用以下行:

    graphView.setVerticalLabels( new String[]{"4","3","2","1","0"});
    

    您可以为图形设置静态标签。因此,垂直标签(y值)不再与数据链接。

    此行用于动态标签。您可以修改将要生成的标签的计数。

    graphView.getGraphViewStyle().setNumVerticalLabels(5);
    

    但是你使用的是静态标签,所以这条线没有意义。

        2
  •  0
  •   Anshul Bansal    12 年前

    http://android-graphview.org/

    访问此页面并滚动到教程的自定义标签格式设置程序部分。

    GraphView graphView = new LineGraphView(this, "example");
    graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
      @Override
    public String formatLabel(double value, boolean isValueX) {
      if (isValueX) {
         if (value < 5) {
            return "small";
         } else if (value < 15) {
            return "middle";
         } else {
            return "big";
         }
      }
        return null; // let graphview generate Y-axis label for us
    }
       });
    

    基本上,您必须将实际的y值与您提供的静态垂直标签进行映射