Не удается отобразить бары на гистограмме? - PullRequest
0 голосов
/ 21 июля 2011
- (void)constructBarChart
{
    // Create barChart from theme
    barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [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
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(300.0f)];


    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(5.0f)];

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    x.axisLineStyle = nil;
    x.majorTickLineStyle = nil;
    x.minorTickLineStyle = nil;
    x.majorIntervalLength = CPTDecimalFromString(@"5");
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    x.title = @"X Axis";
    x.titleLocation = CPTDecimalFromFloat(7.5f);
    x.titleOffset = 55.0f;

    // Define some custom labels for the data elements
    x.labelRotation = M_PI/4;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    //NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];

    NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2],[NSDecimalNumber numberWithInt:3],[NSDecimalNumber numberWithInt:4],[NSDecimalNumber numberWithInt:5],nil];

    NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];

     barCharData = [[NSMutableArray alloc]initWithObjects:[NSDecimalNumber numberWithInt:30], [NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:100],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/4;
        [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(@"50");
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    y.title = @"Y Axis";
    y.titleOffset = 45.0f;
    y.titleLocation = CPTDecimalFromFloat(150.0f);

     /*
    // First bar plot
    CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];

    barPlot.baseValue = CPTDecimalFromString(@"0");
    barPlot.dataSource = self;
    barPlot.barOffset = CPTDecimalFromFloat(-0.25f);
    barPlot.identifier = @"Bar Plot 1";
    [barChart addPlot:barPlot toPlotSpace:plotSpace];
    */

    // Second bar plot
    CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
    barPlot.dataSource = self;
    barPlot.baseValue = CPTDecimalFromString(@"0");
    barPlot.barOffset = CPTDecimalFromFloat(0.25f);
    barPlot.barCornerRadius = 2.0f;
    barPlot.identifier = @"Bar Plot 2";
    barPlot.delegate = self;
    [barChart addPlot:barPlot toPlotSpace:plotSpace];
}

    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
    {
        NSDecimalNumber *num = nil;

             printf("\n  ====================index =====%d",index);
            if(fieldEnum == CPTBarPlotFieldBarLocation )
            {
                num = [barCharData objectAtIndex:index];
            }
            else
            {
                num = [barCharData objectAtIndex:index];
            }
            NSLog(@"\n  ====================num =====%@",num);

        }


        return num;
    }

Я не могу отобразить бары на гистограмме.Я выношу консоль вот так

  ====================index =====0
2011-07-21 12:51:00.692 CPTTestApp-iPad[1004:207] 
  ====================num =====30

  ====================index =====1
2011-07-21 12:51:00.693 CPTTestApp-iPad[1004:207] 
  ====================num =====30

  ====================index =====2
2011-07-21 12:51:00.694 CPTTestApp-iPad[1004:207] 
  ====================num =====40

  ====================index =====3
2011-07-21 12:51:00.695 CPTTestApp-iPad[1004:207] 
  ====================num =====50

  ====================index =====4
2011-07-21 12:51:00.696 CPTTestApp-iPad[1004:207] 
  ====================num =====100

  ====================index =====0
2011-07-21 12:51:00.697 CPTTestApp-iPad[1004:207] 
  ====================num =====30

  ====================index =====1
2011-07-21 12:51:00.698 CPTTestApp-iPad[1004:207] 
  ====================num =====30

  ====================index =====2
2011-07-21 12:51:00.699 CPTTestApp-iPad[1004:207] 
  ====================num =====40

  ====================index =====3
2011-07-21 12:51:00.699 CPTTestApp-iPad[1004:207] 
  ====================num =====50

  ====================index =====4
2011-07-21 12:51:00.700 CPTTestApp-iPad[1004:207] 
  ====================num =====100

1 Ответ

2 голосов
/ 21 июля 2011

В методе numberForPlot:field:recordIndex, когда поле равно CPTBarPlotFieldBarLocation, вы должны просто вернуть index вместо [barCharData objectAtIndex:index].

...