我正在创建一个应用程序,它将在多个屏幕中显示多个图。因此,选择创建一个单独的类,该类从 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
没有调用 plot 的数字,这与代表有关吗?
我目前正在将对象创建为:
//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];
任何帮助,将不胜感激