以下内容是从开源库自带的例子中选出的有实用性的高级用法及部分代码
CPTLineCap类:箭头指示器
例如:
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:bounds];
CPTXYAxisSet *xyAxisSet = (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTLineCap *lineCap = [[CPTLineCap alloc] init];
lineCap.lineStyle = xAxis.axisLineStyle;
lineCap.lineCapType = CPTLineCapTypeSweptArrow;
lineCap.size = CGSizeMake(12.0, 15.0);
lineCap.fill = [CPTFill fillWithColor:xAxis.axisLineStyle.lineColor]; xAxis.axisLineCapMax = lineCap;
CPTGradient类:负责填充色
例如:
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
CPTColor *areaColor = [CPTColor colorWithComponentRed:CPTFloat(1.0) green:CPTFloat(1.0) blue:CPTFloat(1.0) alpha:CPTFloat(0.6)];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]]; areaGradient.angle = -90.0;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromDouble(0.0);
其中,areaBaseValue为设置该填充颜色从哪个值开始描述,上例就是从(0,0)开始填充。
areaFill和areaFill2共两个填充,一个填充上面一个填充下面的颜色。
CPTTradingRangePlot类:每个折点用图片和文字展示具体数值
CPTMutableLineStyle *whiteLineStyle = [CPTMutableLineStyle lineStyle]; whiteLineStyle.lineColor = [CPTColor whiteColor]; whiteLineStyle.lineWidth = 2.0;
CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc] initWithFrame:newGraph.bounds];
ohlcPlot.identifier = @"OHLC";
ohlcPlot.lineStyle = whiteLineStyle; //向上或向下的线条
ohlcPlot.plotStyle = CPTTradingRangePlotStyleCandleStick; ohlcPlot.shadow = whiteShadow;
ohlcPlot.labelShadow = whiteShadow;
[newGraph addPlot:ohlcPlot];
效果如下:
CPTLegend类:条目说明(不同颜色柱状的说明)
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph]; theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:CPTFloat(0.15)]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(16.0, 16.0);
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
whiteTextStyle.fontSize = 12.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 10.0;
theLegend.numberOfRows = 1;
theLegend.paddingLeft = 12.0; theLegend.paddingTop = 12.0;
theLegend.paddingRight = 12.0; theLegend.paddingBottom = 12.0;
graph.legend = theLegend; graph.legendAnchor = CPTRectAnchorBottom;
graph.legendDisplacement = CGPointMake(0.0, 5.0);
Bar Plot 1和Bar Plot 2就是条目说明。
CPTPlotSymbol类:折线的节点用圆点颜色标注,如果需要展示数值时,和CPTTradingRangePlot类类似,可以2选1
例如:
CPTMutableLineStyle * symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor]; symbolLineStyle.lineWidth = 2.0;
CPTPlotSymbol * plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
dataSourceLinePlot.plotSymbol = plotSymbol;
图例:
注:可以参考以下blog,注释较细
CPTScatterPlot类:画折线,也能画直线,可用于类似数据统计的中心线,高位线,警告线
绘制时主要的dataSource的两个代理方法:
1、numberOfRecordsForPlot: 返回散射点个数
2、-(NSNumber*)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnumrecordIndex:(NSUInteger)index 或者
-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index等,都是为了让CorePlot知道每个散射点的具体值(x,y)。
第1个参数指定要绘制的图形对象(CPTPlot),第2个参数指定当前正在绘制的点的字段名(代表x坐标或y坐标),第3个参数表示正在绘制第几个点。
示例图: