CorePlot主題是一個CPTheme子類。CPTheme提供了一系列方法,你可以覆蓋其中3個方法 從而實現自定義的主題:
1、 -( void )applyThemeToBackground:( CPGraph *)graph;
通過傳遞進來的 CPGraph參數,設置背景CPGraph
2、 -( void )applyThemeToPlotArea:( CPPlotAreaFrame *)plotAreaFrame;
通過傳遞進來的 CPPlotArea參數,設置PlotArea風格
3、 -( void )applyThemeToAxisSet:( CPAxisSet *)axisSet;
通過傳遞進來的 CPAxisSet參數,設置坐標系風格
下面是一個 CPTheme子類的例子:
一、 .h文件
.h文件很簡單,申明父類為CPTheme:
#import <Foundation/Foundation.h>
#import <CorePlot/CorePlot.h>
@interface MyCPTheme : CPXYTheme {
}
@end
二、 .m文件
1、首先我們來為背景設置一個自定義顏色漸變:
-( void )applyThemeToBackground:( CPXYGraph *)graph
{
// 終點色: 20 %的灰度
CPColor *endColor = [ CPColor colorWithGenericGray : 0.2f ];
// 創建一個漸變區:起點、終點都是 0.2 的灰度
CPGradient *graphGradient = [ CPGradient gradientWithBeginningColor :endColor endingColor :endColor];
// 設置中間漸變色 1 ,位置在 30 %處,顏色為 3 0 %的灰
graphGradient = [graphGradient addColorStop :[ CPColor colorWithGenericGray : 0.3f ] atPosition : 0.3f ];
// 設置中間漸變色 2 ,位置在 50 %處,顏色為 5 0 %的灰
graphGradient = [graphGradient addColorStop :[ CPColor colorWithGenericGray : 0.5f ] atPosition : 0.5f ];
// 設置中間漸變色 3 ,位置在 60 %處,顏色為 3 0 %的灰
graphGradient = [graphGradient addColorStop :[ CPColor colorWithGenericGray : 0.3f ] atPosition : 0.6f ];
// 漸變角度:垂直 90 度(逆時針)
graphGradient. angle = 90.0f ;
// 漸變填充
graph. fill = [ CPFill fillWithGradient :graphGradient];
}
2、在坐標軸上畫上網格線:
// 在 Y 軸上添加平行線
-( void )applyThemeToAxisSet:( CPXYAxisSet *)axisSet {
// 設置網格線線型
CPLineStyle *majorGridLineStyle = [ CPLineStyle lineStyle ];
majorGridLineStyle. lineWidth = 1.0f ;
majorGridLineStyle. lineColor = [ CPColor lightGrayColor ];
CPXYAxis *axis=axisSet. yAxis ;
// 軸標簽方向: CPSignNone -無,同 CPSignNegative , CPSignPositive -反向 , 在 y 軸的右邊, CPSignNegative -正向,在 y 軸的左邊
axis. tickDirection = CPSignNegative ;
// 設置平行線,默認是以大刻度線為平行線位置
axis. majorGridLineStyle = majorGridLineStyle ;
// 如果 labelingPolicy 設置為 CPAxisLabelingPolicyNone , majorGridLineStyle 將不起作用
//axis.labelingPolicy = CPAxisLabelingPolicyNone ;
}
3、設置繪圖區
下面的代碼在繪圖區( PlotArea)填充一個灰色漸變。注意由于繪圖區(PlotArea)位于背景圖(Graph)的上層(參考 Core Plot 框架的類層次圖 ),因此對于繪圖區所做的設置會覆蓋對 Graph 所做的設置,除非你故意在 Graph 的 4 邊留白,否則看不到背景圖的設置。
-( void )applyThemeToPlotArea:( CPPlotAreaFrame *)plotAreaFrame
{
// 創建一個 20 % -50 %的灰色漸變區,用于設置繪圖區。
CPGradient *gradient = [ CPGradient gradientWithBeginningColor :[ CPColor colorWithGenericGray : 0.2f ] endingColor :[ CPColor colorWithGenericGray : 0.7f ]];
gradient. angle = 45.0f ;
// 漸變填充
plotAreaFrame. fill = [ CPFill fillWithGradient :gradient];
}
三、應用主題
很簡單,將 CPTheme替換成我們自己的主題,并應用到CPGraph:
#import "MyCPTheme.h"
??
CPTheme *theme=[[ MyCPTheme alloc ] init ];
[ graph applyTheme :theme];
來看看效果:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
