如何在iPad中显示条形图?

时间:2012-09-10 10:50:10

标签: iphone ipad fullscreen core-plot bar-chart

目前我正在开发一个iPad应用程序,我正在使用Core-Plot在我的iPhone应用程序中开发条形图,然后我修改了iPad的框架大小以在iPad应用程序中导入相同的东西,但条形图显示在屏幕上小,但我想在iPad上全屏显示,如何解决这个问题?请帮帮我

先谢谢

我试过这个

iPhone

-(void) drawBarChart
{

        NSLog(@"iPhone Bar chart");

        double xAxisLength = [barValuesArray count];

        barGraph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(0, 50, 320, 350)];

        barGraph.paddingLeft = 0.0f;
        barGraph.paddingRight = 0.0f;
        barGraph.paddingTop = 0.0f;
        barGraph.paddingBottom = 0.0f;

        barGraph.plotAreaFrame.paddingLeft = 60.0;
        barGraph.plotAreaFrame.paddingTop = 40.0;
        barGraph.plotAreaFrame.paddingRight = 10.0;
        barGraph.plotAreaFrame.paddingBottom = 40.0;

        if ([selectedName isEqualToString:@"(null)"] || [selectedName isEqualToString:@""] || [selectedName isEqualToString:@"NULL"] || selectedName == @"(null)" || selectedName == NULL || selectedName == @"null" || selectedName == nil) 
        {
            barGraph.title = [namesArray objectAtIndex:0];
        }
        else
        {
            barGraph.title = selectedName;
        }

        hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 10, 320, 400)];
        hostingView.hostedGraph = barGraph;

        CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
        textStyle.color = [CPTColor blackColor];
        textStyle.fontSize = 16.0f;
        textStyle.textAlignment = CPTTextAlignmentCenter;
        barGraph.titleTextStyle = textStyle;
        barGraph.titleDisplacement = CGPointMake(0.0f, -10.0f);
        barGraph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 


        CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barGraph.axisSet;
        CPTXYAxis *x = axisSet.xAxis;
        //    x.axisLineStyle = nil;
        //    x.majorTickLineStyle = nil;
        //    x.minorTickLineStyle = nil;
        x.majorIntervalLength = CPTDecimalFromString(@"70");
        x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
        x.title = @"Names";
        x.titleLocation = CPTDecimalFromFloat(7.5f);
        x.titleOffset = 50.0f; //25.0f;
        x.labelRotation = M_PI/5;
        x.labelingPolicy = CPTAxisLabelingPolicyNone;


        NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt: 5], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:13], nil];


        //    NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Smoke", @"Nicotine", @"Tar", @"", nil];
        //    NSUInteger labelLocation = 0;
        //    
        //    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
        //    for (NSNumber *tickLocation in customTickLocations) 
        //    {
        //        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        //        newLabel.tickLocation = [tickLocation decimalValue];
        //        newLabel.offset = x.labelOffset + x.majorTickLength;
        //        newLabel.rotation = M_PI/xAxisLength;
        //        [customLabels addObject:newLabel];
        //        [newLabel release];
        //    }
        //    
        //    x.axisLabels =  [NSSet setWithArray:customLabels];

        CPTXYAxis *y = axisSet.yAxis;
        //    y.axisLineStyle = nil;
        //    y.majorTickLineStyle = nil;
        //    y.minorTickLineStyle = nil;
        y.majorIntervalLength = CPTDecimalFromString(@"100");
        y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
        //    y.title = @"Work Status";
        //    y.titleOffset = 40.0f;
        //    y.titleLocation = CPTDecimalFromFloat(150.0f);


        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) barGraph.defaultPlotSpace;
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(15.0f)];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(1000.0f)];

        CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
        barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(13)];//xAxisLength
        barPlot.barOffset = CPTDecimalFromFloat(1.0f);
        barPlot.baseValue = CPTDecimalFromString(@"0");
        barPlot.barWidth = CPTDecimalFromFloat(3.0f);
        barPlot.cornerRadius = 2.0f;
        barPlot.dataSource = self;
        [barGraph addPlot:barPlot];

        [self.view addSubview:hostingView];

}

