2011-02-27 69 views
10

我正在使用Core-Plot作爲iPhone應用程序中的趨勢圖並且未找到如何自定義背景。我可以使用內置主題(如kCPPlainWhiteTheme)創建主題,但是如何更改它們?或創建一個新的?核心情節定製主題?

我基本上需要做的是使背景透明。

編輯/ UPDATE

我絕對測試此代碼,但它似乎並沒有工作:

//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"); 

這裏我設置的CPXYGraph *的填充性能chartTrend5* CPXYPlotSpace * chartTrend5PlotSpace爲零。

回答

24

除了Eric的建議之外,您還可以嘗試將填充設置爲清晰的顏色。例如,我用這個在過去提供一個透明的背景圖:

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

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

完美的作品。韓國社交協會。 – oscarm 2011-02-27 18:30:47

+6

如果您擔心任何原因(滾動,調整大小等)的繪圖性能,將這些填充屬性設置爲零將告訴核心繪圖沒有任何繪製。這樣它就知道跳過設置剪裁區域並繪製不會出現在屏幕上的像素。 – 2011-02-27 19:23:02

+0

@Eric - 感謝您指出。我替換了一些清晰填充的代碼,只需將該填充設置爲零,並在不斷重繪陰謀時節省了我的Mac上大約2-3%的CPU負載。 – 2011-02-28 15:32:46

6

主題只是一種方便的方式,可以一次設置很多樣式屬性。您可以單獨設置它們中的任何一個來自定義外觀。任何時候你想要一個區域是透明的,你可以將其填充設置爲nil。與線條樣式相同 - 將它們設置爲nil以防止繪製線條。

您可能會關注兩個「背景」區域。圖表有一個填充和繪圖區域一樣。繪圖區是繪製繪圖的區域。將這兩個屬性上的fill屬性設置爲nil以使背景透明。

+0

感謝您的回覆。似乎沒有工作。 – oscarm 2011-02-27 16:23:20

+0

我剛剛更新了問題以添加一些代碼。 – oscarm 2011-02-27 16:26:40

+1

我不能做chartTrend5PlotSpace.fill = nil; – oscarm 2011-02-27 16:36:15

1

我想這&它爲我工作:

// 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

我用.fill.plot.AreaFrame.fill屬性:

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]]; 

這對我有用。