Cocoa touch 无法沿CorePlot的X轴绘制NSdate

Cocoa touch 无法沿CorePlot的X轴绘制NSdate,cocoa-touch,nsdate,core-plot,Cocoa Touch,Nsdate,Core Plot,我是coreplot新手,我想沿着使用coreplot绘制的条形图的x轴显示NSdate(年、月或日)。我已经查看了一些样本,以便沿着x轴绘制NSdate。但我无法绘制 请任何人能提供我沿x轴绘制NSDate的代码。。。 谢谢 我尝试了stackoverflow的以下代码,但无法在x轴上显示标签 // If you make sure your dates are calculated at noon, you shouldn't have to // worry about daylight

我是coreplot新手,我想沿着使用coreplot绘制的条形图的x轴显示NSdate(年、月或日)。我已经查看了一些样本,以便沿着x轴绘制NSdate。但我无法绘制

请任何人能提供我沿x轴绘制NSDate的代码。。。 谢谢

我尝试了stackoverflow的以下代码,但无法在x轴上显示标签

// If you make sure your dates are calculated at noon, you shouldn't have to 
// worry about daylight savings. If you use midnight, you will have to adjust
// for daylight savings time.
NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10];
NSTimeInterval oneDay = 24 * 60 * 60;

// Invert graph view to compensate for iOS coordinates
//CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
// self.view.transform = verticalFlip;

 // allocate the graph within the current view bounds
graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];

// assign theme to graph
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];

// Setting the graph as our hosting layer
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];

[self.view addSubview:hostingView];

hostingView.hostedGraph = graph;

graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 150.0;

// setup a plot space for the plot to live in
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
// sets the range of x values
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow)
                                               length:CPTDecimalFromFloat(oneDay*5.0f)];
// sets the range of y values
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) 
                                               length:CPTDecimalFromFloat(5)];

// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;

// X-axis parameters setting
CPTXYAxisSet *axisSet = (id)graph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay);
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); //added for date, adjust x line
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;

// added for date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;

// Y-axis parameters setting    
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"0.5");
axisSet.yAxis.minorTicksPerInterval = 2;
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay); // added for date, adjusts y line
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;


// This actually performs the plotting
CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease];

CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
//xSquaredPlot.identifier = @"X Squared Plot";
xSquaredPlot.identifier = @"Date Plot";

dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle = dataLineStyle;
xSquaredPlot.dataSource = self;

CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;  

// add plot to graph
[graph addPlot:xSquaredPlot];

// Add some data
NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < 5; i++ ) {
    NSTimeInterval x = oneDay*i;
    id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
    [newData addObject:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX], 
      y, [NSNumber numberWithInt:CPTScatterPlotFieldY], 
      nil]];
    NSLog(@"%@",newData);
}
plotData = [newData retain];

}

