2011-02-25 4 views
7

Cómo cambiar el color del degradado de área para los valores negativos en Gráfico de dispersión de degradado en Core-Plot en iPhone?iPhone: ¿Cómo cambiar el color del degradado para valores negativos en Core Plot?

Quiero colores de la pendiente de la siguiente manera:

  • Para valores positivos sean verde

  • Para valores negativos sean Rojo.

¿Cómo debo hacer eso?

+0

Es probable que necesita para normalizar sus valores. Por lo que puedo ver, CPGradient solo acepta 'CGFloat 0 - 1'. Entonces, si sus valores van desde -1024 hasta +1024, deberá agregar un desplazamiento y dividir por el rango total. Eso te dará un número del 0-1. No puedo responder con certeza, b/c. Nunca he trabajado con CorePlot. –

+0

@Stephen Furlani: gracias por la entrada. No tengo claro lo que has dicho. ¿Puede explicarlo en detalle? –

+0

Ya sabe, ya sea de antemano, o mirando su conjunto de datos, que todos los valores 'n' son tales que' j ≤ n ≤ k'. Entonces, puede obtener el valor de gradiente como '(n - j)/(k - j)', que estará en el rango '[0-1]'. Esto interpola linealmente entre 'j' y' k'. – tJener

Respuesta

3

Sólo implementan el siguiente método de la CPBarPlotDataSource está bien:

- (CPFill *)barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSUInteger)index 
{ 
    if (barPlot.identifier == @"profit") { 
     id item = [self.profits objectAtIndex:index]; 
     double profit = [[item objectForKey:@"profit"] doubleValue]; 

     if (profit < 0.0) { 
      return [CPFill fillWithGradient:[CPGradient gradientWithBeginningColor:[CPColor redColor] endingColor:[CPColor blackColor]]]; 
     } 
    } 

    return nil; 
} 

esperamos ayudar :)

+0

No estoy seguro de por qué se ha marcado esta respuesta como aceptable, ya que la pregunta se trata de un diagrama de dispersión. Cuando implemento '-barFillForBarPlot: recordIndex:' cuando se utiliza un diagrama de dispersión, nunca se llama al método. ¿Cómo solucionó esto el problema? – simonbs

2

Bueno, no sé si es demasiado bonito, pero supongo que podría hacer dos trazados separados con el mismo dataSource, digamos positivePlot y negativePlot.

CPScatterPlot *positivePlot = [[[CPScatterPlot alloc] init] autorelease]; 
positivePlot.identifier = @"PositivePlot"; 
positivePlot.dataSource = self; 
[graph addPlot:positivePlot]; 

CPScatterPlot *negativevePlot = [[[CPScatterPlot alloc] init] autorelease]; 
negativevePlot.identifier = @"NegativePlot"; 
negativePlot.dataSource = self; 
[graph addPlot:negativePlot]; 

Ahora, si configura su plotSpaces adecuadamente, puede separar los valores positivos y negativos y configurar cada parcela adecuada, incluyendo el área de gradiente:

CPXYPlotSpace *positivePlotSpace = [graph newPlotSpace]; 
positivePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) 
              length:CPDecimalFromFloat(100)]; 
positivePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) 
              length:CPDecimalFromFloat(100)]; 

CPXYPlotSpace *negativevePlotSpace = [graph newPlotSpace]; 
negativePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-100) 
              length:CPDecimalFromFloat(100)]; 
negativePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) 
              length:CPDecimalFromFloat(100)]; 


// Plots configuration 


//POSITIVE VALUES 
positivePlot.plotSpace = positivePlotSpace; 

// Green for positive 
CPColor *areaColor = [CPColor colorWithComponentRed:0.0 
              green:1.0 
              blue:0.0 
              alpha:1.0]; 
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                endingColor:[CPColor clearColor]]; 
areaGradient.angle = -90.0f; 
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient]; 
positivePlot.areaFill = areaGradientFill; 


//NEGATIVE VALUES 
negativePlot.plotSpace = negativePlotSpace; 

// Red for negative 
areaColor = [CPColor colorWithComponentRed:1.0 
            green:0.0 
             blue:0.0 
            alpha:1.0]; 
areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
              endingColor:[CPColor clearColor]]; 
areaGradient.angle = -90.0f; 
areaGradientFill = [CPFill fillWithGradient:areaGradient]; 
negativePlot.areaFill = areaGradientFill; 

NOTA: Esta es la parte superior de mi cabeza, ya que no tengo documentos de Core-Plot aquí, o una configuración que funcione para probar esto, por lo que la sintaxis o algo podría estar apagado, pero creo que el concepto general debería funcionar.

Saludos

Cuestiones relacionadas