Change the appearance of the title of a Core Plot pie chart

久未见 提交于 2019-12-12 12:35:41

问题


I would like to use a plain black background to my pie chart which I generate using Core Plot. I have the chart rendering nicely but the title is in a default blackColor which is hard to see on a black background. I know it's displaying because when I change the theme I can see it in black. Can anyone tell me how to change the text colour as I did with the data labels?

Here is a snippet of my code...

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

Many thanks in advance for any assistance/advice/links offered...


回答1:


I found the answer it was staring me in the face the whole time in the Core Plot sample code...

For anyone else stumbling upon this question here is the answer:

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


来源:https://stackoverflow.com/questions/9763664/change-the-appearance-of-the-title-of-a-core-plot-pie-chart

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