#pragma mark - Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return plotData.count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:     (NSUInteger)index
 {
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
//如果你确定你的日期是在中午计算的,你就不必这样做了
//担心夏令时。如果你使用午夜,你将不得不调整
//夏令时。
NSDate*refDate=[NSDate DATE WITH TIME INTERVALUES INCERFERENCEDATE:31556926*10];
NSTimeInterval一天=24*60*60;
//反转图形视图以补偿iOS坐标
//CGAffineTransform verticalFlip=CGAffineTransformMakeScale(1,-1);
//self.view.transform=垂直翻转;
//在当前视图范围内分配图形
graph=[[CPTXYGraph alloc]initWithFrame:self.view.bounds];
//为图表指定主题
CPTTheme*theme=[CPTTheme themeName:kCPTDarkGradientTheme];
[图形应用主题:主题];
//将图形设置为托管层
CPTGraphHostingView*hostingView=[[CPTGraphHostingView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
hostingView.hostedGraph=图形;
graph.paddingLeft=20.0;
graph.paddingTop=20.0;
graph.paddingRight=20.0;
graph.paddingBottom=150.0;
//设置要居住的打印空间
CPTXYPlotSpace*plotSpace=(CPTXYPlotSpace*)graph.defaultPlotSpace;
NSTimeInterval xLow=0.0f;
//设置x值的范围
plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow)
长度:CPTDecimalFromFloat(一天*5.0f)];
//设置y值的范围
plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)
长度:cptdecimlfromfloat(5)];
//打印样式设置为直线打印
CPTMutableLineStyle*lineStyle=[CPTMutableLineStyle];
lineStyle.lineColor=[CPTColor blackColor];
lineStyle.lineWidth=2.0f;
//X轴参数设置
CPTXYAxisSet*axisSet=(id)graph.axisSet;
axisSet.xAxis.majorIntervalLength=CPTDecimalFromFloat(一天);
axisSet.xAxis.minorTicksPerInterval=0;
axisSet.xAxis.OrthogonalCoordinatedCIMAL=CPTDecimalFromString(@“1”)//添加日期,调整x行
axisSet.xAxis.majorTickLineStyle=线条样式;
axisSet.xAxis.minorTickLineStyle=线条样式;
axisSet.xAxis.axisLineStyle=线型;
axisSet.xAxis.minorTickLength=5.0f;
axisSet.xAxis.majorTickLength=7.0f;
axisSet.xAxis.labelOffset=3.0f;
//添加日期
NSDateFormatter*dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateStyle=kCFDateFormatterShortStyle;
CPTTimeFormatter*timeFormatter=[[CPTTimeFormatter alloc]initWithDateFormatter:dateFormatter]autorelease];
timeFormatter.referenceDate=refDate;
axisSet.xAxis.labelFormatter=时间格式;
//Y轴参数设置
axisSet.yAxis.majorIntervalLength=CPTDecimalFromString(@“0.5”);
axisSet.yAxis.minorTicksPerInterval=2;
axisSet.yAxis.OrthogonalCoordinatedCIMAL=CPTDecimalFromFloat(一天);//添加日期,调整y线
axisSet.yAxis.majorTickLineStyle=线条样式;
axisSet.yAxis.minorTickLineStyle=线条样式;
axisSet.yAxis.axisLineStyle=线型;
axisSet.yAxis.minorTickLength=5.0f;
axisSet.yAxis.majorTickLength=7.0f;
axisSet.yAxis.labelOffset=3.0f;
//这实际上执行打印
cptspatterplot*xSquaredPlot=[[cptspatterplot alloc]init]autorelease];
CPTMutableLineStyle*dataLineStyle=[CPTMutableLineStyle];
//xSquaredPlot.identifier=@“X平方图”;
xSquaredPlot.identifier=@“日期图”;
dataLineStyle.lineWidth=1.0f;
dataLineStyle.lineColor=[CPTColor redColor];
xSquaredPlot.dataLineStyle=dataLineStyle;
xSquaredPlot.dataSource=self;
CPTPlotSymbol*greenCirclePlotSymbol=[CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill=[CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size=CGSizeMake(2.0,2.0);
xSquaredPlot.plotSymbol=绿色圆圈plotSymbol;
//将绘图添加到图形中
[图形添加图:xSquaredPlot];
//添加一些数据
NSMutableArray*newData=[NSMutableArray];
NSUI;
对于(i=0;i<5;i++){
n时间间隔x=一天*i;
id y=[NSDecimalNumber numberWithFloat:1.2*rand()/(float)rand_MAX+1.2];
[newData addObject:
[NSDictionary Dictionary WithObjects和Keys:
[NSDecimalNumber numberWithFloat:x],[NSNumber NUMBERWITHIT:CPTScatterPlotFieldX],
y、 [NSNumber numberWithInt:CPTSpatterPlotFieldy],
无]];
NSLog(@“%@”,新数据);
}
plotData=[newData retain];
}
#pragma标记-绘图数据源方法
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot*)绘图
{
返回plotData.count;
}
-(NSNumber*)numberForPlot:(CPTPlot*)绘图字段:(NSInteger)字段枚举记录索引:(NSInteger)索引
{
NSDecimalNumber*num=[[plotData objectAtIndex:index]objectForKey:[NSNumber numberwhithint:fieldEnum]];
返回num;
}

如果你想做这样的事情。。。(我对图像的大小表示歉意,我不知道如何调整图像的大小)

您需要为X轴定义自定义标签

考虑以下代码片段(取自生成上述图像的代码):

/*--为数据元素定义一些自定义标签--*/
//注意,x的定义为:CPTXYAxisSet*axisSet=(CPTXYAxisSet*)self.graph.axisSet;
//CPTXYAxis*x=axisSet.xAxis;
x、 labelingPolicy=CPTAxisLabelingPolicyNone;//这允许我们为x轴创建自定义轴标签
NSMutableArray*刻度=[NSMutableArray阵列容量:1];
for(无符号整数计数器=0;计数器<[\u AxislabelString计数];计数器++){
//这里的实例变量_axisLabelString是custo的列表
/* --Define some custom labels for the data elements-- */
// Note that x is defined as: CPTXYAxisSet *axisSet = (CPTXYAxisSet*)self.graph.axisSet;
//                                      CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone; // This allows us to create custom axis labels for x axis
NSMutableArray *ticks = [NSMutableArray arrayWithCapacity:1];
for(unsigned int counter = 0; counter < [_axisLabelStrings count];counter++) {
    // Here the instance variable _axisLabelStrings is a list of custom labels
    [ticks addObject:[NSNumber numberWithInt:counter]];
}
NSUInteger labelLocation = 0;
NSMutableArray* customLabels = [NSMutableArray arrayWithCapacity:[_axisLabelStrings count]];
@try {
    for (NSNumber *tickLocation in ticks) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [_axisLabelStrings objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = 3.0f;//x.labelOffset + x.majorTickLength could be useful here.
        newLabel.rotation = M_PI/3.5f;
        [customLabels addObject:newLabel];
        [newLabel release];
    }
}
@catch (NSException * e) {
    NSLog(@"An exception occurred while creating date labels for x-axis");
}
@finally {
    x.axisLabels =  [NSSet setWithArray:customLabels];  
}
x.majorTickLocations = [NSSet setWithArray:ticks];