【问题标题】:Custom axis labels for Core Plot核心图的自定义轴标签
【发布时间】:2015-02-27 05:35:14
【问题描述】:

我花了一个多星期的时间尝试为 Core Plot 自定义轴标签,但没有成功。我阅读了许多关于 SO 的教程和示例,并尝试在我的项目中复制它,但没有成功。请帮忙。

这是我的代码:

// 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];

这是我的数据结构:

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];

如果我将 labelingPolicy 转换为 CPTAxisLabelingPolicyAutomatic,它将显示默认标签,但至少可以确认标签没有被其他东西隐藏。不幸的是,我没有足够的声誉来发布图片。

请帮助找出错误,那里一定有一些愚蠢的东西。

【问题讨论】:

    标签: charts core-plot labeling


    【解决方案1】:

    您将自定义标签定位在可见绘图范围之外。 xRange 涵盖 800 到 1,200 之间的值(length 为 400)。标签位于 1、2、3 等处。使用标签值作为tickLocation

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

    【讨论】:

    • 是的,就是这样。非常感谢,埃里克。你真是个天才。
    • @hsh @Eric 它对我不起作用 x 轴标签显示来自 xLocations 的值而不是来自 xLabels 标题。
    • @MuhammadYawarAli 这个问题已经有一年多了。请提出一个新问题,详细说明您的情况,我们可以提供帮助。请务必在问题上使用core-plot 标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多