1

I encountered a problem when using a custom label in Core Plot:

  1. My Core Plot supports zoom.

  2. The x-axis label is custom x.axisLabels

On first load, the interface is normal, but when I narrow Core Plot, the axis labels overlap together.

How to design a level on the label? For example, when the minimum level only shows a year, in level display date, the maximum level shows the date.

- (void)initPlotGraph
{
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
    self.hostedGraph = graph;
    locationLabels = [[NSMutableArray alloc]init];

    graph.plotAreaFrame.paddingLeft   += 5.0;
    graph.plotAreaFrame.paddingTop    += 5.0;
    graph.plotAreaFrame.paddingRight  += 5.0;
    graph.plotAreaFrame.paddingBottom += 17.5;

    //[self setAllowPinchScaling:NO];

    // Setup scatter plot space
    plotSpace                       = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate              = self;

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)];
    plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)];

    [self initPlotAxis];
 }

- (void)initPlotAxis
{
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth            = 2;
    axisLineStyle.lineColor            = [CPTColor colorWithComponentRed:CPTFloat(0.11765) green:CPTFloat(0.49804) blue:CPTFloat(0.87451) alpha:CPTFloat(1)];

    CPTMutableLineStyle *axisTickLineStyle = [CPTMutableLineStyle lineStyle];
    axisTickLineStyle.lineWidth            = 0;

    CPTLineCap *lineCap = [CPTLineCap sweptArrowPlotLineCap];
    lineCap.size        = CGSizeMake(10, 10);
    lineCap.lineCapType = CPTLineCapTypeNone;

    // Axes
    // Label x axis with a fixed interval policy
    lineCap.lineStyle     = axisLineStyle;
    lineCap.fill          = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]];
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    x.majorIntervalLength = CPTDecimalFromInt(1);
    x.axisLineStyle       = axisLineStyle;
    x.axisConstraints     = [CPTConstraints constraintWithRelativeOffset:0];
    x.axisLineCapMax      = lineCap;
    x.axisLabels          = [self buildLabelTitle];
    x.labelingPolicy      = CPTAxisLabelingPolicyNone;
    x.minorTickLocations = [NSSet setWithArray:locationLabels];

    // Label y with an automatic label policy.
    lineCap.lineStyle       = axisLineStyle;
    lineCap.fill            = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]];
    CPTXYAxis *y = axisSet.yAxis;
    //    y.majorIntervalLength   = CPTDecimalFromDouble(5);
    y.majorTickLineStyle    = axisTickLineStyle;
    y.minorTickLineStyle    = axisTickLineStyle;
    y.axisLineStyle         = axisLineStyle;
    y.axisLineCapMax        = lineCap;
    y.axisConstraints       = [CPTConstraints constraintWithRelativeOffset:0];

    // Set axes
    graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
}

- (NSMutableSet*)buildLabelTitle
{
    NSMutableSet *newAxisLabels = [NSMutableSet set];

    CPTMutableTextStyle *textStyleB = [CPTMutableTextStyle textStyle];
    textStyleB.color = [CPTColor colorWithComponentRed:CPTFloat((float)0x09/0xFF) green:CPTFloat((float)0x31/0xFF) blue:CPTFloat((float)0x4A/0xFF) alpha:CPTFloat(1)];

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(corePlotNums - 9)length:CPTDecimalFromInt(10)];
    plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(corePlotNums + 1)];

    for ( NSUInteger i = 1; i < corePlotNums + 1; i++)
    {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[[m_labelStr objectAtIndex:i-1] substringWithRange:NSMakeRange(5, 11)] textStyle:textStyleB];
        newLabel.tickLocation = CPTDecimalFromUnsignedInteger(i);
        newLabel.offset = 5;
        [newAxisLabels addObject:newLabel];
        [newLabel release];
    }

    return newAxisLabels;
}

enter image description here

enter image description here

4

1 回答 1

0

If the user is allowed to change the plot ranges (i.e., by pinch zooming), use a plot space delegate to monitor changes to the plot space. Use the new plot ranges and the size of the plot area to determine what time scale to use and how many labels to display. Adjust the axis labeling properties as needed.

于 2013-05-11T14:06:20.690 回答