2014-01-30 20 views
3

目前,下面的代碼顯示附條形圖,與包括小數,並在2GraphView垂直標籤在整數從0遞增

我的問題是啓動一個規模:是否有一個啓動Y-方式軸標籤從0開始,並且整數增加到數據的最大值?例如在此,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

回答

10

首先要知道的是,如果你讓GraphView管理的Y規模,它會顯示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 
+0

這非常明確和有益的,謝謝! – otherdan

+0

不客氣。不要忘記投票和標記解決;) – user3261759

+0

真的很有用,非常感謝! –