繁体   English   中英

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

[英]Change the Y-axis labels and lines dynamically in 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)];

编辑::

登录输出

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

你能帮我吗...

谢谢

您的问题是在最后指定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))];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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