iPad条形图:

-(void) drawBarChart
{

        NSLog(@"iPAD");

        double xAxisLength = [barValuesArray count];


        barGraph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(0, 50, 700, 1000)];

        barGraph.paddingLeft = 0.0f;
        barGraph.paddingRight = 0.0f;
        barGraph.paddingTop = 0.0f;
        barGraph.paddingBottom = 0.0f;

        barGraph.plotAreaFrame.paddingLeft = 60.0;
        barGraph.plotAreaFrame.paddingTop = 40.0;
        barGraph.plotAreaFrame.paddingRight = 10.0;
        barGraph.plotAreaFrame.paddingBottom = 40.0;

        if ([selectedName isEqualToString:@"(null)"] || [selectedName isEqualToString:@""] || [selectedName isEqualToString:@"NULL"] || selectedName == @"(null)" || selectedName == NULL || selectedName == @"null" || selectedName == nil) 
        {
            barGraph.title = [namesArray objectAtIndex:0];
        }
        else
        {
            barGraph.title = selectedName;
        }

        hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 10, 700, 1000)];
        hostingView.hostedGraph = barGraph;

        CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
        textStyle.color = [CPTColor blackColor];
        textStyle.fontSize = 16.0f;
        textStyle.textAlignment = CPTTextAlignmentCenter;
        barGraph.titleTextStyle = textStyle;
        barGraph.titleDisplacement = CGPointMake(0.0f, -10.0f);
        barGraph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 


        CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barGraph.axisSet;
        CPTXYAxis *x = axisSet.xAxis;
        //    x.axisLineStyle = nil;
        //    x.majorTickLineStyle = nil;
        //    x.minorTickLineStyle = nil;
        x.majorIntervalLength = CPTDecimalFromString(@"70");
        x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
        x.title = @"Names";
        x.titleLocation = CPTDecimalFromFloat(7.5f);
        x.titleOffset = 50.0f; //25.0f;
        x.labelRotation = M_PI/5;
        x.labelingPolicy = CPTAxisLabelingPolicyNone;


        NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt: 5], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:13], nil];


        //    NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Smoke", @"Nicotine", @"Tar", @"", nil];
        //    NSUInteger labelLocation = 0;
        //    
        //    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
        //    for (NSNumber *tickLocation in customTickLocations) 
        //    {
        //        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        //        newLabel.tickLocation = [tickLocation decimalValue];
        //        newLabel.offset = x.labelOffset + x.majorTickLength;
        //        newLabel.rotation = M_PI/xAxisLength;
        //        [customLabels addObject:newLabel];
        //        [newLabel release];
        //    }
        //    
        //    x.axisLabels =  [NSSet setWithArray:customLabels];

        CPTXYAxis *y = axisSet.yAxis;
        //    y.axisLineStyle = nil;
        //    y.majorTickLineStyle = nil;
        //    y.minorTickLineStyle = nil;
        y.majorIntervalLength = CPTDecimalFromString(@"100");
        y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
        //    y.title = @"Work Status";
        //    y.titleOffset = 40.0f;
        //    y.titleLocation = CPTDecimalFromFloat(150.0f);


        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) barGraph.defaultPlotSpace;
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(15.0f)];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(1000.0f)];

        CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
        barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(13)];//xAxisLength
        barPlot.barOffset = CPTDecimalFromFloat(1.0f);
        barPlot.baseValue = CPTDecimalFromString(@"0");
        barPlot.barWidth = CPTDecimalFromFloat(3.0f);
        barPlot.cornerRadius = 2.0f;
        barPlot.dataSource = self;
        [barGraph addPlot:barPlot];

        [self.view addSubview:hostingView];
}

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这种方式可能对你有帮助:

CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(10, 70, 768, 1004)];
    hostingView.tag=1;
    [self.view addSubview:hostingView];
//  [hostingView addSubview:OptionBtn];
    barChart = [[CPXYGraph alloc] initWithFrame:self.view.bounds];
    //barChart.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"FinalGraph.png"]];
    hostingView.hostedGraph = barChart;
相关问题