3

目前,下面的代码显示了附加的条形图,其刻度包含小数并从 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);

当前条形图

4

1 回答 1

11

首先要知道的是,如果让 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
于 2014-02-02T01:29:28.167 回答