【问题标题】:GraphView vertical labels increment in integer from 0GraphView 垂直标签从 0 开始以整数递增
【发布时间】:2014-02-23 13:11:57
【问题描述】:

目前,下面的代码显示附加的条形图,其刻度包含小数并从 2 开始。

我的问题是:有没有办法从 0 开始 y 轴标签,并将整数增加到数据的最大值?比如这里,0,1,2,3,4,5?

barData = this.getIntent().getExtras().getString("GraphData");

            GraphViewSeries barGraphSeries = new GraphViewSeries(
                    new GraphViewData[] {
                            new GraphViewData(0, Integer.parseInt(barData
                                    .substring(0, barData.indexOf(",")))),
                            new GraphViewData(1, Integer.parseInt(barData
                                    .substring(barData.indexOf(",") + 1,
                                            barData.length()))) });

            GraphView statGraphView = new BarGraphView(this,
                    "Current Stat Graph");

            statGraphView.getGraphViewStyle().setGridColor(Color.BLACK);
            statGraphView.getGraphViewStyle().setHorizontalLabelsColor(
                    Color.BLACK);
            statGraphView.getGraphViewStyle().setVerticalLabelsColor(
                    Color.BLACK);
            String[] horLabels = { "Correct", "Incorrect" };
            statGraphView.setHorizontalLabels(horLabels);
            statGraphView.getGraphViewStyle().setNumHorizontalLabels(2);
            statGraphView.getGraphViewStyle().setNumVerticalLabels(10);



            statGraphView.addSeries(barGraphSeries);

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

【问题讨论】:

    标签: java android bar-chart android-graphview


    【解决方案1】:

    首先要知道的是,如果让 GraphView 管理 Y-scale,它将显示 10 个区间,即 11 个值。 因此,如果您的值介于 0 到 10 或 0 到 20 之间,则显示的值将是整数。

    您可以使用 GraphView.setManualYAxisBounds(double max, double min) 手动设置垂直边界 在您的情况下,您可能希望使用 setManualYAxisBounds(5, 0),但不会显示整数。 所以你必须使用 getGraphViewStyle().setNumVerticalLabels(6)

    这是我用来动态调整比例的一段代码,其值从 0 到 200,最大比例值尽可能接近我数据的最大值(我希望我可以理解,哈哈)

      int maxValue = ...    // here, you find your max value
      // search the interval between 2 vertical labels
      int interval;
      if (maxValue <= 55) {
          interval = 5; // increment of 5 between each label
      } else if (maxValue <= 110) {
          interval = 10; // increment of 10 between each label
      } else {
          interval = 20; // increment of 20 between each label
      }
      // search the top value of your graph, it must be a multiplier of your interval
      int maxLabel = maxValue;
      while (maxLabel % interval != 0) {
          maxLabel++;
      }
      // set manual bounds
      setManualYAxisBounds(maxLabel, 0);
      // indicate number of vertical labels
      getGraphViewStyle().setNumVerticalLabels(maxLabel / interval + 1);
      // now, it's ok, you should have a graph with integer labels
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多