Ios 如何在CPTSatterPlot图形的x轴上显示自定义对象
我已经在y轴上实现了自定义对象(来自webservices),从y轴上的y正交40开始,在y轴上间隔20,并且其工作正常。 目前,我们在x轴上显示的时间间隔为1天。但现在我想显示定制对象(即日期,但有可能在同一日期的两个不同时间发生)(来自webservices),间隔为20,就像在y轴上一样。 我尝试了类似于y轴的方法,但没有成功。所有值均以单个位置显示,但不是以20为间隔显示。 服务器响应示例如下所示,仅适用于x轴。 例如:-Ios 如何在CPTSatterPlot图形的x轴上显示自定义对象,ios,graph,core-plot,Ios,Graph,Core Plot,我已经在y轴上实现了自定义对象(来自webservices),从y轴上的y正交40开始,在y轴上间隔20,并且其工作正常。 目前,我们在x轴上显示的时间间隔为1天。但现在我想显示定制对象(即日期,但有可能在同一日期的两个不同时间发生)(来自webservices),间隔为20,就像在y轴上一样。 我尝试了类似于y轴的方法,但没有成功。所有值均以单个位置显示,但不是以20为间隔显示。 服务器响应示例如下所示,仅适用于x轴。 例如:- { date : “2015-12-25 6:30:
{
date : “2015-12-25 6:30:00”,
date : “2015-12-30 14:00:00”,
date : “2016-01-25 5:30:00”,
date : “2016-01-25 7:00:00”,
date : “2016-01-10 10:00:00”,
date : “2016-01-10 12:30:00”,
date : “2016-01-11 11:00:00”,
date : “2016-01-12 16:30:00”
}
这是我的代码:
-(void)reloadData
{
if ( !self.graph ) {
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
self.graph = newGraph;
newGraph.paddingTop = 0.0;
newGraph.paddingBottom = 0.0;
newGraph.paddingLeft = 0.0;
newGraph.paddingRight = 0.0;
self.graph.plotAreaFrame.paddingTop = 2.0f;
self.graph.plotAreaFrame.paddingRight = 8.0f;
self.graph.plotAreaFrame.paddingBottom = 30.0f;
self.graph.plotAreaFrame.paddingLeft = 42.0f;
//self.graph.backgroundColor = [[UIColor greenColor] CGColor];
self.graph.paddingTop = 2.0f;
self.graph.paddingRight = 2.0f;
self.graph.paddingBottom = 2.0f;
self.graph.paddingLeft = 2.0f;
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
dataSourceLinePlot.identifier = @"Systolic";
dataSourceLinePlot.dataSource = self;
dataSourceLinePlot.delegate = self;
CPTColor *areaColor = [CPTColor colorWithComponentRed:249.0/255.0 green:183.0/255.0 blue:183.0/255.0 alpha:.7];
CPTFill *areaGradientFill = [CPTFill fillWithColor:areaColor];
[dataSourceLinePlot setAreaFill:areaGradientFill];
[dataSourceLinePlot setAreaBaseValue:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInt(60)]];//[NSNumber numberWithInt:CPTDecimalFromInt(0)]];
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
CPTColor *aaplColor = [CPTColor redColor];
lineStyle.lineColor = aaplColor;
CPTXYAxisSet axisSet = (CPTXYAxisSet ) newGraph.axisSet;
/*
// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
*/
// set the majorGridLinestyleProperty by this line as.
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.lineColor = [CPTColor grayColor];
gridLineStyle.lineWidth = 1.0f;
gridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2], nil];
gridLineStyle.patternPhase=0.0f;
dataSourceLinePlot.dataLineStyle = lineStyle;
axisSet.yAxis.majorGridLineStyle = gridLineStyle;
// axisSet.yAxis.axisLineStyle = axisLineStyle;
axisSet.xAxis.majorGridLineStyle = gridLineStyle;
// axisSet.xAxis.axisLineStyle = axisLineStyle;
dataSourceLinePlot.dataSource = self;
[newGraph addPlot:dataSourceLinePlot];
//-------------------------------------------------------------------------------------
CPTScatterPlot *dataSourceLinePlot2 = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
dataSourceLinePlot2.identifier = @"Diastolic";
dataSourceLinePlot2.dataSource = self;
dataSourceLinePlot2.delegate = self;
CPTColor *areaColor1 = [CPTColor colorWithComponentRed:229.0/255.0 green:87.0/255.0 blue:87.0/255.0 alpha:.7];
CPTFill *areaGradientFill1 = [CPTFill fillWithColor:areaColor1];
[dataSourceLinePlot2 setAreaFill:areaGradientFill1];
[dataSourceLinePlot2 setAreaBaseValue:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInt(60)]];
CPTMutableLineStyle *lineStyle1 = [dataSourceLinePlot.dataLineStyle mutableCopy];
CPTColor *aaplColor1 = [CPTColor redColor];
lineStyle1.lineColor = aaplColor1;
dataSourceLinePlot2.dataLineStyle = lineStyle1;
dataSourceLinePlot2.dataSource = self;
[newGraph addPlot:dataSourceLinePlot2];
}
CPTXYGraph *theGraph = self.graph;
self.graphHost.hostedGraph = theGraph;
CPTXYPlotSpace plotSpace = (CPTXYPlotSpace )theGraph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:NO];
NSTimeInterval oneDay = 24 60 60;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(xLow)]
length:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(oneDay*5.0f)]];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@50.0 length:@(120)];
// Axes
CPTXYAxisSet axisSet = (CPTXYAxisSet )theGraph.axisSet;
NSDate refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 10];
// added for date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM"];
// dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
NSLog(@"Ref date: %@", refDate);
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = [NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(oneDay)];
x.orthogonalPosition = @60.0;
x.minorTicksPerInterval = 1;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = @20;
y.majorTickLineStyle = nil;
y.minorTicksPerInterval = 4;
y.minorTickLineStyle = nil;
y.orthogonalPosition = @60.0;
y.tickDirection = CPTSignPositive;
[theGraph reloadData];
}
请让我知道如何做到这一点。
提前感谢。您可以使用
-[NSDateFormatter dateFromString:
将从服务器接收的日期字符串转换为NSDate
对象。看起来图表已设置为显示日期。感谢您的回复。我们得到的是图形,但我们在x轴上显示的是基于日差间隔(即24小时)的日期,但正如我已经解释过的相同日期有不同时间获得2个值的场景,如何处理这种情况?请查看回复日期以了解更多信息。您可以在散点图上重复x值或y值。如果要删除时间组件,请使用“-[NSCalendar components:fromDate:][code>将日期转换为NSDateComponents
,清除时间组件,然后检索新的日期。