Вопрос

Currently, the code below displays the attached bar graph, with a scale that includes decimals and starts at 2.

My question is: Is there a way to start the y-axis labels from 0, and increase in integers up to the maximum value of the data? For example in this, 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);

Current bar graph

Это было полезно?

Решение

First thing to know is that if you let GraphView manage the Y-scale, it will display 10 intervals, ie 11 values. So, if you have values from 0 to 10, or 0 to 20, displayed values will be integers.

You can manually set vertical bounds with GraphView.setManualYAxisBounds(double max, double min) In you case, you would want to use setManualYAxisBounds(5, 0), but you wouldn't have integers displayed. So you must use getGraphViewStyle().setNumVerticalLabels(6)

Here's a piece of code I use to dynamically adapt scale with values from 0 to 200, with a max scale value as close as possible from the max value of my data (I hope I'm understandable, lol)

  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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top