Coreplot 条形图问题在正确的位置显示股票?

发布于 2024-12-01 23:12:07 字数 7179 浏览 0 评论 0原文

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

在此处输入图像描述

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

I am working on a iphone barchart application. I am not getting the ticker label value in accordance as shown in below image:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2024-12-08 23:12:07

对于要与标签对齐的绘图,将 barOffset 设置为零。将其他项设置为相对于该项。例如:

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

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

Set the barOffset to zero for the plot that you want lined up with the labels. Set the others relative to that one. For example:

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

You will also need to adjust the xRange of the plot space to keep the first couple of bars visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文