2016-09-21 105 views
1

我正在使用GraphView庫,我想在X軸上顯示一年中的月份。我的問題是標籤在「肖像」模式下重疊。它在「風景」模式下看起來更好,但它們也是重疊的標籤。如何在graphview(Android Studio)中的X軸上設置固定的水平標籤

這是我的圖表外觀上的 「肖像」 模式:

enter image description here

這是我的圖表外觀上的 「風景」 模式(更好):

enter image description here

當我試圖製作較短的標籤(1月,2月,3月,4月等)或更小的字體大小它變得更糟(它看起來像創建更多的垂直線):

enter image description here

那是我的代碼:

GraphView graph_LastYear = (GraphView) findViewById(R.id.graph_yearly); 
    GraphView graph_Last3Months = (GraphView) findViewById(R.id.graph_last3month); 

    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{ 

      new DataPoint(1, Sallries_WholeYear.get(0).getNetoPayment()), 
      new DataPoint(2, Sallries_WholeYear.get(1).getNetoPayment()), 
      new DataPoint(3, Sallries_WholeYear.get(2).getNetoPayment()), 
      new DataPoint(4, Sallries_WholeYear.get(3).getNetoPayment()), 
      new DataPoint(5, Sallries_WholeYear.get(4).getNetoPayment()), 
      new DataPoint(6, Sallries_WholeYear.get(5).getNetoPayment()), 
      new DataPoint(7, Sallries_WholeYear.get(6).getNetoPayment()), 
      new DataPoint(8, Sallries_WholeYear.get(7).getNetoPayment()), 
      new DataPoint(9, Sallries_WholeYear.get(8).getNetoPayment()), 
      new DataPoint(10, Sallries_WholeYear.get(9).getNetoPayment()), 
      new DataPoint(11, Sallries_WholeYear.get(10).getNetoPayment()), 
      new DataPoint(12, Sallries_WholeYear.get(11).getNetoPayment()) 
    } 
    ); 



    series.setDrawDataPoints(true); 
    series.setColor(Color.BLACK); 
    series.setDrawDataPoints(true); 
    series.setDataPointsRadius(10); 
    series.setThickness(8); 

    graph_LastYear.addSeries(series); 
    graph_LastYear.setTitle("Last Year"); 
    graph_LastYear.setTitleTextSize(60); 

    graph_LastYear.getViewport().setScalable(true); 
    graph_LastYear.getViewport().setScrollable(true); 



    graph_LastYear.getGridLabelRenderer().setTextSize(40); 
    graph_LastYear.getGridLabelRenderer().setNumHorizontalLabels(12); 
    graph_LastYear.getGridLabelRenderer().setGridColor(Color.BLUE); 


    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph_LastYear); 
    staticLabelsFormatter.setHorizontalLabels(new String[]{getString(R.string.Janury_Month), getString(R.string.February_Month), getString(R.string.March_Month), getString(R.string.April_Month), getString(R.string.My_Month), getString(R.string.June_Month), getString(R.string.July_Month), getString(R.string.August_Month), 
      getString(R.string.September_Month), getString(R.string.October_Month), getString(R.string.November_Month), getString(R.string.December_Month)}); 
    graph_LastYear.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); 

謝謝您的幫助!

回答

0

可以垂直通過使用旋轉標貼:

GridLabelRenderer renderer = graphView.getGridLabelRenderer(); 
renderer.setHorizontalLabelsAngle(90); 

或者做一些標貼隱形如果沒有x軸的值是通過使用FOLL

GridLabelRenderer gridLabel = graphView.getGridLabelRenderer(); 
gridLabel.setLabelFormatter(new DefaultLabelFormatter(){ 
      @Override 
      public String formatLabel(double value, boolean isValueX) { 
       if (isValueX) { 
        // return as Integer 
        if (value % Commons.getDivisier(finalCategoryArray.length) == 0){ 
         return ""+ finalCategoryArray[((int) value)]; 
        }else { 
         return null; 
        } 

       } else { 
        // show currency for y values 
        return super.formatLabel(value, isValueX); 
       } 
      } 
     }); 

public static double getDivisier(int length) { 
     if (length < 5){ 
       return 1; 
     }else if (length < 10){ 
      return 2; 
     }else if (length < 50){ 
      return 5; 
     }else if (length < 100){ 
      return 10; 
     } 
     return 1; 
    } 
過大
相關問題