0

我想创建一个带有 IOS 绘图的饼图,我已经下载了框架,但它从 plist 文件中获取数据,它只是静态的解决这个问题,我不明白这个情节部分的填充值是如何产生的。这是在单独的视图中我想要在我现有的视图控制器中

   #import "PieChartViewController.h"
   #import "PCPieChart.h"

   @implementation PieChartViewController

  - (id)init
  {
self = [super init];
if (self)
{
    [self.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
    [self.titleLabel setText:@"Pie Chart"];


    int height = [self.view bounds].size.width/3*2.; // 220;
    int width = [self.view bounds].size.width; //320;
    PCPieChart *pieChart = [[PCPieChart alloc] initWithFrame:CGRectMake(([self.view bounds].size.width-width)/2,([self.view bounds].size.height-height)/2,width,height)];
    [pieChart setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
    [pieChart setDiameter:width/2];
    [pieChart setSameColorLabel:YES];

    [self.view addSubview:pieChart];
    [pieChart release];

    if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad)
    {
        pieChart.titleFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
        pieChart.percentageFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:50];
    }

//  NSString *sampleFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample_piechart_data.plist"];
//  NSDictionary *sampleInfo = [NSDictionary dictionaryWithContentsOfFile:sampleFile];
    NSMutableArray *components = [NSMutableArray array];


    for (int i=0; i<[[components] count]; i++)
    {
        NSDictionary *item = [[sampleInfo objectForKey:@"data"] objectAtIndex:i];
        PCPieComponent *component = [PCPieComponent pieComponentWithTitle:[item objectForKey:@"title"] value:[[item objectForKey:@"value"] floatValue]];
        [components addObject:component];

        if (i==0)
        {
            [component setColour:PCColorYellow];
        }
        else if (i==1)
        {
            [component setColour:PCColorGreen];
        }
        else if (i==2)
        {
            [component setColour:PCColorOrange];
        }
        else if (i==3)
        {
            [component setColour:PCColorRed];
        }
        else if (i==4)
        {
            [component setColour:PCColorBlue];
        }
    }
    [pieChart setComponents:components];

    }
    return self;
        }
4

1 回答 1

0

我使用过相同的框架并面临相同的问题,我通过使用这种方法解决了这个问题,试试吧,可能会奏效。

NSMutableArray *components = [NSMutableArray array];

 NSDictionary *value1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Bala", @"title", [NSNumber numberWithInt:13702.79], @"value", nil];
    NSDictionary *value2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Justin", @"title", [NSNumber numberWithInt:130000], @"value", nil];


    NSArray *dictionaryArray = [NSArray arrayWithObjects:value1, value2,  nil];

    NSDictionary *dataDictonary = [NSDictionary dictionaryWithObjectsAndKeys:dictionaryArray, @"data", nil];

    NSLog(@"%@", dataDictonary);
    NSMutableArray *components = [[NSMutableArray alloc]init];

    for (int i=0; i<[[dataDictonary objectForKey:@"data"] count]; i++)
    {

        NSDictionary *item = [[dataDictonary objectForKey:@"data"] objectAtIndex:i];
        NSLog(@"%@ item with inde",item);

        PCPieComponent *component = [PCPieComponent pieComponentWithTitle:[item objectForKey:@"title"] value:[[item objectForKey:@"value"] floatValue]];

        [components addObject:component];

        if (i==0)
        {
            [component setColour:PCColorYellow];
        }
        else if (i==1)
        {
            [component setColour:PCColorGreen];
        }
        else if (i==2)
        {
            [component setColour:PCColorOrange];
        }
        else if (i==3)
        {
            [component setColour:PCColorRed];
        }
        else if (i==4)
        {
            [component setColour:PCColorBlue];
        }
    }
    [pieChart setComponents:components];

}

这是符合您要求的示例项目

于 2012-05-25T05:31:37.037 回答