0

我是 Mac 应用程序开发的新手。我正在使用核心图框工作绘制条形图。实际上我想要的是,当我选择一个月时,我想绘制过去 6 个月的图表。当我选择“12/2012”时,我能够正确绘制过去 6 个月的图表。但是当我选择“02/2013”​​时,图表只显示一月和二月,而不是前几个月。

- (int)maxVal:(NSMutableArray *)arr {
    NSDictionary *dict  = [arr objectAtIndex:0];
    NSString* dictValue = [dict objectForKey:@"X_VAL"];
    int mxm = [dictValue intValue];
    for (int i=0; i<[arr count]; i++) {
        if ([[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue] > mxm) {
            mxm = [[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue];
        }
    }
    return mxm;

}


- (int)minVal:(NSMutableArray *)arr {


    int mn = [[[arr objectAtIndex:0] objectForKey:@"X_VAL"] intValue];
    for (int i=0; i<[arr count]; i++) {
        if ([[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue] < mn) {
            mn = [[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue];
        }
    }
    return mn;
}

- (void)drawGraph {
    if ([graphMutableArray count] > 0) {
        NSRect viewRect = [aGraphView bounds];
        double xAxisStart = 0, yAxisStart = 0, xAxisLength = [graphMutableArray count], yAxisLength = [[graphMutableArray valueForKeyPath:@"@max.Y_VAL"] doubleValue], tenthPartOfYAxis = (yAxisLength + (yAxisLength / 10));
        NSLog(@"Y Axis Length = %f", yAxisLength);
        for (int k = 0; k < xAxisLength; k++) {
            NSDictionary *dict1 = [graphMutableArray objectAtIndex:k];
            NSDictionary *dict2 = [dict1 objectForKey:@"X_VAL"];
            NSDictionary *dict3 = [dict1 valueForKey:@"Y_VAL"];
            NSLog(@"dict 2 = %@ and dict 3 = %@", dict2, dict3);
        }

        graph = [[CPTXYGraph alloc] initWithFrame:viewRect];
        [graph applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]];//kCPTDarkGradientTheme]];//kCPTStocksTheme]];
        aGraphView.hostedGraph = graph;
        graph.plotAreaFrame.masksToBorder = NO;
        graph.paddingLeft = 90.0;
        graph.paddingTop = 20.0;
        graph.paddingRight = 20.0;
        graph.paddingBottom = 70.0;

        // Add plot space for horizontal bar charts
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart)
                                                        length:CPTDecimalFromDouble(xAxisLength + 1)];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisStart)
                                                        length:CPTDecimalFromDouble(tenthPartOfYAxis)]; 

        CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
        CPTXYAxis *x = axisSet.xAxis;
        NSString *cptDecimalstr = [NSString stringWithFormat:@"%f", xAxisLength /[x.axisLabels count]];
        x.majorIntervalLength = CPTDecimalFromString(cptDecimalstr);
        x.minorTicksPerInterval = 1;
        x.majorTickLineStyle = nil;
        x.minorTickLineStyle = nil;
        x.axisLineStyle = nil;
        x.titleLocation = CPTDecimalFromFloat(1.0f);
        x.titleOffset = 55.0f;

       // Define some custom labels for the data elements
        x.labelRotation = M_PI/4;
        x.labelingPolicy = CPTAxisLabelingPolicyNone;   


        NSLog(@"Max = %d and Min = %d", [self maxVal:graphMutableArray], [self minVal:graphMutableArray]);

        NSMutableArray *tempCustomTickLocations = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.7],[NSNumber numberWithFloat:3.0],[NSNumber numberWithFloat:4.1],[NSNumber numberWithFloat:5.3],[NSNumber numberWithFloat:6.5],[NSNumber numberWithFloat:6.9],[NSNumber numberWithFloat:7.1],[NSNumber numberWithFloat:7.5],[NSNumber numberWithFloat:8.1],[NSNumber numberWithFloat:8.5],[NSNumber numberWithFloat:8.9],nil];

        NSMutableArray *tempMonthsArr = [NSMutableArray arrayWithObjects:@"",@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];


        NSMutableArray *tempMonthsMutArr 
        = [[[NSMutableArray alloc] initWithCapacity:[graphMutableArray count]] autorelease], *tempMutCustomTickLocations 
        = [[[NSMutableArray alloc] initWithCapacity:[graphMutableArray count]] autorelease];

        int k = 0;

        for (int l = [self minVal:graphMutableArray]; l <=[self maxVal:graphMutableArray]; l++) {
            NSString *tempMonth = [tempMonthsArr objectAtIndex:l];
            [tempMonthsMutArr addObject:tempMonth];
            [tempMutCustomTickLocations addObject:[tempCustomTickLocations objectAtIndex:k]];
            k = k + 1;
            NSLog(@"tempMutCustomTickLocations= %@",tempMutCustomTickLocations);
        }

        if ([self minVal:graphMutableArray] == [self maxVal:graphMutableArray]) {
            NSString *tempMonth = [tempMonthsArr objectAtIndex:[self minVal:graphMutableArray]];
            [tempMonthsMutArr addObject:tempMonth];
            [tempMutCustomTickLocations addObject:[tempCustomTickLocations objectAtIndex:0]];
        }


        NSMutableArray *customTickLocations = [NSMutableArray arrayWithArray:tempMutCustomTickLocations];


        NSArray *xAxisLabels = [NSArray arrayWithArray:tempMonthsMutArr];
        NSUInteger labelLocation = 0;
        NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[graphMutableArray count]];

        for (NSNumber *tickLocation in customTickLocations) {
            //NSLog(@"tickLocation==%d",tickLocation.intValue);
            CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
            newLabel.tickLocation = [tickLocation decimalValue];//[tickLocation decimalValue];
            newLabel.offset = x.labelOffset + x.majorTickLength;
           // NSLog(@"[tickLocation floatValue] = %f", [tickLocation floatValue]);
            newLabel.offset = x.labelOffset + x.majorTickLength;
            //NSLog(@"x.labelOffset = %f, x.majorTickLength = %f and newLabel.offset = %f", x.labelOffset, x.majorTickLength, newLabel.offset);
            newLabel.rotation = M_PI/4;
            //NSLog(@"-=-=-=--=-=-=- %f",x.labelOffset);
            //NSLog(@"%f", x.majorTickLength );

            [customLabels addObject:newLabel];
            //NSLog(@"%@",customLabels);

            [newLabel release];
        }

        x.axisLabels =  [NSSet setWithArray:customLabels];
        NSLog(@"%@",x.axisLabels);


        CPTXYAxis *y = axisSet.yAxis;
        y.axisLineStyle = nil;
        y.majorTickLineStyle = nil;
        y.minorTickLineStyle = nil;
        NSString *cptDecimalStr = [NSString stringWithFormat:@"%f", yAxisLength / 10];
        y.majorIntervalLength = CPTDecimalFromString(cptDecimalStr);
        //y.constantCoordinateValue = CPTDecimalFromString(@"0");
        // y.title = @"Y Axis";
        y.titleOffset = 45.0f;
        y.titleLocation = CPTDecimalFromFloat(150.0f);
        //CPTPieChart 

        CPTBarPlot *plot = [[CPTBarPlot alloc] initWithFrame:CGRectZero];
        plot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0)
                                                      length:CPTDecimalFromDouble(xAxisLength)];
        plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"0.50"]
                          decimalValue]; //CPTDecimalFromDouble(0.50f);

        plot.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
        plot.dataSource = self;

        [graph addPlot:plot ];//toPlotSpace:plotSpace];
    }
    else {

        aGraphView.hostedGraph = nil;
        [[NSAlert alertWithMessageText:@"No Data Found" defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal];

    }
}

谁能帮我解决问题!!!

4

1 回答 1

0

您需要更新xRange绘图空间的。这会将轴标签移动到正确的位置并在新范围内绘制数据点。您还需要将新数据点添加到绘图中。

于 2013-01-11T02:49:07.933 回答