如何使用 CorrPlot 创建看起来像饼图的饼图?
How can I create a piechart that looks like a pie using CorePlot?
左边的图是CorePlot 图库演示中的图,右边的图是我用下面的代码创建的。我想 为了让它看起来像一个真正的圆形馅饼,我遗漏了一些东西:
-(void)configureChart {
// 1 - Get reference to graph
pieChartGraph = [[CPTXYGraph alloc] initWithFrame:self.pieChartgraphHostView.bounds];
self.pieChartgraphHostView.hostedGraph = pieChartGraph;
// 2 - Create chart
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.delegate = self;
// pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
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
[pieChartGraph addPlot:pieChart];
pieChart.dataSource = self;
self.dataForChart = [@[@20.0, @30.0, @60.0] mutableCopy];
//self.dataForChart = [@[@20.0, @30.0, @60.0]mutableCopy];
}
我试图从演示库中复制代码(class 从 CorePlot example project here 调用 CorePlot)。
我做错了什么?为什么我的图表是正方形而不是圆形?
尝试编辑图表的半径。这就是使它成为一个圆圈的原因。要从方形视图中获取圆形,您通常会执行以下操作:
squareView.radius = view.frame.size.width/2;
你可以在饼图上试试这个:
pieChart.pieRadius = pieChart.frame.size.width/2;
顺便说一下,您将 pieChart 数据源设置为 2 倍
左边的图是CorePlot 图库演示中的图,右边的图是我用下面的代码创建的。我想 为了让它看起来像一个真正的圆形馅饼,我遗漏了一些东西:
-(void)configureChart {
// 1 - Get reference to graph
pieChartGraph = [[CPTXYGraph alloc] initWithFrame:self.pieChartgraphHostView.bounds];
self.pieChartgraphHostView.hostedGraph = pieChartGraph;
// 2 - Create chart
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.delegate = self;
// pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
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
[pieChartGraph addPlot:pieChart];
pieChart.dataSource = self;
self.dataForChart = [@[@20.0, @30.0, @60.0] mutableCopy];
//self.dataForChart = [@[@20.0, @30.0, @60.0]mutableCopy];
}
我试图从演示库中复制代码(class 从 CorePlot example project here 调用 CorePlot)。
我做错了什么?为什么我的图表是正方形而不是圆形?
尝试编辑图表的半径。这就是使它成为一个圆圈的原因。要从方形视图中获取圆形,您通常会执行以下操作:
squareView.radius = view.frame.size.width/2;
你可以在饼图上试试这个:
pieChart.pieRadius = pieChart.frame.size.width/2;
顺便说一下,您将 pieChart 数据源设置为 2 倍