2014-09-11 1 views
3

막 대형 차트를 만들었지 만 태블릿에서 x 축 레이블이 잘리는 문제가 발생했습니다. 휴대 전화의 올바른 레이아웃과 타블렛의 잘못된 레이아웃이 첨부됩니다.Androidplot - X 축 레이블 잘림

태블릿에서이 문제가 발생하는 이유와 해결 방법을 아는 사람이 있습니까?

전화 레이아웃

https://drive.google.com/file/d/0B4CxFPSlLEYpdGZCMm8xdGZHTlk/edit?usp=sharing

태블릿 레이아웃

https://drive.google.com/file/d/0B4CxFPSlLEYpakx1WFo3cGhNSTg/edit?usp=sharing

이 막대 차트를 만드는 내 코드입니다.

레이아웃 파일

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.usage.bigpondpro.MainActivity$PlaceholderFragment" 
     android:id="@+id/svDailyContainter" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <com.androidplot.xy.XYPlot 
       android:id="@+id/dailyXYPlot" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       androidPlot.title="Sample Bar Graph" 
       androidPlot.titleWidget.labelPaint.textSize="@dimen/title_font_size" 
       androidPlot.graphWidget.marginTop="20dp" 
       androidPlot.graphWidget.marginLeft="10dp" 
       androidPlot.graphWidget.marginBottom="20dp" 
       androidPlot.graphWidget.marginRight="10dp" 
       androidPlot.rangeLabelWidget.labelPaint.textSize="@dimen/range_label_font_size" 
       androidPlot.domainLabelWidget.labelPaint.textSize="@dimen/range_label_font_size" 
       />  

     </LinearLayout> 
    </ScrollView> 

활동

private void CreateGraph() 
    { 
     // initialize our XYPlot reference: 
     XYPlot plot = (XYPlot)this.findViewById(R.id.dailyXYPlot); 

     // Create 

a couple arrays of y-values to plot: 
     Number[] series1Numbers = GenerateGraphValues(); 

     // Turn the above arrays into XYSeries': 
     XYSeries series1 = new SimpleXYSeries(
     Arrays.asList(series1Numbers),   // SimpleXYSeries takes a List so turn our array into a List 
     SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
       "Series1");        // Set the display title of the series 

     BarFormatter series1Format = new BarFormatter(Color.rgb(51, 181, 229), Color.TRANSPARENT); 

     PointLabelFormatter plf = new PointLabelFormatter(); 
     plf.getTextPaint().setTextSize(18); 
     plf.getTextPaint().setColor(Color.BLACK); 
     series1Format.setPointLabelFormatter(plf); 


     series1Format.setPointLabeler(new PointLabeler() { 
      DecimalFormat df = new DecimalFormat("##0.00"); 

      public String getLabel(XYSeries series, int index) { 

       //need to check for null 
       if(series.getY(index) == null) return ""; 

       return df.format(series.getY(index)); 
      } 
     }); 



     // add a new series' to the xyplot: 
     plot.addSeries(series1, series1Format); 


     //Y axis config 
     plot.setRangeLabel("Values"); //label 
     plot.setRangeBoundaries(0, 110, BoundaryMode.FIXED); //scale 
     plot.setRangeStep(XYStepMode.SUBDIVIDE, 5); //steps 
     plot.getGraphWidget().getRangeLabelPaint().setTextSize(26); //font size 

     DecimalFormat nf = new DecimalFormat("#0"); 
     plot.setRangeValueFormat(nf); 

     //X Axs config 
     plot.setDomainLabel("Indexes"); 
     plot.setDomainStep(XYStepMode.SUBDIVIDE, 25); 
     // plot.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat()); 
     plot.getGraphWidget().getDomainLabelPaint().setTextSize(18); 
     plot.getGraphWidget().setDomainLabelVerticalOffset(5); 

     //other config 
     plot.getLegendWidget().setVisible(false); //hide legend 
     plot.getGraphWidget().getDomainGridLinePaint().setColor(Color.TRANSPARENT); //hide grid lines 
     plot.getGraphWidget().getRangeGridLinePaint().setColor(Color.TRANSPARENT); //hide grid lines 
     plot.getGraphWidget().setGridPaddingLeft(40); //give some padding 
     plot.getGraphWidget().setGridPaddingRight(40); //give some padding 
     plot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE); //background color 
     plot.getTitleWidget().setPaddingTop(10); //give some padding 

     //set bar width 
     BarRenderer<?> renderer = (BarRenderer<?>) plot.getRenderer(BarRenderer.class); 
     renderer.setBarWidth(25); 

     //change x lable orientation 
     plot.getGraphWidget().setDomainLabelOrientation(-90); 

     //set graph height and width 

     /** 
     For some reason when the device is in landscape mode the entire graph canvas is blank. 
     Through trial and error if in landscape mode and the screen width is multiplied by 0.96 the graph and all it's data appear again.....why??? 
     **/ 
     DisplayMetrics displaymetrics = new DisplayMetrics(); 
     ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displaymetrics); 

     int Width = (int) (displaymetrics.widthPixels * 0.96); 

     //default to portrait 
     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 750); 
     //need to check the orienation 
     if(IsLandscapeOrientation()) 
     { 
      lp = new LayoutParams(Width, 560); 
     } 

     plot.setLayoutParams(lp); 

    } 

    private Number[] GenerateGraphValues() 
    { 
     int min = 30; 
     int max = 100; 
     int PlotPoints = 25; 
     Number[] series1Numbers = new Number[PlotPoints]; 


     for(int x = 0; x < PlotPoints; x++){ 

      Random r = new Random(); 
      int randomNumber = r.nextInt(max - min + 1) + min; 
      series1Numbers[x] = randomNumber; 

     } 

     return series1Numbers; 

    } 

    private boolean IsLandscapeOrientation() 
    { 
     Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     int rotation = display.getRotation(); 

     boolean IsLandscape; 


     switch (rotation) { 
     case Surface.ROTATION_0: 
     case Surface.ROTATION_180: 
      IsLandscape = false; 
     break; 

     case Surface.ROTATION_90: 
     case Surface.ROTATION_270: 
      IsLandscape = true; 
     break; 

     default: 
      IsLandscape = true; 
     break; 
     } 

     return IsLandscape; 
    } 

답변

2

문제는 그래프 위젯 하단 여백에 충분한 공간이 될 것으로 보인다. 플롯의 XML에 다음을 추가하면 :

androidPlot.graphWidget.marginBottom="100dp" 

그런 다음 문제가 없어집니다. 100dp는 아마도 과장이므로 다이얼을해야하지만 아이디어를 얻을 수 있습니다 :)

+0

나는 androidPlot.graphWidget.marginBottom에 대해 20dp 값을 이미 설정했지만 30dp로 증가하면 내 문제가 해결되었습니다. – Mark