2012-02-01 2 views
0

J'ai un cas où mon application se connecte au serveur pour obtenir des données et tracer sur le graphique. Lorsque la fonction d'initialisation de l'intrigue est appelée, le graphique est redessiné à un mauvais endroit et Auto se corrige là où il est censé être. Cela provoque un scintillement dans le processus. J'ai une minuterie rised toutes les 10 secondes pour me connecter au serveur. Voici le code d'initialisation du tracé.scintillement iOS noyau graphique Graphique

-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data 
    { 

    self = [super init]; 

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

return self; 
} 

// Ceci fait le travail actuel de création du tracé si nous n'avons pas déjà un objet graphique.

-(void)initialisePlot:(NSUInteger)indexOfPlot 
    { 
    // Start with some simple sanity checks before we kick off 
    if ((self.hostingView == nil) || (self.graphData == nil)) { 
    NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or  data."); 
    return; 
    } 

if (self.graph != nil) { 
    NSLog(@"TUTSimpleScatterPlot: Graph object already exists."); 
    return; 
} 

// Create a graph object which we will use to host just one scatter plot. 

CGRect frame; 
//CGRect frame = [self.hostingView bounds]; 
if(indexOfPlot == 0){ 
self.graph = [[CPTXYGraph alloc] initWithFrame:frame]; 

// Add some padding to the graph, with more at the bottom for axis labels. 
self.graph.plotAreaFrame.paddingTop = 500.0f; 
self.graph.plotAreaFrame.paddingRight = 20.0f; 
self.graph.plotAreaFrame.paddingBottom = 50.0f; 
self.graph.plotAreaFrame.paddingLeft= 20.0f; 

// Tie the graph we've created with the hosting view. 
self.hostingView.hostedGraph = self.graph; 

// If you want to use one of the default themes - apply that here. 
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

// Create a line style that we will apply to the axis and data line. 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.lineColor = [CPTColor redColor]; 
lineStyle.lineWidth = 2.0f; 

// Create a text style that we will use for the axis labels. 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.fontName = @"Helvetica"; 
textStyle.fontSize = 14; 
textStyle.color = [CPTColor blackColor]; 

// Create the plot symbol we're going to use. 
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
plotSymbol.lineStyle = lineStyle; 
plotSymbol.size = CGSizeMake(8.0, 8.0); 

// Setup some floats that represent the min/max values on our axis. 
float xAxisMin = -10; 
float xAxisMax = 10; 
float yAxisMin = 0; 
float yAxisMax = 100; 

// We modify the graph's plot space to setup the axis' min/max values. 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)]; 

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

axisSet.xAxis.title = @"Data X"; 
axisSet.xAxis.titleTextStyle = textStyle; 
axisSet.xAxis.titleOffset = 30.0f; 
axisSet.xAxis.axisLineStyle = lineStyle; 
axisSet.xAxis.majorTickLineStyle = lineStyle; 
axisSet.xAxis.minorTickLineStyle = lineStyle; 
axisSet.xAxis.labelTextStyle = textStyle; 
axisSet.xAxis.labelOffset = 3.0f; 
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f); 
axisSet.xAxis.minorTicksPerInterval = 1; 
axisSet.xAxis.minorTickLength = 5.0f; 
axisSet.xAxis.majorTickLength = 7.0f; 

axisSet.yAxis.title = @"Data Y"; 
axisSet.yAxis.titleTextStyle = textStyle; 
axisSet.yAxis.titleOffset = 40.0f; 
axisSet.yAxis.axisLineStyle = lineStyle; 
axisSet.yAxis.majorTickLineStyle = lineStyle; 
axisSet.yAxis.minorTickLineStyle = lineStyle; 
axisSet.yAxis.labelTextStyle = textStyle; 
axisSet.yAxis.labelOffset = 3.0f; 
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(10.0f); 
axisSet.yAxis.minorTicksPerInterval = 1; 
axisSet.yAxis.minorTickLength = 5.0f; 
axisSet.yAxis.majorTickLength = 7.0f; 

// Add a plot to our graph and axis. We give it an identifier so that we 
// could add multiple plots (data lines) to the same graph if necessary. 
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init]; 
plot.dataSource = self; 
plot.identifier = @"mainplot"; 
plot.dataLineStyle = lineStyle; 
plot.plotSymbol = plotSymbol; 
    //[self.graph reloadData]; 
