ios - 核心绘图实时图
<p><p>我正在使用核心图来制作实时图表。
这些值来自 web 服务,但只提供了 45 个值,然后它必须停止。
我得到了core plot 1.4的实时图样本,它几乎可以工作了。</p>
<p>我有两个问题,我的图表固定在 0,0 并且我的 y 轴消失了。
该图在值数组(X 轴)处正确输入数据。
日期数组(Y 轴)与值数组不同。
另外,在此之前,我得到了没有动画的图表。</p>
<p>到目前为止,这是我的代码,我相信所需的一切都在那里:</p>
<pre class="lang-c prettyprint-override"><code>#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return ;
}
-(void)newData:(NSTimer *)theTimer
{
// CPTGraph *theGraph = ;
CPTPlot *thePlot = ;
if ( thePlot ) {
if ( plotData.count >= kMaxDataPoints ) {
;
;
;
return;
}
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.hostView.hostedGraph.defaultPlotSpace;
NSUInteger location = (currentIndex >= kMaxDataPoints ? currentIndex - kMaxDataPoints + 2 : 0);
CPTPlotRange *newRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location)
length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 2)];
[CPTAnimation animate:plotSpace
property:@"xRange"
fromPlotRange:plotSpace.xRange
toPlotRange:newRange
duration:(1.0 / kFrameRate)];
currentIndex++;
NSLog(@"%@",plotData);
];
-1];
;
}
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSInteger valueCount = [count];
//switch (fieldEnum) {
// case CPTScatterPlotFieldX:
// if (index < valueCount) {
// return ;
// }
// break;
//
// case CPTScatterPlotFieldY:
//
// if ( == YES) {
// return [[ monthlyPrices:CPDTickerSymbolAAPL] objectAtIndex:index];
// } else if ( == YES) {
// return [[ monthlyPrices:CPDTickerSymbolGOOG] objectAtIndex:index];
// } else if ( == YES) {
// return [[ monthlyPrices:CPDTickerSymbolMSFT] objectAtIndex:index];
// }
// break;
//}
NSNumber *num = nil;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
num = ;
break;
case CPTScatterPlotFieldY:
num = ;
break;
default:
break;
}
return num;
return ;
}
-(void)configureAxes {
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = ;
axisTitleStyle.color = ;
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 16.0f;
CPTMutableTextStyle *axisTextStyle = [ init];
axisTextStyle.fontName = @"Helvetica";
axisTextStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = ;
axisLineStyle.lineWidth = 0.5f;
axisLineStyle.lineColor = [ colorWithAlphaComponent:0.4];
CPTMutableLineStyle *tickLineStyle = ;
tickLineStyle.lineColor = ;
tickLineStyle.lineWidth = 0.0f;
CPTMutableLineStyle *majorGridLineStyle = ;
majorGridLineStyle.lineWidth = 1.0;
majorGridLineStyle.lineColor = [ colorWithAlphaComponent:0.4];
CPTMutableLineStyle *minorGridLineStyle = ;
minorGridLineStyle.lineColor = ;
minorGridLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
//x.title = @"Day of Month";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 15.0f;
x.axisLineStyle = axisLineStyle;
// x.majorGridLineStyle = majorGridLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 30.0f;
x.tickDirection = CPTSignNegative;
//x.labelOffset = -25;
CGFloat dateCount = 1;
NSMutableSet *xLabels = ;
NSMutableSet *xLocations = ;
NSInteger i = 0;
// NSLog(@"datesInMonth - >%@",);
NSArray *_weekDays = @[@"", @"D", @"S", @"T", @"Q", @"Q", @"S", @"S"];
int z = 0;
for (NSString *date in ) {
int contador = i++;
NSDateFormatter *df = [ init];
;
NSString *dateTimeString = ;
const char* dateString = ;
struct tm tm;
time_t timestamp;
strptime(dateString, "%d/%m/%Y %H:%M:%S", &tm);
timestamp = mktime(&tm);
NSDate *myDate = ;
NSDateComponents *components = [ components:NSCalendarUnitDay |
NSCalendarUnitMonth |
NSCalendarUnitYear fromDate:myDate];
NSCalendar *calendar = ;
NSRange weekdayRange = ;
NSDateComponents *_components = ;
NSUInteger weekdayOfDate = ;
if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
axisTextStyle.color = ;
CPTPlotRange *bandRange;
bandRange = ;
CPTColor *areaColor = ;
CPTColor *areaColorEnd = ;
CPTGradient *areaGradient = ;
areaGradient.angle = -90.0f;
CPTFill *areaGradientFill = ;
areaGradient.angle = -90.0f;
areaGradientFill = ;
];
} else {
axisTextStyle.color = ;
}
axisTextStyle.textAlignment = CPTAlignmentCenter;
x.labelTextStyle = axisTextStyle;
CPTAxisLabel *label = [
initWithText:, _weekDays]
textStyle:x.labelTextStyle];
label.alignment = CPTAlignmentRight;
NSDecimal location = CPTDecimalFromInteger(contador);
label.tickLocation = CPTDecimalFromCGFloat(contador);
//label.offset = x.majorTickLength - 10;
if (label){
;
}
];
z++;
}
axisTextStyle.color = ;
axisTextStyle.textAlignment = CPTAlignmentCenter;
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
CPTXYAxis *y = axisSet.yAxis;
//y.title = @"";
y.titleTextStyle = axisTitleStyle;
//y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.majorTickLineStyle = axisLineStyle;
//y.minorTickLineStyle = nil;
y.majorGridLineStyle = majorGridLineStyle;
//y.minorGridLineStyle = minorGridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 36.0f;
//y.majorTickLineStyle = axisLineStyle;
//y.majorTickLength = 4.0f;
//y.minorTickLength = 1.0f;
y.gridLinesRange = ;
y.tickDirection = CPTSignPositive;
NSInteger majorIncrement = 10000000;
NSInteger minorIncrement = 10000000;
CGFloat yMax = 180000000;// should determine dynamically based on max price
NSMutableSet *yLabels = ;
//NSMutableSet *yMajorLocations = ;
//NSMutableSet *yMinorLocations = ;
NSMutableSet *yLocations = ;
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
NSNumberFormatter *numFormatter = [ init];
;
if (mod == 0) {
CPTAxisLabel *label = [ initWithText:] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset - 60;
if (label) {
;
}
];
}
}
y.axisLabels = yLabels;
//y.majorTickLocations = yMajorLocations;
//y.minorTickLocations = yMinorLocations;
y.majorTickLocations = yLocations;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该使用约束来定位 y 轴,这样它就不会随着图形的滚动而移动。</p>
<pre><code>y.axisConstraints = ;
</code></pre>
<p><code>kMaxDataPoints</code> 的值是多少?如果它大于数据点的数量,则图表将不会滚动。</p></p>
<p style="font-size: 20px;">关于ios - 核心绘图实时图,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21528833/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21528833/
</a>
</p>
页:
[1]