【问题标题】:iOS Core-Plot scroll doesn't work welliOS Core-Plot 滚动效果不佳
【发布时间】:2015-05-21 09:09:49
【问题描述】:

使用 CorePlot 1.6 版。 运行 iOS 8.3。

我使用 Core-Plot 和 ScatterPlot 来表示多条线。 当我滚动时,图表会跳转并锁定。我不知道会发生什么。你能帮助我吗? 这是我的代码:

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface TemporalLineViewController:UIViewController<CPTPlotDataSource,
CPTScatterPlotDelegate>

@property (strong, nonatomic) IBOutlet CPTGraphHostingView *hostingView;

@end


@interface TemporalLineViewController (){
    NSMutableDictionary *dates;
}
@property (nonatomic, readwrite, strong) NSArray *plotData;
@end

@implementation TemporalLineViewController
-(CPTGraph *)createGraph{
   initWithFrame:CGRectMake(viewGraph.frame.origin.x, viewGraph.frame.origin.y, 500, viewGraph.frame.size.height)];

    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];
    [graph applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]];
    hostingView.hostedGraph=graph;

    // Axes
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    x.majorIntervalLength         = CPTDecimalFromDouble(ONEDAY);
    x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
    x.minorTicksPerInterval       = 0;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy"];
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
    timeFormatter.referenceDate = [[NSDate alloc]init];
    x.labelFormatter            = timeFormatter;
    x.labelRotation             = CPTFloat(M_PI_4);

    CPTXYAxis *y = axisSet.yAxis;
    y.hidden=YES;
    y.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
    return graph;
}

-(void)createScatterPlot{

    CPTGraph * graph = [self createGraph];

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    NSTimeInterval xLow       = 0.0;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow) length:CPTDecimalFromDouble(ONEDAY * 10.0)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1) length:CPTDecimalFromDouble(5.0)];
    plotSpace.allowsUserInteraction = YES;
    plotSpace.allowsMomentum = YES;

    CPTScatterPlot *diagnosticPlot = [self createScatterPlotWithIdentifier:DIAGNOSTIC_PLOT withLineColor:[CPTColor greenColor] withFillColor:[CPTColor greenColor]];
    CPTScatterPlot *vaccinePlot = [self createScatterPlotWithIdentifier:VACCINE_PLOT withLineColor:[CPTColor blackColor] withFillColor:[CPTColor blackColor]];
    CPTScatterPlot *interventionPlot = [self createScatterPlotWithIdentifier:INTERVENTION_PLOT withLineColor:[CPTColor blueColor] withFillColor:[CPTColor blueColor]];
    CPTScatterPlot *smokingHabitPlot = [self createScatterPlotWithIdentifier:SMOKING_HABIT_PLOT withLineColor:[CPTColor yellowColor] withFillColor:[CPTColor yellowColor]];
    CPTScatterPlot *menstrualPlot = [self createScatterPlotWithIdentifier:MENSTRUAL_PLOT withLineColor:[CPTColor redColor] withFillColor:[CPTColor redColor]];
    CPTScatterPlot *menstrualTreatmentPlot = [self createScatterPlotWithIdentifier:MENSTRUAL_TREATMENT_PLOT withLineColor:[CPTColor orangeColor] withFillColor:[CPTColor orangeColor]];
    CPTScatterPlot *psychomotorMilestonePlot = [self createScatterPlotWithIdentifier:PSYCHOMOTOR_MILESTONE_PLOT withLineColor:[CPTColor purpleColor] withFillColor:[CPTColor purpleColor]];

    [graph addPlot:vaccinePlot toPlotSpace:plotSpace];
    [graph addPlot:diagnosticPlot toPlotSpace:plotSpace];
    [graph addPlot:interventionPlot toPlotSpace:plotSpace];
    [graph addPlot:smokingHabitPlot toPlotSpace:plotSpace];
    [graph addPlot:menstrualPlot toPlotSpace:plotSpace];
    [graph addPlot:menstrualTreatmentPlot toPlotSpace:plotSpace];
    [graph addPlot:psychomotorMilestonePlot toPlotSpace:plotSpace];   
}

-(CPTScatterPlot *)createScatterPlotWithIdentifier:(NSString *)identifier withLineColor:(CPTColor *)lineColor withFillColor:(CPTColor *) fillColor{
    CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
    dataSourceLinePlot.identifier = identifier;

    CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
    lineStyle.lineWidth              = 3.0;
    lineStyle.lineColor              = lineColor;
    dataSourceLinePlot.dataLineStyle = lineStyle;
    CPTPlotSymbol *plotSymbol2 = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol2.fill               = [CPTFill fillWithColor:fillColor];
    plotSymbol2.size               = CGSizeMake(10.0, 10.0);
    dataSourceLinePlot.plotSymbol = plotSymbol2;

    return dataSourceLinePlot;
}

