2017-01-27 3 views
3

은 이전에 우리는 다음과 같이 막대 차트 데이터의 X 축을 설정할 수 있습니다 : -가로 막 대형 차트 데이터에 x 축을 설정하는 방법 - ios-charts?

ios-charts 라이브러리의 업데이트 된 최신에서 BarChartData *data = [[BarChartData alloc] initWithXVals:xvals dataSets:dataSets];

는, 구문이 아래로 변경 : - BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];

방법 설정 x 축은 BarChartData에 있습니까?

+0

당신이 x 축에 표시 할 필요가 당신이 나를 알 수 있습니까? 또는 당신이 달성하려고하는 스크린 샷을 제공하여 적절한 아이디어를 얻을 수 있습니까? – CodeChanger

+0

x 값은'IAxisValueFormatter'와 그것의 델리게이트 메소드'stringForValue'에 의해 표시된다는 것을 알게되었습니다. 내 viewController IAxisValueFormatter 사용하려고하면 다음 오류가 발생합니다 : -'프로토콜 선언을 찾을 수 없습니다'. [이 답변] (http://stackoverflow.com/a/39149070/988169) 신속하게 작업하는 동안 접근 방식을 공유했습니다. 당신은 나에게 객관적으로 c라고 말할 수 있습니까? – pkc456

답변

3

Answer (swift version)의 도움으로 막대 차트에 xAxis 문자열 값을 Objective-C에 표시 할 수 있습니다.

NSMutableArray *xValsDietArray; 

차트 데이터를 설정하십시오. 다음과 같이 기본 예입니다 : - 다음과 같이

for (int i=0; i<dietArray.count; i++) { 
    DietInput *obj = [dietArray objectAtIndex:i]; 
    NSString *dateStr = @"Jan"; 
    [xValsDietArray addObject:dateStr];//Maintain this xValsDietArray. It will be used later on while setting x values. This contains array of x-axis values 
    [yvals addObject:[[BarChartDataEntry alloc]initWithX:[[xValsDietArray objectAtIndex:i] doubleValue] y:obj.calorieInput.doubleValue data:xValsDietArray] ]; 
} 

BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yvals label:@"Water Consumed"]; 
NSMutableArray *dataSets = [[NSMutableArray alloc] init]; 
[dataSets addObject:set1]; 
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets]; 
cell.chartView.data=data; 
cell.chartView.xAxis.valueFormatter = self;// Set the delegate to self. THIS IS THE MAIN ADDITION IN NEW CHART LIBRARY 

stringForValue 대리자 메서드를 구현 : -

- (NSString * _Nonnull)stringForValue:(double)value axis:(ChartAxisBase * _Nullable)axis 
{ 
    NSString *xAxisStringValue = @""; 
    int myInt = (int)value; 

    if(xValsDietArray.count > myInt) 
     xAxisStringValue = [xValsDietArray objectAtIndex:myInt]; 

    return xAxisStringValue; 
} 
+0

하지만 어떻게 할 수있는 BarChartDataEntry에서 값을 설정하고 대리자 메서드를 사용하는 동안 뭔가 의심스러워서 내가 용의자라고 – CodeChanger

+0

이것은 나를 위해 일했습니다. 고맙습니다. – UIResponder

1

당신은 인덱스로 IAxisValueFormatter의 같은 방법 아래 사용하고 xArray에서 데이터를 가져 그 안에 사용자 정의 X values를 표시 할 수 있습니다.

#pragma mark - IAxisValueFormatter 

- (NSString *)stringForValue:(double)value 
         axis:(ChartAxisBase *)axis 
{ 
    NSString *xValue = [xArray objectAtIndex:value]; 
    return xValue; 
} 

IAxisValueFormatter의 위임 방법으로 사용할 수 있습니다.

희망이 있으면 차트에 xAxis를 설정하는 데 도움이됩니다.

+0

나는 똑같이했다. 그러나 가치는 색인으로 취급되지 않습니다. – pkc456

+0

BarChartDataEntry 코드를 보여줌으로써 문제를 쉽게 해결할 수 있습니까? – CodeChanger

+0

'[[BarChartDataEntry alloc] initWithX : idx y : yValue];'이건 하나. – CodeChanger