0

我在横向模式下实现了一个带有散点图的图表。我已经在绘图空间上启用了用户交互。图表视图似乎只能识别图表左半部分的用户交互,而不能识别右半部分的用户交互。我确保没有与图形视图重叠的视图,并且它位于视图堆栈的顶部。谁能建议可能出了什么问题?.. 谢谢

代码在这里:

图主机的初始化:

-(void)initgraphhost{
    CGRect graphcontainer=CGRectMake(0,0,568,300);
    self.graphhostview = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:graphcontainer];
    self.graphhostview.backgroundColor=[UIColor clearColor];
    self.graphhostview.allowPinchScaling = YES;
    [self.view addSubview:self.graphhostview];
}

添加图表:

-(void)AddGraph:(NSString *)graphname tographcontainer:(CPTGraphHostingView *)host{
    graph = [[CPTXYGraph alloc] initWithFrame:host.bounds];
    graph.borderColor=(__bridge CGColorRef)([UIColor clearColor]);
    [graph applyTheme:[CPTTheme themeNamed:nil]];
    host.hostedGraph = graph;
    //  plotspace= (CPTXYPlotSpace *)graph.defaultPlotSpace;
    CPTXYGraph *currentgraph=(CPTXYGraph *)host.hostedGraph;
    CPTXYPlotSpace *currentplotspace=(CPTXYPlotSpace *) host.hostedGraph.defaultPlotSpace;
    currentgraph.plotAreaFrame.paddingBottom=25;
    currentgraph.plotAreaFrame.paddingLeft=45;
    currentgraph.plotAreaFrame.paddingTop=10;
    [currentgraph setBackgroundColor:[gBkGcolor CGColor]];
    currentplotspace = (CPTXYPlotSpace *)currentgraph.defaultPlotSpace;
    currentplotspace.delegate=self;
    currentplotspace.allowsUserInteraction=YES;
    //Configure layer hierarchy
    NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels],[NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines],
                        [NSNumber numberWithInt:CPTGraphLayerTypePlots],
                        [NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisLines],
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles],

                        nil];
    currentgraph.topDownLayerOrder = chartLayers;

    //Styles and formats

    CPTMutableLineStyle  *clearlineStyle = [CPTLineStyle lineStyle];
    clearlineStyle.lineColor = [CPTColor clearColor];
    clearlineStyle.lineWidth = 2.0f;

    CPTMutableLineStyle  *gridLineStyle = [CPTLineStyle lineStyle];
    gridLineStyle.lineColor = [CPTColor darkGrayColor];
    gridLineStyle.lineWidth = 0.5f;

    NSArray *dashpatternArray=[NSArray arrayWithObjects:[NSNumber numberWithDouble:2],[NSNumber numberWithDouble:2],[NSNumber numberWithDouble:2], nil];
    gridLineStyle.dashPattern=dashpatternArray;

    CPTMutableLineStyle  *AxisLineStyle = [CPTLineStyle lineStyle];
    AxisLineStyle.lineColor = [CPTColor clearColor];
    AxisLineStyle.lineWidth = 2.0f;

    CPTMutableLineStyle  *TickLineStyle = [CPTLineStyle lineStyle];
    TickLineStyle.lineColor = [CPTColor clearColor];
    TickLineStyle.lineWidth = 1.0f;

    CPTMutableTextStyle *textstyle=[CPTTextStyle textStyle];
    textstyle.fontSize=12;
    textstyle.color=[CPTColor blackColor];

    CPTMutableTextStyle *ylabelstyle=[CPTMutableTextStyle textStyle];
    ylabelstyle.fontSize=12;
    ylabelstyle.color = [CPTColor blackColor];

    NSNumberFormatter *Yformatter = [[NSNumberFormatter alloc] init];
    [Yformatter setGeneratesDecimalNumbers:NO];
    [Yformatter setNumberStyle:NSNumberFormatterNoStyle];

    NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0];
    NSTimeInterval oneDay = 24 * 60 * 60;
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:CurrentDF];
    timeFormatter.referenceDate = refDate;

    //Format axes

    //X axis
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)currentgraph.axisSet;
    axisSet.xAxis.majorIntervalLength =CPTDecimalFromFloat(CurrentXinterval);
    axisSet.xAxis.minorTicksPerInterval = 0;
    axisSet.xAxis.majorTickLineStyle = TickLineStyle;
    axisSet.xAxis.minorTickLineStyle = clearlineStyle;
    axisSet.xAxis.axisLineStyle = AxisLineStyle;
    axisSet.xAxis.majorTickLength = 7.0f;
    axisSet.xAxis.labelOffset=3.0f;
    axisSet.xAxis.labelTextStyle=textstyle;
    axisSet.xAxis.majorGridLineStyle=clearlineStyle;
    axisSet.xAxis.labelingPolicy=CPTAxisLabelingPolicyAutomatic ;
    axisSet.xAxis.labelFormatter = timeFormatter;
    axisSet.xAxis.labelExclusionRanges=[[NSArray alloc]initWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(0)], nil];


    //Y Axis

    axisSet.yAxis.axisConstraints=[CPTConstraints constraintWithLowerOffset:0.0];
    axisSet.yAxis.labelFormatter = Yformatter;
    axisSet.yAxis.minorTicksPerInterval = 0;
    axisSet.yAxis.majorTickLineStyle = TickLineStyle;
    axisSet.yAxis.axisLineStyle = AxisLineStyle;
    axisSet.yAxis.minorTickLength = 5.0f;
    axisSet.yAxis.majorTickLength = 7.0f;
    axisSet.yAxis.majorGridLineStyle=gridLineStyle;
    axisSet.yAxis.labelTextStyle=ylabelstyle;

    //Labels
    CPTColor *targetlinecolor=[CPTColor colorWithComponentRed:(float)186/255 green:(float)175/255 blue:(float)158/255 alpha:1];
    CPTMutableTextStyle *TargetLabelTextStyle = [CPTMutableTextStyle textStyle];
    TargetLabelTextStyle.color    = targetlinecolor;
    TargetLabelTextStyle.fontSize = 10.0f;
    TargetLabelTextStyle.fontName = @"OpenSans-Bold";
    TargetLabelTextStyle.textAlignment=CPTTextAlignmentRight;


    //Set graph specific properties

    if ([graphname isEqualToString:@"metabolism"]) {
        self.graphtitleLb.text=@"Metabolism (kCal/day)";
        MetabolismTest *latest=[metadata lastObject];
        NSTimeInterval mostrelevantdate = [[latest timestamp] timeIntervalSince1970];
        NSTimeInterval twoDaysAgo=mostrelevantdate-(2*oneDay);
        currentplotspace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(twoDaysAgo) length:CPTDecimalFromFloat((mostrelevantdate - twoDaysAgo)*2)];

        axisSet.xAxis.orthogonalCoordinateDecimal=CPTDecimalFromFloat(metaYlow);
        axisSet.yAxis.orthogonalCoordinateDecimal=CPTDecimalFromString(@"0");

        currentplotspace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(metaYlow)                                                            length:CPTDecimalFromFloat(metaYHigh-metaYlow)];

        axisSet.yAxis.majorIntervalLength = [[NSNumber numberWithFloat:metaYinterval] decimalValue];


        axisSet.yAxis.visibleRange   = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(metaYlow) length:CPTDecimalFromInteger(metaYHigh-metaYlow)];
        self.Metabolismplot = [[CPTScatterPlot alloc]
                           init ];
        self.Metabolismplot.identifier = @"metabolism";
        self.Metabolismplot.delegate=self;
        self.Metabolismplot.title=@"Metabolism";
        self.Metabolismplot.plotSymbolMarginForHitDetection=5;

        //Format plot and plot symbols

        CPTMutableLineStyle *plotLineStyle = [self.Metabolismplot.dataLineStyle mutableCopy];
        plotLineStyle.lineWidth = 3.0f;
        plotLineStyle.lineColor = [CPTColor colorWithComponentRed:(float)125/255 green:(float)177/255 blue:(float)52/255 alpha:1];
        self.Metabolismplot.dataLineStyle = plotLineStyle;
        self.Metabolismplot.dataSource = self;
        self.Metabolismplot.delegate=self;

        CPTMutableLineStyle *plotsymbolLinestyle=[CPTMutableLineStyle lineStyle];
        plotsymbolLinestyle.lineColor=[CPTColor colorWithComponentRed:0 green:0.6 blue:0 alpha:1];
        CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        greenCirclePlotSymbol.lineStyle=plotLineStyle;
        greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:(float)125/255 green:(float)177/255 blue:(float)52/255 alpha:1]];
        greenCirclePlotSymbol.size = CGSizeMake(7.0, 7.0);
        self.Metabolismplot.plotSymbol=greenCirclePlotSymbol;

        //Format plot and plot symbols for avg plots

        CPTMutableLineStyle *avgLineStyle = [self.metabolismHighplot.dataLineStyle mutableCopy];
        avgLineStyle.lineWidth = 1.5f;
        avgLineStyle.lineColor = [CPTColor clearColor];
        CPTMutableLineStyle *clearsymbolLinestyle=[CPTMutableLineStyle lineStyle];
        clearsymbolLinestyle.lineColor=[CPTColor clearColor];
        CPTPlotSymbol *clearPlotSymbol = [CPTPlotSymbol crossPlotSymbol];
        clearPlotSymbol.lineStyle=clearsymbolLinestyle;
        clearPlotSymbol.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
        clearPlotSymbol.size = CGSizeMake(0,0);

        //Average plots
        self.metabolismHighplot = [[CPTScatterPlot alloc] init ];
        self.metabolismHighplot.identifier = @"metahigh";
        self.metabolismHighplot.delegate=self;
        self.metabolismHighplot.title=@"High";
        [self.metabolismHighplot setDataLineStyle:avgLineStyle];
        self.metabolismHighplot.dataSource = self;
        self.metabolismHighplot.delegate=self;
        self.metabolismHighplot.plotSymbol=clearPlotSymbol;

        self.metabolismLowplot = [[CPTScatterPlot alloc] init ];
        self.metabolismLowplot.identifier = @"metalow";
        self.metabolismLowplot.delegate=self;
        self.metabolismLowplot.title=@"Low";
        [self.metabolismLowplot setDataLineStyle:avgLineStyle];
        self.metabolismLowplot.dataSource = self;
        self.metabolismLowplot.delegate=self;
        self.metabolismLowplot.plotSymbol=clearPlotSymbol;


        //Add background for the area between average plots
        [self.metabolismHighplot setAreaFill:[CPTFill fillWithColor:targetlinecolor]];
        self.metabolismHighplot.areaBaseValue=CPTDecimalFromInt(metaYlow);
        self.metabolismLowplot.areaFill=[CPTFill fillWithImage:[[CPTImage alloc] initForPNGFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"historyGraphBkgroungtemplate"] ofType:@"png"]]];
        self.metabolismLowplot.areaBaseValue=CPTDecimalFromInt(0);

        //Plot hierarchy
        [currentgraph addPlot:self.metabolismHighplot];
        [currentgraph addPlot:self.metabolismLowplot];
        [currentgraph addPlot:self.Metabolismplot];
    }
}
4

0 回答 0