Coreplot条形图问题在正确的位置显示代码?

时间:2011-08-30 08:47:24

标签: iphone core-plot

我正在开发iphone条形图应用程序。我没有按照下图所示获得股票代码标签值:

enter image description here

barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTStocksTheme];
[barChart applyTheme:theme];
CPTGraphHostingView *hostingView =(CPTGraphHostingView *)self.view;
//[self.scrollview addSubview:hostingView];
hostingView.hostedGraph = barChart;

// Border
barChart.plotAreaFrame.borderLineStyle = nil;
barChart.plotAreaFrame.cornerRadius = 10.0f;

// Paddings
barChart.paddingLeft = 0.0f;
barChart.paddingRight = 0.0f;
barChart.paddingTop = 0.0f;
barChart.paddingBottom = 0.0f;

barChart.plotAreaFrame.paddingLeft = 70.0;
barChart.plotAreaFrame.paddingTop = 20.0;
barChart.plotAreaFrame.paddingRight = 20.0;
barChart.plotAreaFrame.paddingBottom = 80.0;

// Graph title
barChart.title = @"Statistics";
CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
textStyle.color = [CPTColor greenColor];
textStyle.fontSize = 20.0f;
textStyle.textAlignment = CPTTextAlignmentCenter;
barChart.titleTextStyle = textStyle;
barChart.titleDisplacement = CGPointMake(0.0f, -20.0f);
barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop;

// Add plot space for horizontal bar charts
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(15.0f)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(20.0f)];
plotSpace.globalXRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(20.0f)];
    plotSpace.globalYRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(20.0f)];
plotSpace.allowsUserInteraction=TRUE;



CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
[x setMinorTickLineStyle:CPTLinearBlendingMode];
x.majorIntervalLength = CPTDecimalFromString(@"10");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.title = @"Quiz Date";
x.majorTickLength=10.0f;
x.titleLocation = CPTDecimalFromFloat(7.5f);
x.titleOffset = 60.0f;
x.labelOffset=5.0f;

// Define some custom labels for the data elements
x.labelRotation = M_PI/2;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
//NSDecimalNumber add=0;


CPTPlotRange *globalxrange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(1000.0f)];
plotSpace.globalXRange=globalxrange; 

NSMutableArray *customTickLocations = [[NSMutableArray alloc]init];
NSDecimalNumber *num = nil;
for (int i=0;i<[arr count];i++) {
    num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:i*5];
    [customTickLocations addObject:num];
}

NSArray *xAxisLabels =[[NSMutableArray alloc]initWithArray:labelValue]; //[NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", 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/2;
    NSLog(@"-=-=-=--=-=-=- %d",x.labelOffset+ x.majorTickLength );
    [customLabels addObject:newLabel];
    [newLabel release];
}
x.axisLabels =  [NSSet setWithArray:customLabels];





CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy=CPTAxisLabelingPolicyAutomatic;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPTDecimalFromString(@"1");
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
y.title = @"No. of Questions";
y.titleOffset = 50.0f;
y.titleLocation = CPTDecimalFromFloat(150.0f);

// First bar plot
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.baseValue = CPTDecimalFromString(@"0");
barPlot.dataSource = self;
barPlot.barWidth=CPTDecimalFromFloat(1.0f);
barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0)                                       length:CPTDecimalFromDouble(x.labelOffset + x.majorTickLength)];
barPlot.barOffset =CPTDecimalFromFloat(1.0f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 1";

[barChart addPlot:barPlot toPlotSpace:plotSpace];
    `// barPlot.barOffset = CPTDecimalFromFloat(0.25f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 2";
[barChart addPlot:barPlot toPlotSpace:plotSpace];`

// third bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barWidth=CPTDecimalFromFloat(1.0f);
barPlot.baseValue = CPTDecimalFromString(@"0"); 
barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0)length:CPTDecimalFromDouble(x.labelOffset + x.majorTickLength)];
barPlot.barOffset =CPTDecimalFromFloat(3.0f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 3";
[barChart addPlot:barPlot toPlotSpace:plotSpace];


// arrayWithObjects:[NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [arr count];`}`

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index `{`
NSDecimalNumber *num = nil;
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
    switch ( fieldEnum ) {
        case CPTBarPlotFieldBarLocation:
            num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            break;

        case CPTBarPlotFieldBarTip:
            if ( [plot.identifier isEqual:@"Bar Plot 1"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[totalValues objectAtIndex:index]];

            if ( [plot.identifier isEqual:@"Bar Plot 2"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[correctValues objectAtIndex:index]];

            if ( [plot.identifier isEqual:@"Bar Plot 3"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[wrongArray objectAtIndex:index]];

            break;


        case CPTScatterPlotFieldX:  
            if ( [plot.identifier isEqual:@"Bar Plot 1"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[totalValues objectAtIndex:index]];
            return num;

            if ( [plot.identifier isEqual:@"Bar Plot 2"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[correctValues objectAtIndex:index]];
                return num;

            if ( [plot.identifier isEqual:@"Bar Plot 3"] ) 
                num=[NSDecimalNumber decimalNumberWithString:[wrongArray objectAtIndex:index]];
            return num;

            break;


    }
}

return num;`}`

-(CPTFill *) barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSNumber *)index; 
{
return nil;
}

1 个答案:

答案 0 :(得分:3)

为要与标签排列的图表将barOffset设置为零。设置相对于那个的其他人。例如:

barPlot1.barOffset = CPTDecimalFromDouble(-1.0);
barPlot2.barOffset = CPTDecimalFromDouble( 0.0);
barPlot3.barOffset = CPTDecimalFromDouble( 1.0);

您还需要调整绘图空间的xRange以保持前几个条形可见。