core plot 使用方法

 

- (void)constructScatterPlot

{

// Create graph from theme

graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];

CPTheme *theme = [CPTheme themeNamed: @"Dark Gradients"];

[graph applyTheme:theme];

scatterPlotView.hostedGraph = graph;

 

graph.paddingLeft = 10.0;

graph.paddingTop = 10.0;

graph.paddingRight = 10.0;

graph.paddingBottom = 10.0;

 

// Setup plot space

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;

plotSpace.allowsUserInteraction = YES;

//设置x y

plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(2.0)];

plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(3.0)];

 

// Axes

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

CPXYAxis *x = axisSet.xAxis;

x.majorIntervalLength = CPDecimalFromString(@"2");//设置坐标轴的单位长度

x.orthogonalCoordinateDecimal = CPDecimalFromString(@"2");//设置坐标系的交叉位置

x.minorTicksPerInterval = 2;

NSArray *exclusionRanges = [NSArray arrayWithObjects:

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.99) length:CPDecimalFromFloat(0.02)],

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.99) length:CPDecimalFromFloat(0.02)],

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(2.99) length:CPDecimalFromFloat(0.02)],

nil];

x.labelExclusionRanges = exclusionRanges;

 

CPXYAxis *y = axisSet.yAxis;

y.majorIntervalLength = CPDecimalFromString(@"0.5");

y.minorTicksPerInterval = 5;

y.orthogonalCoordinateDecimal = CPDecimalFromString(@"2");

exclusionRanges = [NSArray arrayWithObjects:

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.99) length:CPDecimalFromFloat(0.02)],

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.99) length:CPDecimalFromFloat(0.02)],

[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(3.99) length:CPDecimalFromFloat(0.02)],

nil];

y.labelExclusionRanges = exclusionRanges;

 

// Create a green plot area

//创建绿色区域

CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];

dataSourceLinePlot.identifier = @"Green Plot";

 

//设置绿色区域边框的样式

CPMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];

lineStyle.lineWidth = 3.f;

   lineStyle.lineColor = [CPColor greenColor];

lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];

dataSourceLinePlot.dataLineStyle = lineStyle;

 

//设置数据元代理

dataSourceLinePlot.dataSource = self;

 

// Put an area gradient under the plot above

CPColor *areaColor = [CPColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];

//设置区域的颜色,第一个为结束颜色,第二个为开始颜色

CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor endingColor:[CPColor clearColor]];

areaGradient.angle = -90.0f;

CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];

dataSourceLinePlot.areaFill = areaGradientFill;

dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"2");//填充的范围,通常设置为y轴的最大范围

 

// Animate in the new plot, as an example

dataSourceLinePlot.opacity = 0.0f;//设置开的透明度, 然后会有动画

dataSourceLinePlot.cachePrecision = CPPlotCachePrecisionDecimal;

[graph addPlot:dataSourceLinePlot];//简短添加一个默认的曲线到默认的曲线空间。

 

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

fadeInAnimation.duration = 1.0f;

fadeInAnimation.removedOnCompletion = NO;

fadeInAnimation.fillMode = kCAFillModeForwards;

fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];//动画最终结束的值

[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];

 

// Create a blue plot area

CPScatterPlot *boundLinePlot = [[[CPScatterPlot alloc] init] autorelease];

boundLinePlot.identifier = @"Blue Plot";

 

lineStyle = [[boundLinePlot.dataLineStyle mutableCopy] autorelease];

lineStyle.miterLimit = 1.0f;

lineStyle.lineWidth = 3.0f;

lineStyle.lineColor = [CPColor blueColor];

lineStyle = lineStyle;

 

boundLinePlot.dataSource = self;

boundLinePlot.cachePrecision = CPPlotCachePrecisionDouble;

boundLinePlot.interpolation = CPScatterPlotInterpolationHistogram;

[graph addPlot:boundLinePlot];

 

// Do a blue gradient

CPColor *areaColor1 = [CPColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];

CPGradient *areaGradient1 = [CPGradient gradientWithBeginningColor:areaColor1 endingColor:[CPColor clearColor]];

areaGradient1.angle = -90.0f;

areaGradientFill = [CPFill fillWithGradient:areaGradient1];

boundLinePlot.areaFill = areaGradientFill;

boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];

 

// Add plot symbols 在坐标处添加一个点

CPMutableLineStyle *symbolLineStyle = [CPMutableLineStyle lineStyle];

symbolLineStyle.lineColor = [CPColor blackColor];

CPPlotSymbol *plotSymbol = [CPPlotSymbol ellipsePlotSymbol];

plotSymbol.fill = [CPFill fillWithColor:[CPColor blueColor]];

plotSymbol.lineStyle = symbolLineStyle;

plotSymbol.size = CGSizeMake(10.0, 10.0);

boundLinePlot.plotSymbol = plotSymbol;

//dataSourceLinePlot.plotSymbol = plotSymbol;

 

// Add some initial data//添加坐标的临时数据

NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];

NSUInteger i;

for ( i = 0; i < 60; i++ ) {

id x = [NSNumber numberWithFloat:1+i*0.05];

id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];//2147483647

[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];

}

self.dataForPlot = contentArray;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值