无法从核心图,RealTimePlot中删除或隐藏轴

时间:2014-04-15 14:24:12

标签: ios objective-c core-graphics core-animation core-plot

如标题中所述,我已成功将CorePlot添加到我的项目中,并且按预期工作。除了显示的轴标签,我无法删除或隐藏。这是我的renderInLayer代码,

-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
    CGRect bounds = layerHostingView.bounds;
#else
    CGRect bounds = NSRectToCGRect(layerHostingView.bounds);
#endif

    CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:bounds] autorelease];
    [self addGraph:graph toHostingView:layerHostingView];

    graph.plotAreaFrame.paddingTop    = 0.0;
    graph.plotAreaFrame.paddingRight  = 0.0;
    graph.plotAreaFrame.paddingBottom = 0.0;
    graph.plotAreaFrame.paddingLeft   = 0.0;
    graph.plotAreaFrame.masksToBorder = NO;

    // Create the plot
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot.identifier     = kPlotIdentifier;
    dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;

    CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
    lineStyle.lineWidth              = 3.0;
    lineStyle.lineColor              = [CPTColor whiteColor];
    dataSourceLinePlot.dataLineStyle = lineStyle;

    dataSourceLinePlot.dataSource = self;
    dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationCurved;
    [graph addPlot:dataSourceLinePlot];

    // Plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 2)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(1)];

    [dataTimer invalidate];
    [dataTimer release];

    if ( animated ) {
        dataTimer = [[NSTimer timerWithTimeInterval:1.0 / kFrameRate
                                             target:self
                                           selector:@selector(newData:)
                                           userInfo:nil
                                            repeats:YES] retain];
        [[NSRunLoop mainRunLoop] addTimer:dataTimer forMode:NSRunLoopCommonModes];
    }
    else {
        dataTimer = nil;
    }
}

用糟糕的数据视觉效果,

enter image description here

修改

我试过这个,但它没有用,

 CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
 CPTXYAxis *axis          = axisSet.xAxis;

 axis.hidden = YES;
 for (CPTAxisLabel *axisLabel in axis.axisLabels) {
      axisLabel.contentLayer.hidden = YES;
 }

2 个答案:

答案 0 :(得分:2)

好的,所以我设法找到了自己的答案。如果将来有人想要删除标签。那么请这样做,

axis.labelingPolicy = CPTAxisLabelingPolicyNone;

如果你想完全删除图表的轴,那么。

graph.axisSet = nil;

一切都消失了,你现在有了清晰漂亮的图形:)

答案 1 :(得分:1)

您可以简单地隐藏所有标签。

CPTAxis *axis = xAxis;

axis.hidden = YES;
for (CPTAxisLabel *axisLabel in axis.axisLabels) {
    axisLabel.contentLayer.hidden = YES;
}

我发布了自定义xAxis标签的方式:

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)_graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.majorIntervalLength         = CPTDecimalFromString(@"5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.minorTicksPerInterval       = 4;
x.title = @"";
x.titleTextStyle = axisTitleStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone; //No labels provided; user sets labels and tick locations.
//x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 5.0f;
x.tickDirection = CPTSignNegative;

CPTMutableTextStyle *axisLabelStyle = [CPTMutableTextStyle textStyle];
axisLabelStyle.fontName = @"Helvetica Neue Thin";
axisLabelStyle.fontSize = 8.0f;
NSArray *lastThirtyDates = [self.analyticsDataController lastThirtyDays];
NSUInteger daysCount = 31;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:daysCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:daysCount];
NSMutableSet *xMinorLocations = [NSMutableSet setWithCapacity:daysCount];

for (//iterate over your data source for x values and create your custom labels) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:@"" textStyle:axisLabelStyle];
        CGFloat location = i;
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        if (label) {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
}

x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
x.minorTickLocations = xMinorLocations;