您好我正在使用核心图进行散点图绘制。我正在针对日期时间绘制数据(最多3位小数)。关于yaxis的数据和xaxis上的日期时间。对于某些数据值,yaxis Custom tick标签不可见。
以下是代码
CPTXYGraph *scatterGraph = [[CPTXYGraph alloc] initWithFrame:graphView.bounds];
graphView.hostedGraph = scatterGraph;
NSMutableArray *plotdataArray = [[NSMutableArray alloc] init];
NSMutableArray *timedataArray = [[NSMutableArray alloc] init];
plotdataArray = arrPlotData;
timedataArray = arrTimeData;
//create data plot
self.dataSourceLinePlot = [[CPTScatterPlot alloc]init];
self.dataSourceLinePlot.identifier = NSLocalizedString(@"Glucose Trend Chart", nil);
self.dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDecimal;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) scatterGraph.defaultPlotSpace;
//assign data to chartdata class
CGM_GlucoseChartData *chartDataObj = [[CGM_GlucoseChartData alloc] initWithView:self.dataSourceLinePlot];
chartDataObj.parent = self;
self.dataSourceLinePlot.dataSource = chartDataObj;
chartDataObj.dataForPlot = arrPlotData;
chartDataObj.arrTimeData = timedataArray;
[scatterGraph addPlot:self.dataSourceLinePlot];
scatterGraph.plotAreaFrame.frame = scatterGraph.bounds;
//add padding to graphview
scatterGraph.plotAreaFrame.paddingTop = 0;
scatterGraph.plotAreaFrame.paddingRight = 0;
scatterGraph.plotAreaFrame.paddingBottom = 90;
scatterGraph.plotAreaFrame.paddingLeft = 66;
scatterGraph.plotAreaFrame.masksToBorder = YES;
//add linestyle to graph
CPTMutableLineStyle *lineStyle = [self.dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor blueColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
plotSymbol.size = CGSizeMake(1.0f, 1.0f);
self.dataSourceLinePlot.dataLineStyle = lineStyle;
//calculate min and max values from array
float yMax = [[arrPlotData valueForKeyPath:@"@max.self"] floatValue];
float yMin = [[arrPlotData valueForKeyPath:@"@min.self"] floatValue];
NSUInteger xMax = timedataArray.count;
//create axis set for graph
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)scatterGraph.axisSet;
// Configure Line-Style //
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineWidth = 1.0f;
tickLineStyle.lineColor = [CPTColor blackColor];
axisSet.yAxis.majorTickLineStyle = tickLineStyle;
axisSet.xAxis.majorTickLineStyle = tickLineStyle;
// y-axis labels
CPTXYAxis *y = axisSet.yAxis;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0.0);
y.labelingPolicy = CPTAxisLabelingPolicyNone;
//set axis title
NSString *temp = NSLocalizedString(@"Glucose", nil);
y.title = [NSString stringWithFormat:@"%@ (%@)",temp,unitType];
y.titleOffset = 52;
// x-axis labels
CPTXYAxis *x = axisSet.xAxis;
//set axis title
x.title = NSLocalizedString(@"Time (Hour)", nil);
x.titleOffset = 75;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0.0);
x.labelingPolicy = CPTAxisLabelingPolicyNone;
if(plotdataArray.count == 0) {
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(1)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(1)];
}
else{
float centerLocation = (yMax+yMin) / 2;
float ytemp1 = (yMin +centerLocation) / 2;
float ytemp2 = (yMax+centerLocation) / 2;
//if differnce between min and max values is 0.1
if((yMax - yMin) == 0.001) {
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",yMax],nil];
tempVal = yMax/4;
}
else{
if(ytemp1 == centerLocation || ytemp2 == centerLocation) {
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",centerLocation],
[NSString stringWithFormat:@"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",centerLocation],
[NSString stringWithFormat:@"%.3f",yMax],nil];
tempVal = yMax/4;;
}
else{
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",ytemp1],
[NSString stringWithFormat:@"%.3f",centerLocation],
[NSString stringWithFormat:@"%.3f",ytemp2],
[NSString stringWithFormat:@"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.3f",yMin],
[NSString stringWithFormat:@"%.3f",ytemp1],
[NSString stringWithFormat:@"%.3f",centerLocation],
[NSString stringWithFormat:@"%.3f",ytemp2],
[NSString stringWithFormat:@"%.3f",yMax],nil];
tempVal = (yMax-ytemp2)/2;
}
}
}
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [yAxisLabels objectAtIndex:labelLocation++] textStyle:y.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = y.labelOffset + y.majorTickLength;
[customLabels addObject:newLabel];
}
y.majorTickLocations = [NSSet setWithArray:customTickLocations];
y.axisLabels = [NSSet setWithArray:customLabels];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd-MM (HH:mm)";
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+0:00"]];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:timedataArray.count];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:timedataArray.count];
NSUInteger maximumValue = 0;
if(timedataArray.count > 5 && timedataArray.count < 15) {
maximumValue = xMax - 1;
}
else {
maximumValue = xMax - (timedataArray.count/15);
}
if(timedataArray.count <= 5) {
for (NSInteger j = 0; j < timedataArray.count; j ++) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[dateFormatter stringFromDate:timedataArray[j]] textStyle:x.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
if (label)
{
[xLabels addObject:label];
label.offset = 5.0f;
[xLocations addObject:[dateFormatter stringFromDate:timedataArray[j]]];
}
}
x.majorTickLocations = xLocations;
x.axisLabels = xLabels;
}
else {
//if(timedataArray.count < 15) {
NSInteger centerLocation = timedataArray.count / 2;
NSInteger temp1 = centerLocation / 2;
NSInteger temp2 = (timedataArray.count-centerLocation) / 2;
NSArray *customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%.1f",0.0],
[NSString stringWithFormat:@"%.1ld",(long)temp1],
[NSString stringWithFormat:@"%.1ld",(long)centerLocation],
[NSString stringWithFormat:@"%.1ld",(long)centerLocation + temp2],
[NSString stringWithFormat:@"%.1lu",(unsigned long)maximumValue],nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:timedataArray[0]]],
[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:timedataArray[temp1]]],
[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:timedataArray[centerLocation]]],
[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:timedataArray[centerLocation + temp2]]],
[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:timedataArray[maximumValue]]],nil];
x.majorTickLocations = [NSSet setWithArray:customTickLocations];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
[customLabels addObject:newLabel];
}
x.axisLabels = [NSSet setWithArray:customLabels];
}
//rotate the x axis label
x.labelRotation = M_PI/3;
//create plot space and assign range for graph
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(xMax)];
if(timedataArray.count == 1 || yMax == yMin) {
if(yMax == 0) {
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)
length:CPTDecimalFromFloat(1.0)];
}
else{
if(tempVal == 0) {
tempVal = yMax/4;
}
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)
length:CPTDecimalFromString([NSString stringWithFormat:@"%.2f",yMax+tempVal])];
}
}
else{
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString([NSString stringWithFormat:@"%.2f",yMin])
length:CPTDecimalFromString([NSString stringWithFormat:@"%.2f",(yMax-yMin)+tempVal])];
}
}
[self.dataSourceLinePlot reloadData];
请向我的代码提出一些问题