2016-06-29 1 views
0

에 미친 버그 :나는이 같은 도메인 라벨 형식을 샘플 코드 SimpleXYPlotActivity를 변경 setDomainValueFormat

// create a couple arrays of y-values to plot: 
    Number[] domainLabels = {1, 2, 3, 6, 7, 8, 9, 10, 13, 14}; 
    Number[] series1Numbers = {1, 4, 2, 8, 4, 16, 8, 32, 16, 64}; 
    Number[] series2Numbers = {5, 2, 10, 5, 20, 10, 40, 20, 80, 40}; 

    // turn the above arrays into XYSeries': 
    // (Y_VALS_ONLY means use the element index as the x value) 
    //XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1"); 
    XYSeries series1 = new SimpleXYSeries(Arrays.asList(domainLabels), Arrays.asList(series1Numbers), "Series1"); 
    //XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2"); 
    XYSeries series2 = new SimpleXYSeries(Arrays.asList(domainLabels), Arrays.asList(series2Numbers), "Series2"); 

    //...other lines 

    plot.setDomainValueFormat(new Format() { 
     @Override 
     public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 
     int day = ((Number) obj).intValue(); 
     System.out.println(day); 
     return new StringBuffer(day); 
     } 
     @Override 
     public Object parseObject(String source, ParsePosition pos) { 
     return null; 
     } 
    }); 

    // reduce the number of range labels 
    //plot.setTicksPerRangeLabel(3); 

    // rotate domain labels 45 degrees to make them more compact horizontally: 
    plot.getGraphWidget().setDomainLabelOrientation(-45); 

내가 왜 몰라,하지만 줄거리는 다음과 같은 도메인 라벨을 인쇄 : 1, 2, 3, 5, 1,235,1, 2, 3, 6, 7, 8, 9, 10, 13, 14

왜 이런 미친 행동을 했나요? ?

답변

1

위의 코드를 실행하면 전혀 레이블이 인쇄되지 않습니다. 하지만 대체 할 경우 :

return new StringBuffer(day); 

로 :

return toAppendTo.append(day); 

그런 다음 레이블이 나타납니다. 이동 ...

포맷터가 각 도메인 눈금 레이블과 연관된 부동 소수점 x- 값을 자릅니다. 이 부동 소수점 구성 요소가있는 이유는 SimpleXYSeries를 인스턴스화 할 때 자신의 xVals를 지정하기 때문입니다. 나는 당신이 정말로 원하는 것은 xVals를 실제로 만들지 않기 때문에 iVals만을 지정하는 것이라고 생각한다.

나는 이러한 변경을했다 :

첫째,이 같은 iVals에서 xVals을 만들려면 XYSeries 인스턴스를 만들 :

XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1"); 
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2"); 

다음과 같이 도메인 라벨 조회 배열을 사용하는 도메인 포매터를 업데이트 : obj가 제 float로 변환 된 다음 iVal로서 사용되기 전에 int로 반올림 왜 사람들 궁금 들어

plot.setDomainValueFormat(new Format() { 
      @Override 
      public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 
       int i = Math.round(((Number) obj).floatValue()); 
       return toAppendTo.append(domainLabels[i]); 
      } 
      @Override 
      public Object parseObject(String source, ParsePosition pos) { 
       return null; 
      } 
     }); 

: 이는 일을 방지 할 필요가있다 float 값을 int 값으로 변환 할 때 발생하는 자연스러운 바닥재 동작입니다. 예를 들어 1의 Double 표현에 대해 intValue를 호출하는 경우 내부적으로 0.99999999 ...로 표시되고 1로 반올림되지 않고 변환으로 float됩니다.