CorePlot的Scatter图未使用El Capitan 10.11 GM绘制

时间:2015-09-17 09:44:55

标签: objective-c macos core-plot

我已升级到10.11 El Capitan和Xcode 7,但看起来我的图表的散点图线未显示。显然,不会调用绘图数据源方法。它在10.10.X下运行良好,我没有对代码做任何改动

#import "TempOutdoorViewController.h"
#import <CorePlot/CorePlot.h>
#import <Quartz/Quartz.h>

@interface TempOutdoorViewController ()


@end

@implementation TempOutdoorViewController

@synthesize plotData, data, metric, window;


- (void)drawRect:(NSRect)dirtyRect {
    // set any NSColor for filling, say white:
    [[NSColor clearColor] setFill];
    NSRectFill(dirtyRect);
}


-(void)awakeFromNib
{
     [super awakeFromNib];
    window.alphaValue = 0.0f;
    [window setBackgroundColor:[NSColor clearColor]];
    [window setOpaque:NO];

    NSMutableArray *array = [[NSMutableArray alloc] init];


    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:now];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];
    NSDate *morningStart = [calendar dateFromComponents:components];

    NSTimeInterval todaysDiff = [morningStart timeIntervalSinceNow];


    NSDate *refDate       = morningStart;
    NSTimeInterval oneDay = -todaysDiff/[plotData count];


    // If you make sure your dates are calculated at noon, you shouldn't have to
    // worry about daylight savings. If you use midnight, you will have to adjust
    // for daylight savings time.
    NSMutableArray *newData = [NSMutableArray array];
    NSUInteger i;
    for ( i = 0; i < (unsigned long)[[data objectForKey:@"body"] count]; i++ ) {

        NSNumberFormatter *f = [[NSNumberFormatter alloc] init];

        NSLocale *US = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

        [f setLocale:US];
        [f setNumberStyle:NSNumberFormatterNoStyle];        NSString *string =  [NSString stringWithFormat:@"%@",[[[[[data objectForKey:@"body"] objectAtIndex:i] objectForKey:@"value"] objectAtIndex:0] objectAtIndex:0]];


        NSNumber *num;

        if ([metric isEqualToString:@"No"]) {
            num= [NSNumber numberWithDouble:([[f numberFromString:string] doubleValue] * 1.8 + 32)];

            ;
        }
        else{
            num=[f numberFromString:string];

        }


        if (num) {
            [array addObject:num];

        }
        else{

        }


        NSTimeInterval x = oneDay * i;
        id y             = num;
        //[NSDecimalNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
        [newData addObject:
         [NSDictionary dictionaryWithObjectsAndKeys:
          [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
          y, [NSNumber numberWithInt:CPTScatterPlotFieldY],
          nil]];
    }



    // Create graph from theme
    graph = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame : CGRectZero];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
    [graph applyTheme:theme];
    hostView.hostedGraph = graph;
    graph.plotAreaFrame.paddingBottom = 25.0;
    graph.plotAreaFrame.paddingTop = 10.0;
    graph.plotAreaFrame.paddingLeft = 35.0;
    graph.plotAreaFrame.paddingRight = 25.0;
    graph.plotAreaFrame.borderLineStyle = nil;    // don't draw a border

    graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    //graph.backgroundColor = [NSColor clearColor];
    // Setup scatter plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    NSTimeInterval xLow       = 0;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(86400)];


    double interval;
    double mini;
    double mini2;

    if ([metric isEqualToString:@"No"]) {
        interval = ([[plotData valueForKeyPath:@"@max.self"] doubleValue]* 1.8  - [[plotData valueForKeyPath:@"@min.self"] doubleValue] * 1.8  )  ;
        mini = ([[plotData valueForKeyPath:@"@min.self"] doubleValue] )  * 1.8 + 32 ;

        mini2 = ([[plotData valueForKeyPath:@"@min.self"] doubleValue] )  * 1.8 + 32;



    }

    else {
        interval = ([[plotData valueForKeyPath:@"@max.self"] doubleValue] - [[plotData valueForKeyPath:@"@min.self"] doubleValue]+3);
        mini = [[plotData valueForKeyPath:@"@min.self"] doubleValue] -  1;

        mini2 = [[plotData valueForKeyPath:@"@min.self"] doubleValue] -1;

    }

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(mini) length:CPTDecimalFromDouble(interval)];
    // Axes
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

    CPTXYAxis *x          = axisSet.xAxis;
    x.majorIntervalLength         = CPTDecimalFromFloat(86400/4);
    x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(mini2);
    x.minorTicksPerInterval       = 0;


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //  dateFormatter.dateStyle = kCFDateFormatterShortStyle;
    [dateFormatter setDateFormat:@"h:mm a"];

    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
    timeFormatter.referenceDate = refDate;
    x.labelFormatter            = timeFormatter;
   // x.labelRotation = M_PI/4;

    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];

    NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];

    if ([osxMode isEqualToString:@"Dark"]) {
        axisTitleStyle.color = [CPTColor whiteColor];

    }
    else {
    axisTitleStyle.color = [CPTColor blackColor];
    }
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 10.0f;

    x.labelTextStyle = axisTitleStyle;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:0];
    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorGridLineStyle.lineWidth = 0.75;

    if ([osxMode isEqualToString:@"Dark"]) {
        majorGridLineStyle.lineColor = [CPTColor colorWithComponentRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/ 255.0f alpha:1.0f];

    }
    else {
        majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
    }

    x.majorGridLineStyle = majorGridLineStyle;


    CPTXYAxis *y = axisSet.yAxis;

    if ([metric isEqualToString:@"No"]) {
        y.majorIntervalLength         = CPTDecimalFromString(@"5");
        numberFormatter.positiveSuffix =@"℉";

    }
    else{
        y.majorIntervalLength         = CPTDecimalFromString(@"2");
        numberFormatter.positiveSuffix =@"℃";

    }

    y.minorTicksPerInterval       = 0;
    y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0);
    y.labelFormatter = numberFormatter;
    y.labelTextStyle = axisTitleStyle;
    y.majorGridLineStyle = majorGridLineStyle;

    // Create a plot that uses the data source method
    CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
    dataSourceLinePlot.identifier = @"Date Plot";

    CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
    lineStyle.lineWidth              = 2.f;
    lineStyle.lineColor              = [CPTColor colorWithComponentRed:98.0f/255.0f green:147.0f/255.0f blue:202.0f/ 255.0f alpha:1.0f];
    dataSourceLinePlot.dataLineStyle = lineStyle;

    dataSourceLinePlot.dataSource = self;
    [graph addPlot:dataSourceLinePlot];








    NSLog(@"%@", newData);
    plotData = newData;

}

#pragma mark -
#pragma mark Plot Data Source Methods

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return plotData.count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:(int)fieldEnum]];

    return num;
}

0 个答案:

没有答案