更改Core Plot饼图标题的外观

时间:2012-03-19 00:38:55

标签: iphone ios graph charts pie-chart

我想在我的饼图中使用纯黑色背景,我使用Core Plot生成。我有很好的图表渲染,但标题是默认的blackColor,很难在黑色背景上看到。我知道它正在显示,因为当我改变主题时,我可以看到它是黑色的。谁能像我对数据标签那样告诉我如何更改文本颜色?

以下是我的代码片段......

    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    CPTGraphHostingView *hostingView = (CPTGraphHostingView*)self.view;
    hostingView.hostedGraph = graph;

    CPTPieChart *pieChart = [[CPTPieChart alloc] init];
    pieChart.dataSource = self;
    pieChart.pieRadius = 80.0;
    pieChart.identifier = @"PieChart1";
    pieChart.startAngle = M_PI_4;
    pieChart.sliceDirection = CPTPieDirectionCounterClockwise;

    [graph addPlot:pieChart];
    graph.title = @"Proportion of Sessions per Sport";

}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
    static CPTMutableTextStyle *whiteText = nil;
    if (!whiteText) {
        whiteText = [[CPTMutableTextStyle alloc] init];
        whiteText.color = [CPTColor whiteColor];
    }

    CPTLayer *layer = [[CPTLayer alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];
    CPTTextLayer *newLayer = nil;
    newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [pieLabels objectAtIndex:index]] style:whiteText];
    [layer addSublayer:newLayer];
    return newLayer;
}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [self.pieData count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    return [self.pieData objectAtIndex:index];
}

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
    return [NSString stringWithFormat:@"Pie Slice %u", index];
}

非常感谢您提供的任何协助/建议/链接...

1 个答案:

答案 0 :(得分:5)

我在Core Plot示例代码中找到了他一直盯着我的答案......

对于任何绊倒这个问题的人来说,答案是:

CPTMutableTextStyle *whiteText = [CPTMutableTextStyle textStyle];
    whiteText.color = [CPTColor whiteColor];
    graph.titleTextStyle = whiteText;
    graph.title = @"Your Title Here...";