【问题标题】:How can I create a piechart that looks like a pie using CorePlot?如何使用 CorePlot 创建一个看起来像饼的饼图?
【发布时间】:2015-08-19 13:50:54
【问题描述】:

左图是 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];
}

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

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

【问题讨论】:

    标签: ios graph core-plot pie-chart


    【解决方案1】:

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

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

    你可以在你的饼图上试试这个:

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

    顺便说一下,你设置了 pieChart 数据源的 2 倍

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多