2011-02-27 10 views
10

Estoy usando Core-Plot para un gráfico de tendencias en una aplicación de iPhone y no he encontrado cómo personalizar el fondo. Puedo crear un tema usando los temas integrados, como kCPPlainWhiteTheme, pero ¿cómo puedo cambiarlos? o crear uno nuevo?diagrama de núcleo tema personalizado?

Lo que básicamente tengo que hacer es hacer que el fondo sea transparente.

EDITAR/ACTUALIZAR

I Jus probado este código, pero no parece funcionar:

//CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme]; 
    CPTheme *theme = [[CPTheme alloc]init]; 
    chartTrend5 = (CPXYGraph *)[theme newGraph];  
    chartView.hostedGraph = chartTrend5; 
    chartTrend5.paddingLeft = 0.0; 
    chartTrend5.paddingTop = 0.0; 
    chartTrend5.paddingRight = 0.0; 
    chartTrend5.paddingBottom = 0.0; 
    chartTrend5.fill = nil; 
    chartTrend5.borderLineStyle = nil; 
    chartView.hostedGraph.fill = nil; 

    chartTrend5PlotSpace = (CPXYPlotSpace *)chartTrend5.defaultPlotSpace; 
    chartTrend5PlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) 
                   length:CPDecimalFromFloat(5)]; 

    // range is 0-125, but since the frame heights is 90, 
    // we need to convert the points to this adjusted scale. The factor is 0.72 => (90/125) 
    chartTrend5PlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) 
                   length:CPDecimalFromFloat(90)]; 



    CPXYAxisSet *axisSet = (CPXYAxisSet *)chartTrend5.axisSet; 

    CPXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPDecimalFromFloat(100); 
    //x.constantCoordinateValue = CPDecimalFromFloat(2); 
    x.minorTicksPerInterval = 2; 
    x.borderWidth = 0; 
    x.labelExclusionRanges = [NSArray arrayWithObjects: 
           [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-1) 
                  length:CPDecimalFromFloat(800)], 
           nil];; 

    CPXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = CPDecimalFromFloat(100); 
    y.minorTicksPerInterval = 1; 
    //y.constantCoordinateValue = length:CPDecimalFromFloat(2); 
    y.labelExclusionRanges = [NSArray arrayWithObjects: 
           [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-26) 
                  length:CPDecimalFromFloat(100)], 
           nil]; 





    CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease]; 
    dataSourceLinePlot.identifier = @"TrendChart"; 
    dataSourceLinePlot.dataLineStyle.lineWidth = 2.f; 
    dataSourceLinePlot.dataLineStyle.lineColor = [CPColor colorWithComponentRed:(16/255.f) 
                      green:(101/255.f) 
                      blue:(122/255.f) 
                      alpha:1]; 
    dataSourceLinePlot.dataSource = self; 
    [chartTrend5 addPlot:dataSourceLinePlot]; 
    chartTrend5.fill = nil; 
    // Put an area gradient under the plot above 

    CPColor *areaColor = [CPColor colorWithComponentRed:(212/255.f) 
                green:(233/255.f) 
                blue:(216/255.f) 
                alpha:1]; 

    CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                  endingColor:areaColor]; 
    areaGradient.angle = -90.0f; 
    CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient]; 
    dataSourceLinePlot.areaFill = areaGradientFill; 
    dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"5.25"); 

aquí que establezca la propiedad del relleno de CPXYGraph * chartTrend5 y * CPXYPlotSpace * chartTrend5PlotSpace a nil.

Respuesta

24

Además de lo que sugiere Eric, también puede intentar configurar el relleno a un color claro. Por ejemplo, he utilizado en el pasado para proporcionar un fondo transparente para gráficos:

CPTTheme *theme = [CPTTheme themeNamed:kCPPlainWhiteTheme]; 
graph = (CPTXYGraph *)[theme newGraph]; 

graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]]; 
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]]; 
+0

funciona perfectamente. tks. – oscarm

+6

Si le preocupa el rendimiento del dibujo por algún motivo (desplazarse, cambiar el tamaño, etc.), al establecer esas propiedades de relleno en cero le dirá a Core Plot que no hay nada que dibujar. De esta forma, se saltea la configuración de las regiones de recorte y los píxeles de dibujo que no aparecerán en la pantalla. –

+0

@Eric - Gracias por señalarlo. Reemplacé un código en el que tenía un relleno claro con solo poner ese relleno en cero y ahorrar alrededor del 2-3% de la carga de la CPU en mi Mac cuando reenvía constantemente un diagrama. –

6

Los temas son solo una manera conveniente de establecer muchas de las propiedades de estilo a la vez. Puede configurar cualquiera de ellos individualmente para personalizar el aspecto. Cada vez que desee que un área sea transparente, puede establecer su relleno en nil. Lo mismo con los estilos de línea: configúrelos en nil para evitar que se dibuje una línea.

Hay dos áreas de "fondo" que podrían preocuparle. El gráfico tiene un relleno al igual que el área de trazado. El área de la trama es la región donde se trazan las parcelas. Establezca la propiedad fill en estos dos en nil para hacer que el fondo sea transparente.

+0

gracias por la respuesta. no parece funcionar – oscarm

+0

Acabo de actualizar las preguntas para agregar algún código. – oscarm

+1

No puedo hacer chartTrend5PlotSpace.fill = nil; – oscarm

1

probé este & funciona para mí:

// Here 'hostingView' is your CPTGraphHostingView to 
// which you add your graph 
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; 
[hostingView addSubview:backgorundImage]; 
[hostingView sendSubviewToBack:backgroundImage]; 
0

utilicé las .fill y .plot.AreaFrame.fill propiedades:

plot = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme]; 
[plot applyTheme:theme]; 
self.graph.collapsesLayers = NO; 
self.graph.hostedGraph = plot; 

plot.paddingLeft = 1.0; 
plot.paddingTop = 1.0; 
plot.paddingRight = 1.0; 
plot.paddingBottom = 1.0; 
plot.fill = [CPTFill fillWithColor:[CPTColor clearColor]]; 
plot.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]]; 

Esto funciona para mí.

Cuestiones relacionadas