根据库Core Plot
中提供的示例 RealTime ,我尝试在我的项目中重新实现它。
我可以更新y axis
,添加符号,绘制标签等等。但我完全无法绘制线条。
CPTMutableLineStyle
对象似乎已正确即时并正确设置为CPTGraph
对象,但未在图表中呈现
以下是我在图表中添加线条样式的方法
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; // [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 3.0;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
这是完整的类代码
#import "TUTSimpleScatterPlot.h"
const double kFrameRate = 5.0; // frames per second
const double kAlpha = 0.15; // smoothing constant
const NSUInteger kMaxDataPoints = 21;
NSString *kPlotIdentifier = @"Data Source Plot";
@implementation TUTSimpleScatterPlot
// Initialise the scatter plot in the provided hosting view with the provided data.
// The data array should contain NSValue objects each representing a CGPoint.
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data
{
self = [super init];
if ( self != nil ) {
self.hostingView = hostingView;
self.graphData = data;
self.graph = nil;
}
return self;
}
-(void)initialisePlot
{
self.graphData = [NSMutableArray array];
CGRect bounds = self.hostingView.bounds;
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds] ;
[self.hostingView setHostedGraph:graph];
graph.title = @"Measure";
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontName = @"Helvetica-Bold";
textStyle.fontSize = round(bounds.size.height / (CGFloat)20.0);
graph.titleTextStyle = textStyle;
graph.titleDisplacement = CGPointMake( 0.0f, round(bounds.size.height / (CGFloat)18.0) ); // Ensure that title displacement falls on an integral pixel
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
CGFloat boundsPadding = round(bounds.size.width / (CGFloat)20.0); // Ensure that padding falls on an integral pixel
graph.paddingLeft = boundsPadding;
if ( graph.titleDisplacement.y > 0.0 ) {
graph.paddingTop = graph.titleDisplacement.y * 2;
}
else {
graph.paddingTop = boundsPadding;
}
graph.paddingRight = boundsPadding;
graph.paddingBottom = boundsPadding;
graph.plotAreaFrame.paddingTop = 15.0;
graph.plotAreaFrame.paddingRight = 15.0;
graph.plotAreaFrame.paddingBottom = 55.0;
graph.plotAreaFrame.paddingLeft = 55.0;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
// Axes
// X axis
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.minorTicksPerInterval = 9;
x.title = @"X Axis";
x.titleOffset = 35.0;
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.numberStyle = NSNumberFormatterNoStyle;
x.labelFormatter = labelFormatter;
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.minorTicksPerInterval = 3;
y.labelOffset = 5.0;
y.title = @"Y Axis";
y.titleOffset = 30.0;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Rotate the labels by 45 degrees, just to show it can be done.
x.labelRotation = M_PI * 0.25;
// Create the plot
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.identifier = kPlotIdentifier;
dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
// dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationCurved;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; // [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 3.0;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [[CPTColor blackColor] colorWithAlphaComponent:0.5];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[[CPTColor blueColor] colorWithAlphaComponent:0.5]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
dataSourceLinePlot.plotSymbol = plotSymbol;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 1)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(1)];
self.graph = graph;
self.dataTimer = [NSTimer timerWithTimeInterval:1.0 / kFrameRate
target:self
selector:@selector(newData:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.dataTimer forMode:NSDefaultRunLoopMode];
}
#pragma mark -
#pragma mark Timer callback
-(void)newData:(NSTimer *)theTimer
{
CPTGraph *theGraph = self.graph;
CPTPlot *thePlot = [theGraph plotWithIdentifier:kPlotIdentifier];
if ( thePlot ) {
// if ( self.graphData.count >= kMaxDataPoints - 5 ) {
// [self.graphData removeObjectAtIndex:0];
// [thePlot deleteDataInIndexRange:NSMakeRange(0, 1)];
// }
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)theGraph.defaultPlotSpace;
NSUInteger location = (_currentIndex >= kMaxDataPoints ? _currentIndex - kMaxDataPoints + 5 : 0);
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location)
length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 1)];
_currentIndex++;
NSNumber * num;
if (_currentIndex == 1) {
num = [NSNumber numberWithDouble: 5 + (arc4random() % 15) ];
} else {
NSLog(@"lastVal: %d", [[self.graphData lastObject] intValue]);
float low_bound = (1 - kAlpha) * [[self.graphData lastObject] floatValue];
float high_bound = (1.01 + kAlpha) * [[self.graphData lastObject] floatValue];
float random = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
NSLog(@"random: %f", random);
num = [NSNumber numberWithDouble:random];
}
// Update y range if necessary
if (_currentIndex < kMaxDataPoints) {
int maxValue = -1;
for (int i = self.graphData.count -1 ; (i >= 0); i--) {
if ([[self.graphData objectAtIndex:i] intValue] > maxValue) {
maxValue = [[self.graphData objectAtIndex:i] intValue];
}
}
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(maxValue + 1)];
} else if (_currentIndex >= kMaxDataPoints) {
int maxValue = -1;
for (int i = self.graphData.count -1 ; (i >= self.graphData.count - kMaxDataPoints + 2); i--) {
if ([[self.graphData objectAtIndex:i] intValue] > maxValue) {
maxValue = [[self.graphData objectAtIndex:i] intValue];
}
}
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(maxValue + 1)];
}
[self.graphData addObject:num];
[thePlot insertDataAtIndex:self.graphData.count - 1 numberOfRecords:1];
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return [self.graphData count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num = nil;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
num = [NSNumber numberWithUnsignedInteger:index + _currentIndex - self.graphData.count];
break;
case CPTScatterPlotFieldY:
num = [self.graphData objectAtIndex:index];
break;
default:
break;
}
return num;
}
@end
有什么想法吗?提示 ?哈克?
更新1
在我的项目中,我使用了库的1.2
版本。我试图回归到1.0
版本,现在显示了行。关于如何将我的项目转换为使用上一版本的任何想法?
更新2 *
将Podfile
从pod 'CorePlot'
更改为pod 'CorePlot', '~> 1.2'
似乎有所不同。我的线现在正确绘制了......
答案 0 :(得分:0)
通过这种方式使用cocoapods
似乎存在差异
pod 'CorePlot'
或通过这种方式
pod 'CorePlot', '~> 1.2'
然后执行pod install
现在该项目能够画线