如何使用CorePlot创建看起来像饼图的饼图?

时间:2015-08-19 13:50:55

标签: ios graph core-plot pie-chart

左侧的图形是CorePlot图库演示中的图形,右侧的图形是我使用下面的代码创建的图形。我认为我错过了一些东西,以使它看起来像一个实际的圆形饼

enter image description here

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

我尝试从演示库中复制代码(来自CorePlot example project her的名为 CorePlot 的类)。

我做错了什么?为什么我的图表是正方形而不是圆圈?

1 个答案:

答案 0 :(得分:1)

尝试编辑图表的半径。这就是它成为一个圆圈的原因。要从方形视图中获取圆形,您通常会执行以下操作:

squareView.radius = view.frame.size.width/2;

你可以在你的pieChart上试试这个:

pieChart.pieRadius = pieChart.frame.size.width/2;

顺便说一句,你设置了2次pieChart dataSource