CPBarPlot 中不显示条形图

发布于 2024-11-25 18:14:32 字数 4165 浏览 3 评论 0原文

我是 CPBarPlot 的新手,完全不知道如何将我自己的条形添加到条形图中。我有一个整数数组,我想显示条形图,但无济于事。我已经尝试过以下方法。请有人帮忙... plzzzzz。

- (void)constructBarChart
{
    // Create barChart from theme
    barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
    CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
    [barChart applyTheme:theme];
    barChartView.hostedGraph = barChart;
    barChart.plotAreaFrame.masksToBorder = NO;

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

// Add plot space for horizontal bar charts
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(300.0f)];
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(16.0f)];

CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPDecimalFromString(@"5");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.title = @"Age Group";
x.titleLocation = CPDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;

// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"< 40", @"40 - 50", @"50-60", @"> 60", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

_barChartData = [[NSMutableArray alloc]initWithObjects:[NSDecimalNumber numberWithInt:30], [NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:100],nil];

CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPDecimalFromString(@"50");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Number of Individuals";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(150.0f);


// First bar plot
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
//barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
//barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = CPDecimalFromFloat(0.25f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 2";
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
}

'

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:    (NSUInteger)index 
{
NSDecimalNumber *num = nil;
if ( [plot isKindOfClass:[CPPieChart class]] ) {
    if ( index >= [self.dataForChart count] ) return nil;

    if ( fieldEnum == CPPieChartFieldSliceWidth ) {
        num = [self.dataForChart objectAtIndex:index];
    }
    else {
        num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
    }
}
else if( [plot isKindOfClass:[CPBarPlot class]] ) {
    switch ( fieldEnum ) {
        case CPBarPlotFieldBarLocation:
            //num = [NSNumber numberWithUnsignedInteger:index];
            num = (NSNumber *)[NSNumber numberWithUnsignedInteger:[[_barChartData objectAtIndex:index] unsignedIntegerValue]];
            break;
    }   
}

return num;
}

I am new to CPBarPlot and totally clueless how add my own bars into the bar chart. I have an array of integers i want to display for the bars but to no avail. I have tried the following below. Some one plz help... plzzzzz.

- (void)constructBarChart
{
    // Create barChart from theme
    barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
    CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
    [barChart applyTheme:theme];
    barChartView.hostedGraph = barChart;
    barChart.plotAreaFrame.masksToBorder = NO;

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

// Add plot space for horizontal bar charts
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(300.0f)];
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(16.0f)];

CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPDecimalFromString(@"5");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.title = @"Age Group";
x.titleLocation = CPDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;

// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"< 40", @"40 - 50", @"50-60", @"> 60", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

_barChartData = [[NSMutableArray alloc]initWithObjects:[NSDecimalNumber numberWithInt:30], [NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:100],nil];

CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPDecimalFromString(@"50");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Number of Individuals";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(150.0f);


// First bar plot
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
//barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
//barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = CPDecimalFromFloat(0.25f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 2";
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
}

'

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:    (NSUInteger)index 
{
NSDecimalNumber *num = nil;
if ( [plot isKindOfClass:[CPPieChart class]] ) {
    if ( index >= [self.dataForChart count] ) return nil;

    if ( fieldEnum == CPPieChartFieldSliceWidth ) {
        num = [self.dataForChart objectAtIndex:index];
    }
    else {
        num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
    }
}
else if( [plot isKindOfClass:[CPBarPlot class]] ) {
    switch ( fieldEnum ) {
        case CPBarPlotFieldBarLocation:
            //num = [NSNumber numberWithUnsignedInteger:index];
            num = (NSNumber *)[NSNumber numberWithUnsignedInteger:[[_barChartData objectAtIndex:index] unsignedIntegerValue]];
            break;
    }   
}

return num;
}

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

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

发布评论

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

评论(1

小忆控 2024-12-02 18:14:32

您需要设置绘图数据源:

barPlot.dataSource = self;

-numberForPlot:field:recordIndex: 的条形图部分更改为:

switch ( fieldEnum ) {
    case CPTBarPlotFieldBarLocation:
        num = [NSNumber numberWithUnsignedInteger:index];
        break;
    case CPTBarPlotFieldBarTip:
        num = [_barChartData objectAtIndex:index];
        break;
}

switch 语句中的常量取决于您使用的 Core Plot 版本。我使用了最新的字段常量。早期版本使用前缀“CP”而不是“CPT”。非常旧的版本使用 CPBarPlotFieldBarLength 而不是 CPTBarPlotFieldBarTip

You need to set the plot datasource:

barPlot.dataSource = self;

Change the bar plot section of -numberForPlot:field:recordIndex: to:

switch ( fieldEnum ) {
    case CPTBarPlotFieldBarLocation:
        num = [NSNumber numberWithUnsignedInteger:index];
        break;
    case CPTBarPlotFieldBarTip:
        num = [_barChartData objectAtIndex:index];
        break;
}

The constants in the switch statement depend on which version of Core Plot you're using. I used the latest field constants. Earlier versions used a prefix of "CP" instead of "CPT". Very old versions used CPBarPlotFieldBarLength instead of CPTBarPlotFieldBarTip.

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