[self.graph addPlot:plot]; 
} 
if(indexOfPlot == 1){ 
    self.graph = [[CPTXYGraph alloc] initWithFrame:frame]; 

    // Add some padding to the graph, with more at the bottom for axis labels. 
    self.graph.plotAreaFrame.paddingTop = 20.0f; 
    self.graph.plotAreaFrame.paddingRight = 20.0f; 
    self.graph.plotAreaFrame.paddingBottom = 600.0f; 
    self.graph.plotAreaFrame.paddingLeft= 20.0f; 

    // Tie the graph we've created with the hosting view. 
    self.hostingView.hostedGraph = self.graph; 

    // If you want to use one of the default themes - apply that here. 
    [self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

    // Create a line style that we will apply to the axis and data line. 
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor redColor]; 
    lineStyle.lineWidth = 2.0f; 

    // Create a text style that we will use for the axis labels. 
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
    textStyle.fontName = @"Helvetica"; 
    textStyle.fontSize = 14; 
    textStyle.color = [CPTColor blackColor]; 

    // Create the plot symbol we're going to use. 
    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol crossPlotSymbol]; 
    plotSymbol.lineStyle = lineStyle; 
    plotSymbol.size = CGSizeMake(8.0, 8.0); 

    // Setup some floats that represent the min/max values on our axis. 
    float xAxisMin = -10; 
    float xAxisMax = 10; 
    float yAxisMin = 0; 
    float yAxisMax = 100; 

    // We modify the graph's plot space to setup the axis' min/max values. 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)]; 

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

    axisSet.xAxis.title = @"Data X"; 
    axisSet.xAxis.titleTextStyle = textStyle; 
    axisSet.xAxis.titleOffset = 30.0f; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLineStyle = lineStyle; 
    axisSet.xAxis.labelTextStyle = textStyle; 
    axisSet.xAxis.labelOffset = 3.0f; 
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f); 
    axisSet.xAxis.minorTicksPerInterval = 1; 
    axisSet.xAxis.minorTickLength = 5.0f; 
    axisSet.xAxis.majorTickLength = 7.0f; 

    axisSet.yAxis.title = @"Data Y"; 
    axisSet.yAxis.titleTextStyle = textStyle; 
    axisSet.yAxis.titleOffset = 40.0f; 
    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyle; 
    axisSet.yAxis.labelTextStyle = textStyle; 
    axisSet.yAxis.labelOffset = 3.0f; 
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(10.0f); 
    axisSet.yAxis.minorTicksPerInterval = 1; 
    axisSet.yAxis.minorTickLength = 5.0f; 
    axisSet.yAxis.majorTickLength = 7.0f; 

    // Add a plot to our graph and axis. We give it an identifier so that we 
    // could add multiple plots (data lines) to the same graph if necessary. 
    CPTScatterPlot *plot = [[CPTScatterPlot alloc] init]; 
    plot.dataSource = self; 
    plot.identifier = @"mainplot"; 
    plot.dataLineStyle = lineStyle; 
    plot.plotSymbol = plotSymbol; 
    //[self.graph reloadData]; 
    [self.graph addPlot:plot]; 

    } 
} 

Dois-je reconsidérer la façon dont il est en cours d'initialisation?

+0

Pourquoi tout ce code dupliqués pour 'indexOfPlot == 0' et' indexOfPlot == 1'? Que se passe-t-il si vous passez un autre index? –

+0

Bonjour Eric, j'essaie d'ajouter deux graphiques dans la même page. Cependant, la fonction ci-dessus me permet d'utiliser seulement un à la fois. Je n'ai pas essayé de passer un autre index. si cela se produit, il doit s'agir d'un écran vide –

Répondre

0

Initialisez le frame. Vous avez probablement des valeurs indésirables dans ce graphique lorsque le graphique est initialisé, puis corrigé lorsque vous ajoutez le graphique à la vue d'hébergement.

CGRect frame = [self.hostingView bounds]; 

ou même

CGRect frame = CGRectZero; 
+0

Les étiquettes sont toujours en mouvement lorsque la fonction initialiseplot est appelée. –

Questions connexes