我已经设置了一个带有核心图的图表,该图表在存在要显示的新数据时显示动画。我按照CorePlot的示例代码显示了图中的新动画数据。
情况就是这样:
现在Y轴在每个新值处移动。没有"锚定"在中间。
这是我对图表的初始设置:
- (void)configureHost {
self.hostViewTop = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/3, 2*self.view.frame.size.width/5, self.view.frame.size.height/1.5)];
self.hostViewTop.collapsesLayers = NO;
[self.hostViewTop setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.hostViewTop setAutoresizesSubviews:YES];
self.hostViewTop.allowPinchScaling = NO;
[self.view addSubview:self.hostViewTop];
}
- (void) configurePlots{
CPTGraph *graph = self.hostViewTop.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
// 1 - Create the three plots
...
// 2 - Set up plot space
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(MAX_DATA_ON_CHART)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(MAX_VALUE)];
// 4 - Create styles and symbols
...
}
- (void)configureAxes {
// 1 - Create styles
...
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostViewTop.hostedGraph.axisSet;
// X axis
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.minorGridLineStyle = minorGridLineStyle;
x.majorGridLineStyle = majorGridLineStyle;
x.minorTickLineStyle = minorTickLineStyle;
x.majorTickLineStyle = majorTickLineStyle;
x.minorTicksPerInterval = 9;
x.axisLineStyle = axisLineStyle;
x.labelTextStyle = clearTextStyle;
x.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
x.coordinate = CPTCoordinateX;
x.tickDirection = CPTSignNegative;
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.numberStyle = NSNumberFormatterNoStyle;
x.labelFormatter = labelFormatter;
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
// Y Axis at the beginning is at MAX_VALUE/2
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(MAX_VALUE/2);
y.minorGridLineStyle = minorGridLineStyle;
y.majorGridLineStyle = majorGridLineStyle;
y.minorTickLineStyle = minorTickLineStyle;
y.majorTickLineStyle = majorTickLineStyle;
y.majorIntervalLength = CPTDecimalFromInt(10);
y.minorTicksPerInterval = 3;
y.labelAlignment = CPTAlignmentRight;
y.axisLineStyle = axisLineStyle;
y.labelTextStyle = clearTextStyle;
y.labelFormatter = labelFormatter;
y.coordinate = CPTCoordinateY;
}
当有新数据可用时:
- (void)newData:(int)data {
CPTGraph *graphTop = self.hostViewTop.hostedGraph;
CPTPlot *plotTop = [graphTop plotWithIdentifier:@"ID"];
if (plotTop) {
if (dataArray.count > MAX_DATA) {
NSRange range;
range.location = 0;
range.length = dataArray.count-MAX_DATA;
[dataArray removeObjectsInRange:range];
[plotTop deleteDataInIndexRange:range];
}
CPTXYPlotSpace *plotSpaceTop = (CPTXYPlotSpace *)graphTop.defaultPlotSpace;
NSUInteger location = (currentIndex > MAX_DATA ? currentIndex - MAX_DATA + 1 : 0);
CPTPlotRange *newYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location)
length:CPTDecimalFromUnsignedInteger(MAX_DATA+MAX_DATA/4)];
[plotSpaceTop setYRange:newYRange];
currentIndex++;
// minValue, maxVlue, avgValue calculated from al data visible in chart
minValue = ...
maxValue = ...
avgValue = ...
...
double delta;
if (fabs(avgValue-minValue) > fabs(maxValue-avgValue)) {
delta = fabs(avgValue-minValue);
} else if (fabs(maxValue-avgValue)) {
delta = fabs(maxValue-avgValue);
}
CPTPlotRange *newXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(avgValue-delta) length:CPTDecimalFromDouble(avgValue+delta)];
[plotSpaceTop setXRange:newXRange];
[((CPTXYAxisSet *) graphTop.axisSet).yAxis setOrthogonalCoordinateDecimal:CPTDecimalFromDouble(avgValue)];
...
[plotTop insertDataAtIndex:newData.count - 1 numberOfRecords:1];
...
}
}
提前感谢您的帮助。
答案 0 :(得分:1)
新length
的{{1}}应为xRange
,以将平均值放在中心位置。
delta * 2