2012-03-21 10 views
7

Implementé un gráfico CorePlot en mi aplicación iOS, sin embargo, no puedo dimensionar dinámicamente el eje y en función de los puntos de datos en el conjunto. Algunos conjuntos de datos van desde 10 hasta 50, mientras que otros oscilan entre 400 y 500. En ambos casos, me gustaría que el origen y (0) sea visible.CorePlot - Establezca el rango y-Axis para incluir todos los puntos de datos

He intentado usar el método scaletofitplots:

[graph.defaultPlotSpace scaleToFitPlots:[graph allPlots]]; 

pero esto no tiene ningún efecto sobre los gráficos, que todavía muestran la gama por defecto del eje Y de 0 a 1.

La única manera de que puedo cambiar el eje y es hacerlo de forma manual a través de este:

graph.defaultPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)) length:CPTDecimalFromFloat(500.0f)]; 

sin embargo, esto no funciona bien para los gráficos con los rangos más pequeños (10-50), como se puede imaginar.

¿Hay algún método para recuperar el valor y más alto de mi conjunto de datos para poder establecer manualmente el máximo y-Axis en ese valor? ¿O hay una mejor alternativa?

EDIT: Aquí está el archivo que estoy usando:

#import "TUTSimpleScatterPlot.h" 

@implementation TUTSimpleScatterPlot 

@synthesize hostingView = _hostingView; 
@synthesize graph = _graph; 
@synthesize graphData = _graphData; 

-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data { 
    self = [super init]; 

    if (self != nil) { 
     self.hostingView = hostingView; 
     self.graphData = data; 
     self.graph = nil; 
    } 
    return self; 
} 

-(void)initialisePlot { 

    if ((self.hostingView == nil) || (self.graphData == nil)) { 
     NSLog(@"TUTSimpleScatterPlot: Cannot Initialise plot with hosting view and data"); 
     return; 
    } 

    if (self.graph != nil) { 
     NSLog(@"TUTSimpleScatterPlot: Graph Object already exists."); 
    } 
    NSDate *refDate = [NSDate date]; 
    NSTimeInterval oneDay = 24 * 60 * 60; 

    CGRect frame = [self.hostingView bounds]; 
    self.graph = [[CPTXYGraph alloc] initWithFrame:frame]; 

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = YES; 
    plotSpace.delegate = self; 
    self.graph.plotAreaFrame.paddingBottom = 20.0f; 
    self.graph.plotAreaFrame.paddingLeft = 35.0f; 


    self.hostingView.hostedGraph = self.graph; 

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor whiteColor]; 
    lineStyle.lineWidth = 2.0f; 

    CPTMutableLineStyle *lineStyleThin = [CPTMutableLineStyle lineStyle]; 
    lineStyleThin.lineColor = [CPTColor whiteColor]; 
    lineStyleThin.lineWidth = 1.0f; 

    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
    textStyle.fontName = @"HelveticaNeue-Medium"; 
    textStyle.fontSize = 10; 
    textStyle.color = [CPTColor whiteColor]; 

    CPTMutableLineStyle *plotSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
    plotSymbolLineStyle.lineColor = [CPTColor whiteColor]; 
    plotSymbolLineStyle.lineWidth = 1.0f; 

    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    plotSymbol.lineStyle = plotSymbolLineStyle; 
    plotSymbol.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.4 green:0.6 blue:0.8 alpha:1.0]]; 
    plotSymbol.size = CGSizeMake(8.0, 8.0); 

    CPTScatterPlot *plot = [[CPTScatterPlot alloc] init]; 
    plot.dataSource = self; 
    plot.identifier = @"mainPlot"; 

    [plotSpace scaleToFitPlots:[graph allPlots]]; 

    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy]; 
    [yRange expandRangeByFactor:CPTDecimalFromDouble(615)]; 
    plotSpace.yRange = yRange; 

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(oneDay - (oneDay * 6.3f)) length:CPTDecimalFromFloat(oneDay * 6.6f)]; 
    plotSpace.allowsUserInteraction = YES; 

    CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(615.0f)]; 
    plotSpace.globalYRange = globalYRange; 

    // Modify the graph's axis with a label, line style, etc 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet; 

    axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0]; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.labelTextStyle = textStyle; 
    axisSet.xAxis.labelOffset = 1.0f; 
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay*1); 
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0"); 
    axisSet.xAxis.minorTicksPerInterval = 0; 
    axisSet.xAxis.majorTickLength = 0.0f; 
    axisSet.xAxis.majorGridLineStyle = majorGridLineStyle; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd MMM"]; 
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter]; 
    timeFormatter.referenceDate = refDate; 
    axisSet.xAxis.labelFormatter = timeFormatter; 

    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyleThin; 
    axisSet.yAxis.labelTextStyle = textStyle; 
    axisSet.yAxis.labelOffset = 3.0f; 
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"50.0"); 
    axisSet.yAxis.minorTicksPerInterval = 1; 
    axisSet.yAxis.minorTickLength = 3.0; 
    axisSet.yAxis.majorTickLength = 5.0; 
    axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0); 
    axisSet.yAxis.majorGridLineStyle = majorGridLineStyle; 

    axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0]; 

    CPTMutableLineStyle *theLineStyle = [CPTMutableLineStyle lineStyle]; 
    theLineStyle.lineColor = [CPTColor colorWithComponentRed:0.3 green:0.6 blue:0.9 alpha:1.0]; 
    theLineStyle.lineWidth = 2.0f; 

    plot.dataLineStyle = theLineStyle; 
    plot.plotSymbol = plotSymbol; 

    [self.graph addPlot:plot]; 

} 

Respuesta

8

Parece que estás usando -scaleToFitPlots: correctamente. Este método actualizará los datos de trazado si es necesario. Asegúrese de que los datos adecuados estén disponibles para la fuente de datos antes de llamarlo.

Editar basado en el nuevo código:

-scaleToFitPlots: se está llamando antes de agregar la trama a la gráfica. Por lo tanto, la matriz -allPlots está vacía y no hay nada que escalar. Mueva la operación de escala después de construir y agregar el gráfico al gráfico.

+0

He pegado todo el archivo en mi pregunta ahora. Quizás eso ayude a ver si estoy implementando '' -scaleToFitPlots: 'correctamente. – zdestiny

+0

Moví el 'scaleToFitPlots' hasta el final, justo debajo de' [self.graph addPlot: plot] 'y funciona ahora. Gracias por tu ayuda. – zdestiny

Cuestiones relacionadas