简体   繁体   中英

Change the Y-axis labels and lines dynamically in core plot

I want to change the Y-axis dynamically. I have max and min text fields. When I change the max or min fields, the graph will change the Y-axis.

I have tried like following but only two line are coming. Please check the following code.

This is the method for Configure Y-axis.

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;

And also I am setting the range like this

[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::

Log OUTPUT

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

Could you please help me guys...

Thanks

Your problem is at the end where you specify the y axis visible range:

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

The length should be the range, not the newmaxValue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM