Custom axis labels for Core Plot

南楼画角 提交于 2019-12-11 03:15:28

问题


I spent more than a week trying to have custom axis label for Core Plot, without success. I read many tutorials and examples on SO, and tried to replicate it in my project, but no success. Please help.

This is the code I have:

// Create an CPXYGraph and host it inside the view
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
CPTXYGraph *graph = (CPTXYGraph *)[theme newGraph];
hostView.hostedGraph = graph;

graph.paddingTop = 10.0;
graph.paddingLeft = 10.0;
graph.paddingRight = 10.0;
graph.paddingBottom = 10.0;

[graph.plotAreaFrame setPaddingTop:10.0f];
[graph.plotAreaFrame setPaddingLeft:55.0f];
[graph.plotAreaFrame setPaddingRight:10.0f];
[graph.plotAreaFrame setPaddingBottom:35.0f];

// Set up Plot Space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(800)
                                               length:CPTDecimalFromInteger(400)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(7000)
                                               length:CPTDecimalFromCGFloat(1000)];

// Set up Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(60);
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(7000);
x.minorTicksPerInterval = 3;
x.borderWidth = 0;
x.majorTickLength = 5.0f;
x.minorTickLength = 2.0f;
x.axisLineCapMax = [CPTLineCap openArrowPlotLineCap];
x.axisLineCapMax.size = CGSizeMake(8, 10);

x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

NSMutableSet *xLabels = [NSMutableSet setWithCapacity:[self.arrXValues count]];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[self.arrXValues count]];

CGFloat location = 0.0;
for (NSString *string in self.arrXValues) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string  textStyle:x.labelTextStyle];
    location++;
    label.tickLocation = CPTDecimalFromCGFloat(location);
    label.offset = x.titleOffset + x.majorTickLength;
    if (label) {
        [xLabels addObject:label];
        [xLocations addObject:[NSNumber numberWithFloat:location]];
    }
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromCGFloat(250);
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(800);
y.majorTickLength = 5.0f;
y.minorTickLength = 2.0f;
y.axisLineCapMax = [CPTLineCap openArrowPlotLineCap];
y.axisLineCapMax.size = CGSizeMake(8, 10);

// Set up the chart
CPTScatterPlot *linePlot = [[CPTScatterPlot alloc] init];
linePlot.identifier = @"AAPL";
CPTMutableLineStyle *lineStyle = [linePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 2.f;
lineStyle.lineColor = [CPTColor blueColor];
linePlot.dataLineStyle = lineStyle;

linePlot.dataSource = self;
[graph addPlot:linePlot];

And this is my data structure:

self.arrXValues = [NSMutableArray arrayWithObjects:@"900", @"930", @"1000", @"1010", @"1020", @"1030", @"1040", @"1050", @"1100", @"1130", nil];
self.arrYValues = [NSMutableArray arrayWithObjects:@"7600", @"7460", @"7700", @"7720", @"7680", @"7700", @"7720", @"7720", @"7680", @"7700", nil];

If I turn labelingPolicy to CPTAxisLabelingPolicyAutomatic, it will show the default labels, but at least affirms that the labels are not hidden by something else. Unfortunately, I don't have enough reputations to post images.

Please help spot the mistake, there must something silly out there.


回答1:


You're positioning the custom labels outside the visible plot range. The xRange covers values between 800 and 1,200 (length of 400). The labels are at 1, 2, 3, etc. Use the label value as the tickLocation:

label.tickLocation = CPTDecimalFromString(string);
[xLocations addObject:string];


来源:https://stackoverflow.com/questions/27715864/custom-axis-labels-for-core-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!