在核心图中动态更改Y轴标签和线条

时间:2014-09-05 06:00:28

标签: ios core-plot

我想动态更改Y轴。我有最大和最小文本字段。当我更改最大或最小字段时,图形将更改Y轴。

我试过跟随,但只有两条线来了。请检查以下代码。

这是配置Y轴的方法。

CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy              = CPTAxisLabelingPolicyNone;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle          = majorGridLineStyle;
y.minorGridLineStyle          = minorGridLineStyle;
y.majorTickLength = 0.0;

NSMutableSet *yLabels = [NSMutableSet setWithCapacity:4];
NSMutableSet *yLocations = [NSMutableSet setWithCapacity:4];

NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];

CGFloat max_value = [[preferences valueForKey:MAX_KEY] integerValue];
CGFloat min_value = [[preferences valueForKey:MIN_KEY] integerValue];

NSLog(@"max_value%f",max_value);

NSInteger range = ((max_value-min_value)*10)/100;

NSLog(@"range%d",range);
newmaxValue = (max_value-min_value) - range;
newMinValue = min_value - range;

yAxisLabels = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%.f",newMinValue], [NSString stringWithFormat:@"%.f",newMinValue+20],[NSString stringWithFormat:@"%.f",newmaxValue-20],[NSString stringWithFormat:@"%.f",newmaxValue], nil];

yAxiscustomTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:newMinValue], [NSDecimalNumber numberWithInt:newMinValue+20], [NSDecimalNumber numberWithInt:newmaxValue-20], [NSDecimalNumber numberWithInt:newmaxValue],nil];


NSNumberFormatter * nFormatter = [[NSNumberFormatter alloc] init];
[nFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

for ( NSUInteger i = 0; i < [yAxisLabels count]; i++ ) {

    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%@",[yAxisLabels objectAtIndex:i]]  textStyle:axisTextStyle];
    label.tickLocation = CPTDecimalFromInteger([[yAxiscustomTickLocations objectAtIndex:i] integerValue]);
    //label.offset = y.labelOffset + y.majorTickLength;
    NSLog(@"%d",[[yAxiscustomTickLocations objectAtIndex:i] integerValue]);

    label.offset = y.majorTickLength+2;
    if (label) {
        [yLabels addObject:label];
        [yLocations addObject:[NSString stringWithFormat:@"%d",[[yAxiscustomTickLocations objectAtIndex:i] integerValue]]];
    }
    label = nil;
}

NSLog(@"newmaxValue%f",newmaxValue);
NSLog(@"newMinValue%f",newMinValue);

y.axisLabels = yLabels;
y.majorTickLocations = [NSSet setWithArray:yAxiscustomTickLocations];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)newmaxValue)];
y.labelFormatter = nFormatter;

而且我也设置了这样的范围

[self getCoreplotSpace].xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0.0f) length:CPTDecimalFromUnsignedInteger(1800.0f)];

[self getCoreplotSpace].yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger((NSInteger)newMinValue) length:CPTDecimalFromInteger((NSInteger)newmaxValue)];

EDIT ::

记录输出

  newmaxValue225.000000
  newMinValue-75.000000
  yAxisLabels(
    "-75",
    "-55",
    205,
    225
  )
   yAxiscustomTickLocations(
    "-75",
    "-55",
    205,
    225
)

你能帮帮我们......

由于

1 个答案:

答案 0 :(得分:1)

您的问题在指定y轴可见范围的末尾:

y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)newmaxValue)];

长度应该是范围,而不是newmaxValue:

y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)(newmaxValue-newminValue))];