1
我正在創建一個應用程序,它將在幾個屏幕中顯示幾個圖。因此,它被選擇創建一個獨立的類,從Coreplot框架創建一個圖。除了numberForPlot:plotfield:recordIndex:方法外,所有函數/方法都以正確的方式調用。軸和主機視圖設置正確,但沒有繪製數據。Coreplot方法numberForPlot:plotfield:recordIndex:不會被調用
類被稱爲WiGraph
WiGraph.h
#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"
#import "data.h"
@interface WiGraph : NSObject <CPTPlotDataSource>
@property (nonatomic, strong) NSString *gradient;
@property (nonatomic, strong) CPTGraphHostingView *hostingView;
@property (nonatomic, strong) NSArray *plotData;
@property (nonatomic, strong) CPTScatterPlot *plot;
@property (nonatomic, strong) CPTXYGraph *graph;
- (id)initWithData:(NSArray *)data;
- (void)createGraphInView:(UIView *)content;
- (void)setPlotSpaceWithTimeDifference:(int)diff xStart:(int)starttime xLenght:(int)xlenght yStart:(int)ystart yLength:(int)ylenght;
- (void)addGradientWithRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue andAlpha:(CGFloat)alpha;
- (void)changeDotColor:(CPTColor *)col;
- (void)setAxisFromReferenceTime:(int)time andToXHeight:(float)xheight andYHeight:(float)yheight andXInterval:(float)xint andYInterval:(float)yint andLineColor:(CPTColor*)color;
- (void)addplotToGraph;
@end
WiGraph.m
#import "WiGraph.h"
#import "XMLReader.h"
#import <QuartzCore/QuartzCore.h>
@implementation WiGraph
@synthesize gradient, hostingView, plotData, plot, graph;
-(id)initWithData:(NSArray *)data
{
self = [super init];
if (self) {
self.plotData = data;
self = [self init];
}
return self;
}
#pragma mark create graph
//This function will create a empty graph
- (void)createGraphInView:(UIView *)content
{
hostingView = [[CPTGraphHostingView alloc] initWithFrame:content.bounds];
// Create graph from theme
graph = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
hostingView.hostedGraph = graph;
plot = [[CPTScatterPlot alloc] init];
}
- (void)setAxisFromReferenceTime:(int)time andToXHeight:(float)xheight andYHeight:(float)yheight andXInterval:(float)xint andYInterval:(float)yint andLineColor:(CPTColor*)color;
{
NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:time];
// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
plot.identifier = @"Date Plot";
lineStyle.lineColor = color;
lineStyle.lineWidth = 2.0f;
lineStyle.miterLimit= 1.0f;
plot.dataLineStyle = lineStyle;
plot.dataSource = self;
// X-axis parameters setting
CPTXYAxisSet *axisSet = (id)graph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(xint);
axisSet.xAxis.minorTicksPerInterval = 3;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecim alFromFloat(yheight); //added for date, adjust x line
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 2.0f;
axisSet.xAxis.majorTickLength = 4.0f;
axisSet.xAxis.labelOffset = 1.0f;
// added for date
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormattersec = [[NSDateFormatter alloc]init];
[dateFormattersec setDateFormat:@"HH:mm"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormattersec];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;
// Y-axis parameters setting
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(yint);
axisSet.yAxis.minorTicksPerInterval = 0.5;
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(xheight); // added for date, adjusts y line
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 2.0f;
axisSet.yAxis.majorTickLength = 4.0f;
axisSet.yAxis.labelOffset = 1.0f;
}
-(void)setPlotSpaceWithTimeDifference:(int)diff xStart:(int)starttime xLenght:(int)xlenght yStart:(int)ystart yLength:(int)ylenght
{
graph.paddingLeft = 5.0;
graph.paddingTop = 5.0;
graph.paddingRight = 5.0;
graph.paddingBottom = 5.0;
// setup a plot space for the plot to live in
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(starttime)
length:CPTDecimalFromFloat(xlenght)];
// sets the range of y values
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(ystart)
length:CPTDecimalFromFloat(ylenght)];
}
- (void)addplotToGraph
{
[graph addPlot:plot];
}
#pragma mark Change Colors
- (void)addGradientWithRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue andAlpha:(CGFloat)alpha
{
CPTColor *areaColor1 = [CPTColor colorWithComponentRed:red green:green blue:blue alpha:alpha];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
plot.areaFill = areaGradientFill;
plot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
[self addplotToGraph];
}
- (void)changeDotColor:(CPTColor *)col
{
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:col];
greenCirclePlotSymbol.size = CGSizeMake(3.0, 3.0);
plot.plotSymbol = greenCirclePlotSymbol;
[self addplotToGraph];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return plotData.count;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDictionary *tempDict = [plotData objectAtIndex:index];
int tmp = [[tempDict retrieveForPath:[NSString stringWithFormat:@"%d",fieldEnum]]intValue];
NSDecimalNumber *rr = [[NSDecimalNumber alloc]initWithInt:tmp];
return rr;
}
@end
對情節的數量不會被調用,這是否有委託辦?
我目前正在創建的對象爲:
//Create new Graph
grafiek = [[WiGraph alloc]initWithData:self.measuredData.temperature];
//Create empty hostingView with size of viewgraphA
[grafiek createGraphInView:self.viewGraphA];
[grafiek setPlotSpaceWithTimeDifference:(self.measuredData.maxDate-self.measuredData.refDate)
xStart:(-300)
xLenght:((self.measuredData.maxDate-self.measuredData.refDate)+300)
yStart:self.measuredData.minTemp
yLength:(self.measuredData.maxTemp - self.measuredData.minTemp)];
//Set graph color en scale
[grafiek setAxisFromReferenceTime:self.measuredData.refDate
andToXHeight:0
andYHeight:(self.measuredData.minTemp-1)
andXInterval:(self.measuredData.maxDate-self.measuredData.refDate)/4
andYInterval:1
andLineColor:[CPTColor blueColor]];
//Add Gradient
[grafiek addGradientWithRed:1.0
andGreen:0.3
andBlue:0.3
andAlpha:0.8];
//Change dot color
[grafiek changeDotColor:[CPTColor redColor]];
//Adding axis and labels to the graph
[grafiek addplotToGraph];
[self.viewGraphA addSubview:grafiek.hostingView];
任何幫助,將不勝感激
是'-numberOfRecordsForPlot:'被調用?它返回什麼?如果此方法返回零(0),則圖不會調用其他數據源方法。 –
-numberOfRecordsForPlot被調用兩次, –
它返回什麼?大於零的東西? –