问题描述
我无法更改饼图的位置(我希望将其相对于图例留在上,以使图例不会覆盖饼图)。基本上,我想将其向左移动以显示图例。
I am unable to change the pie chart position (I would like it to be more left in respect to the legend so that the legend does not overlay the pie chart). Basically I would like to shift it towards the left to allow the legend to display.
我尝试修改以下属性(但对饼图位置没有明显影响):
I tried modifying the following properties (but with no apparent effect on the pie graph position):
// pieChartGraph is a CPTPieChart object
pieChartGraph.graph.position = CGPointMake(-100, 0);
// I also tried:
pieChart.position = CGPointMake(-100, 0);
// and also playing with:
pieChart.anchorPoint
这是我得到的,也是我的代码(顺便说一句,标题也没有显示):
Here is what I get and also my code (by the way, the title does not show either):
pieChartGraph = [[CPTXYGraph alloc] initWithFrame:self.pieChartgraphHostView.bounds];
self.pieChartgraphHostView.hostedGraph = pieChartGraph;
pieChartGraph.plotAreaFrame.masksToBorder = NO;
pieChartGraph.axisSet = nil;
pieChart.title = @"title"; // <-- BTW, the title does not show either
// 2 - Create chart
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.delegate = self;
pieChart.pieRadius = self.pieChartgraphHostView.bounds.size.width;
pieChartGraph.delegate = self;
pieChart.identifier = pieChartGraph.title;
pieChart.startAngle = CPTFloat(M_PI_4);
pieChart.sliceDirection = CPTPieDirectionClockwise;
pieChart.borderLineStyle = [CPTLineStyle lineStyle];
// 3 - Create gradient
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
// 4 - Add chart to graph
pieChart.pieRadius = pieChart.pieRadius / 2.3;
[pieChartGraph addPlot:pieChart];
self.dataForChart = [@[@(self.displayedLastDataPoint.percentageUnder), @(self.displayedLastDataPoint.percentageOver), @(self.displayedLastDataPoint.percentageClean), @(self.displayedLastDataPoint.percentageRemaining)] mutableCopy];
// Add legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:pieChartGraph];
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.entryFill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
theLegend.entryBorderLineStyle = [CPTLineStyle lineStyle];
theLegend.entryCornerRadius = CPTFloat(3.0);
theLegend.entryPaddingLeft = CPTFloat(3.0);
theLegend.entryPaddingTop = CPTFloat(3.0);
theLegend.entryPaddingRight = CPTFloat(3.0);
theLegend.entryPaddingBottom = CPTFloat(3.0);
theLegend.cornerRadius = 5.0;
theLegend.delegate = self;
pieChartGraph.legend = theLegend;
pieChartGraph.legendAnchor = CPTRectAnchorRight;
CGFloat legendPadding = +(self.view.bounds.size.width / 16);
pieChartGraph.legendDisplacement = CGPointMake(legendPadding, 0.0);
pieChartGraph.graph.position = CGPointMake(-30, 0);
推荐答案
设置 centerAnchor
将饼图放置在图形中。这是 CGPoint
。两个坐标的范围都在0和1之间,类似于 CALayer
的 anchorPoint
。
Set the centerAnchor
to position the pie chart within the graph. It is a CGPoint
. Both coordinates range between 0 and 1, similar to the anchorPoint
of a CALayer
.
这篇关于CorePlot:无法更改饼图的位置(位移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!