-(void)generateData{
    NSMutableArray *data = [NSMutableArray array];

    [data addObject:[self createDiagnosticData]];
    [data addObject:[self createVaccineData]];
    [data addObject:[self createInterventionData]];
    [data addObject:[self createSmokingHabitData]];
    [data addObject:[self createMenstrualData]];
    [data addObject:[self createMenstrualTreatmentData]];
    [data addObject:[self createPsychomotorMilestoneData]];
    self.plotData = data;
}

#pragma mark -
#pragma mark Plot Data Source Methods

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    NSLog(@"numberOfRecordsForPlot");
    if ([plot.identifier isEqual:DIAGNOSTIC_PLOT]) {
        NSMutableArray *data = self.plotData[0];
        return data.count;
    }else if([plot.identifier isEqual:VACCINE_PLOT]){
        NSMutableArray *data = self.plotData[1];
        return data.count;
    } else if([plot.identifier isEqual:INTERVENTION_PLOT]){
        NSMutableArray *data = self.plotData[2];
        return data.count;
    } else if([plot.identifier isEqual:SMOKING_HABIT_PLOT]){
        NSMutableArray *data = self.plotData[3];
        return data.count;
    } else if([plot.identifier isEqual:MENSTRUAL_PLOT]){
        NSMutableArray *data = self.plotData[4];
        return data.count;
    } else if([plot.identifier isEqual:MENSTRUAL_TREATMENT_PLOT]){
        NSMutableArray *data = self.plotData[5];
        return data.count;
    } else if([plot.identifier isEqual:PSYCHOMOTOR_MILESTONE_PLOT]){
        NSMutableArray *data = self.plotData[6];
        return data.count;
    }
    return 0;
}

-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSLog(@"numberForPlot");
    if ([plot.identifier isEqual:DIAGNOSTIC_PLOT]) {
        NSMutableArray *data = self.plotData[0];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        if ([str intValue]==0) {
            return nil;
        }
        return data[index][@(fieldEnum)];
    }else if([plot.identifier isEqual:VACCINE_PLOT]){
        NSMutableArray *data = self.plotData[1];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        return data[index][@(fieldEnum)];
    }else if([plot.identifier isEqual:INTERVENTION_PLOT]){
        NSMutableArray *data = self.plotData[2];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        return data[index][@(fieldEnum)];
    }else if ([plot.identifier isEqual:SMOKING_HABIT_PLOT]) {
        NSMutableArray *data = self.plotData[3];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        if ([str intValue]==0) {
            return nil;
        }
        return data[index][@(fieldEnum)];
    }else if ([plot.identifier isEqual:MENSTRUAL_PLOT]) {
        NSMutableArray *data = self.plotData[4];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        if ([str intValue]==0) {
            return nil;
        }
        return data[index][@(fieldEnum)];
    }else if ([plot.identifier isEqual:MENSTRUAL_TREATMENT_PLOT]) {
        NSMutableArray *data = self.plotData[5];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        if ([str intValue]==0) {
            return nil;
        }
        return data[index][@(fieldEnum)];
    }else if([plot.identifier isEqual:PSYCHOMOTOR_MILESTONE_PLOT]){
        NSMutableArray *data = self.plotData[6];
        NSString *str = [NSString stringWithFormat:@"%@",(data[index][@(fieldEnum)])];
        return data[index][@(fieldEnum)];
    }
    return nil;
}

【问题讨论】:

  • 你有解决办法吗?
  • 终于找到了。在我的情况下,我的视图控制器位于幻灯片视图控制器中,并且幻灯片视图具有平移手势。那个平移手势与滚动相冲突。因此,如果有人遇到此类问题,请寻找手势或任何自定义触摸事件处理。

标签: ios core-plot


【解决方案1】:

您是否重置了滚动视图的内容大小?如果不是,则将其重置为 ScatterPlot 的帧大小如果那只是滚动视图内的视图

【讨论】:

  • 我没有滚动视图。卷轴属于 ScatterPlot。
  • 那么您尝试将其包含到 UIScrollView 中吗?或者更改散点图的框架,使其能够适应您的框架边界。
  • 我不想将它包含在 UIScrollView 中。该图显示良好,缩放效果很好,但是当我滚动到另一侧时,滚动图会停止它。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多