我像这样子类化了真实的绘图图:
注意:我没有放一些我在这里使用的方法来绘制标签并决定图表的酷我放了现在的代码来展示创建实时绘图的想法。
在我的.h
#import <CorePlot/CorePlot.h>
#import <Cocoa/Cocoa.h>
#import "sharedPrefferences.h"
#import "RMBlurredView.h"//Not really needed I am just using it for my custom label
@interface GenericCPTGraphView : NSView <CPTPlotDataSource,CPTPlotSpaceDelegate>
{
CPTGraphHostingView *hostView;
CPTXYGraph *graph;
NSMutableArray *plotData;
NSTextField* percentLabel;
CPTMutableLineStyle *lineStyle;
CPTScatterPlot *dataSourceLinePlot;
NSInteger currentIndex;
NSTimer* dataTimer;
BOOL animated;
}
-(void)updateGraph :(NSNumber*)Load;//method that will add the new Dots to the plot.
@end
在我的.m
#define SECTION_COUNT 15 //maybe will be changed to 16 who knows.
static const double kAlpha = 0.25; // smoothing constant
static const NSUInteger kMaxDataPoints = 40;
static NSString *const kPlotIdentifier = @"Data Source Plot";
@implementation GenericCPTGraphView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
plotData = [[NSMutableArray alloc] initWithCapacity:kMaxDataPoints];
[self generateData];//create plot method
[self checkGraph];
}
return self;
}
-(void)checkGraph
{
currentIndex=0;
NSDate *refDate = [NSDate dateWithNaturalLanguageString:@"12:00 Oct 29, 2009"];
NSTimeInterval oneDay = 1;
// Create graph from theme
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];
[graph applyTheme:theme];
graph.paddingLeft = 1.0;
graph.paddingTop = 1.0;
graph.paddingRight = 1.0;
graph.paddingBottom = 1.0;
graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[self getCPTColorObjectFromHexString:@"062118" alpha:1.0]];
graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.cornerRadius = 0;
hostView = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
hostView.hostedGraph = graph;
RMBlurredView *LabelView = [[RMBlurredView alloc]initWithFrame:CGRectMake(1,0+hostView.frame.size.height-16 , hostView.frame.size.width-2, 15)];
[LabelView setTintColor:[sharedPrefferences colorWithHexColorString:@"000000" alpha:0.5]];
NSTextField* textLabel = [[NSTextField alloc]initWithFrame:CGRectMake(0, 1, LabelView.frame.size.width, 15)];
dataSourceLinePlot.identifier = (__bridge id<NSCopying,NSCoding,NSObject>)(kMDItemIdentifier);
[textLabel setTextColor:[NSColor whiteColor]];
// textLabel
[textLabel setBezeled:NO];
[textLabel setDrawsBackground:NO];
[textLabel setEditable:NO];
[textLabel setSelectable:NO];
[textLabel setStringValue:@"LOAD"];
[textLabel sizeToFit];
percentLabel = [[NSTextField alloc]initWithFrame:CGRectMake(0, 1, LabelView.frame.size.width, 15)];
[percentLabel setTextColor:[NSColor whiteColor]];
[percentLabel setBezeled:NO];
[percentLabel setDrawsBackground:NO];
[percentLabel setEditable:NO];
[percentLabel setSelectable:NO];
// [percentLabel setStringValue:@"X%"];
//[percentLabel sizeToFitWithAlignmentRight];
LabelView.blurRadius = 0;
dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.delegate = self;
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"];
[graph addPlot:dataSourceLinePlot];
NSTimeInterval xLow = 0.0;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow) length:CPTDecimalFromDouble(oneDay * 40.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(100.0)];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.hidden = YES;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
x.minorTicksPerInterval = 0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
CPTXYAxis *y = axisSet.yAxis;
y.hidden = YES;
y.majorIntervalLength = CPTDecimalFromDouble(0.5);
y.minorTicksPerInterval = 1;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay);
// Create a plot that uses the data source method
//CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
y.minorTicksPerInterval = 0;
[LabelView addSubview:percentLabel];
[LabelView addSubview:textLabel];
[graph.hostingView addSubview:LabelView];
[self addSubview:hostView];
}
-(void)updateGraph :(NSNumber*)Load
{
if ( dataSourceLinePlot ) {
if ( plotData.count >= 40 ) {
[plotData removeObjectAtIndex:0];
[dataSourceLinePlot deleteDataInIndexRange:NSMakeRange(0, 1)];
}
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 2)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(1)];
NSUInteger location = (currentIndex >= kMaxDataPoints ? currentIndex - kMaxDataPoints + 2 : 0);
CPTPlotRange *newRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location)
length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 2)];
plotSpace.xRange = newRange;
currentIndex++;
[plotData addObject:[NSNumber numberWithDouble:((Load.doubleValue - kAlpha) / 100.0)]];
[percentLabel setStringValue:[NSString stringWithFormat:@"%ld%%",(Load.integerValue) ]];
graph.borderWidth = 0.0 ;
dataSourceLinePlot.borderWidth = 0.0;
[dataSourceLinePlot setBorderWidth:0.0];
dataSourceLinePlot.identifier = @"Date Plot";
lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 2.0;
[percentLabel sizeToFitWithAlignmentRight];
lineStyle.lineColor = [self decideColor: [self numberOfSectionsToHighlight:Load.doubleValue]];
dataSourceLinePlot.dataLineStyle = lineStyle;
[dataSourceLinePlot insertDataAtIndex:(plotData.count - 1) numberOfRecords:1];//inserting object to the plot.
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
NSLog(@"%lu",(unsigned long)plotData.count);
return plotData.count;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num = nil;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
num = [NSNumber numberWithUnsignedInteger:index + currentIndex - plotData.count];
break;
case CPTScatterPlotFieldY:
num = [plotData objectAtIndex:index];
break;
default:
break;
}
return num;
}
@end