Change the appearance of the title of a Core Plot

2019-07-04 00:24发布

问题:

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...";