- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有代码显示带有两个图的图表。我想要但没有找到该怎么做的是:在 x 轴上 DateTime 的位置我需要日期之间的正确比例间隔,而不是相同的。这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
DateTime = @[@"2013-10-05 08:47:52",@"2013-10-06 08:47:52",@"2013-10-07 08:47:52",@"2013-10-08 08:47:52",@"2013-10-09 08:47:52",@"2013-10-12 08:47:52",@"2013-10-13 08:47:52"];
temp1 = @[@"17.1",@"20",@"19",@"16",@"15",@"15",@"17"];
temp2 = @[@"13",@"11",@"13",@"10",@"11",@"12",@"13"];
[self initPlot];
}
-(void)initPlot
{
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
-(void)configureHost
{
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.hostView.allowPinchScaling = YES;
[self.view addSubview:self.hostView];
}
-(void)configureGraph
{
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]];
self.hostView.hostedGraph = graph;
// 2 - Set graph title
NSString *title = @"Testovací graf";
graph.title = title;
// 3 - Create and set text style
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 12.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
//graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
// 4 - Set padding for plot area
[graph.plotAreaFrame setPaddingLeft:30.0f];
[graph.plotAreaFrame setPaddingBottom:100.0f];
// 5 - Enable user interactions for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
}
-(void)configurePlots
{
// 1 - Get graph and plot space
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
// 2 - Create the plots
CPTScatterPlot *probe1Plot = [[CPTScatterPlot alloc] init];
probe1Plot.dataSource = self;
probe1Plot.identifier = @"Temp1";
CPTColor *probe1Color = [CPTColor redColor];
[graph addPlot:probe1Plot toPlotSpace:plotSpace];
CPTScatterPlot *probe2Plot = [[CPTScatterPlot alloc] init];
probe2Plot.dataSource = self;
probe2Plot.identifier = @"Temp2";
CPTColor *probe2Color = [CPTColor blueColor];
[graph addPlot:probe2Plot toPlotSpace:plotSpace];
// 3 - Set up plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:probe1Plot, probe2Plot, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.48f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(3.0f)];
plotSpace.yRange = yRange;
// 4 - Create styles and symbols
CPTMutableLineStyle *probe1LineStyle = [probe1Plot.dataLineStyle mutableCopy];
probe1LineStyle.lineWidth = 1.0 ;
probe1LineStyle.lineColor = probe1Color;
probe1Plot.dataLineStyle = probe1LineStyle;
CPTMutableLineStyle *probe1SymbolLineStyle = [CPTMutableLineStyle lineStyle];
probe1SymbolLineStyle.lineColor = probe1Color;
CPTPlotSymbol *probe1Symbol = [CPTPlotSymbol ellipsePlotSymbol];
probe1Symbol.fill = [CPTFill fillWithColor:probe1Color];
probe1Symbol.lineStyle = probe1SymbolLineStyle;
probe1Symbol.size = CGSizeMake(3.0f, 3.0f);
probe1Plot.plotSymbol = probe1Symbol;
CPTMutableLineStyle *probe2LineStyle = [probe2Plot.dataLineStyle mutableCopy];
probe2LineStyle.lineWidth = 1.0;
probe2LineStyle.lineColor = probe2Color;
probe2Plot.dataLineStyle = probe2LineStyle;
CPTMutableLineStyle *probe2SymbolLineStyle = [CPTMutableLineStyle lineStyle];
probe2SymbolLineStyle.lineColor = probe2Color;
CPTPlotSymbol *probe2Symbol = [CPTPlotSymbol diamondPlotSymbol];
probe2Symbol.fill = [CPTFill fillWithColor:probe2Color];
probe2Symbol.lineStyle = probe2SymbolLineStyle;
probe2Symbol.size = CGSizeMake(3.0f, 3.0f);
probe2Plot.plotSymbol = probe2Symbol;
}
-(void)configureAxes
{
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor blackColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 10.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 1.5f;
axisLineStyle.lineColor = [CPTColor blackColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor blackColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.lineColor = [CPTColor grayColor];
gridLineStyle.lineWidth = 0.5f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
x.title = @"DateTime";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 85.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickLabelDirection = CPTSignNegative;
CGFloat dateCount = [DateTime count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;
for (NSString *date in DateTime)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *str = [self localTime:date];
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:str textStyle:x.labelTextStyle];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label)
{
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
x.labelRotation = M_PI / 4;
// 4 - Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.title = @"Temperature";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = 30.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = -16.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 2.0f;
y.minorTickLength = 2.0f;
NSInteger majorIncrement = 10;
NSInteger minorIncrement = 1;
CGFloat yMax = 40.0f; // should determine dynamically based on max temp
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
for (NSInteger j = minorIncrement; j <= yMax; j+= minorIncrement)
{
NSInteger mod = j % majorIncrement;
if (mod == 0)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label)
{
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
}
else
{
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
CPTGraph *graph = self.hostView.hostedGraph;
graph.legend = [CPTLegend legendWithGraph:graph];
CPTMutableLineStyle *legendBorderlineStyle = [CPTMutableLineStyle lineStyle];
legendBorderlineStyle.lineColor = [CPTColor blackColor];
legendBorderlineStyle.lineWidth = 1.0f;
legendBorderlineStyle.lineColor = [CPTColor blackColor];
graph.legend.borderLineStyle = legendBorderlineStyle;
graph.legend.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
graph.legend.cornerRadius = 5.0;
graph.legend.swatchSize = CGSizeMake(10, 20);
graph.legendAnchor = CPTRectAnchorBottom;
graph.legend.textStyle = axisTextStyle;
graph.legendDisplacement = CGPointMake(150.40, 250.0);
}
- (NSString *) localTime:(NSString *)time //this is for recount TimeZone ofset
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//Special Locale for fixed dateStrings
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
//Assuming the dateString is in GMT+00:00
//formatter by default would be set to local timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:time];
//After forming the date set local time zone to formatter
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *newTimeZoneDateString = [formatter stringFromDate:date];
return newTimeZoneDateString;
}
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
// return [DateTime count];
if ([(NSString *)plot.identifier isEqualToString:@"Temp1"])
{
return temp1.count;
}
else if ([(NSString *)plot.identifier isEqualToString:@"Temp2"])
{
return temp2.count;
}
return 0;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
NSNumber *num = nil;
switch (fieldEnum) {
case CPTScatterPlotFieldX:
num = [NSNumber numberWithUnsignedInteger:idx];
break;
case CPTScatterPlotFieldY:
if ([(NSString *)plot.identifier isEqualToString:@"Temp1"])
{
num = [temp1 objectAtIndex:idx];
}
else if ([(NSString *)plot.identifier isEqualToString:@"Temp2"])
{
num = [temp2 objectAtIndex:idx];
}
break;
}
return num;
}
我想要 X 轴上日期时间项之间的比例间隔。如果有人帮助我,我会很幸运。谢谢。 :)
最佳答案
Eric 是专家,他的回答足够好。但我想提供更多细节,这对于像我这样的初学者来说是必要的,因为在将东西放在适当的位置时遇到很多麻烦。
首先,您需要设置适当的范围(我确实从 Eric 那里学到了很多东西)。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:25200]];
NSTimeInterval xLow = [[dateFormatter dateFromString:@"8:30"] timeIntervalSince1970];
NSTimeInterval xHigh = [[dateFormatter dateFromString:@"12:00"] timeIntervalSince1970];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow)
length:CPTDecimalFromDouble(xHigh-xLow)];
接下来,对于固定间隔(例如半小时),使用以下内容:
x.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
NSTimeInterval x1 = [[dateFormatter dateFromString:@"8:30"] timeIntervalSince1970];
NSTimeInterval x2 = [[dateFormatter dateFromString:@"9:30"] timeIntervalSince1970];
x.majorIntervalLength = CPTDecimalFromDouble(x2-x1);
//Label time Format:
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
x.labelFormatter = timeFormatter;
对于自定义标签,将上面两部分替换为以下内容:
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:[self.arrXValues count]];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[self.arrXValues count]];
for (NSString *string in self.arrXValues) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string textStyle:axisTextStyle];
NSDate *time = [dateFormatter dateFromString:string];
NSTimeInterval interval = [time timeIntervalSince1970];
label.tickLocation = CPTDecimalFromDouble(interval);
label.rotation = M_PI * 1/4;
label.offset = 0.0f;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSString stringWithFormat:@"%f", interval]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
最后,核心情节委托(delegate)方法,
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)indexPath
{
if(fieldEnum == CPTScatterPlotFieldX)
{
//return [NSNumber numberWithFloat:[[self.arrXValues objectAtIndex:indexPath] floatValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:25200]];
NSDate *time = [dateFormatter dateFromString:[self.arrXValues objectAtIndex:indexPath]];
NSTimeInterval interval = [time timeIntervalSince1970];
return [NSNumber numberWithDouble:interval];
}
else if(fieldEnum == CPTScatterPlotFieldY) {
return [NSNumber numberWithFloat:[[self.arrYValues objectAtIndex:indexPath] floatValue]];
}
return nil;
}
关于iOS CorePlot x 轴 DateTime 间隔成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23495402/
我正在尝试使用以下方法对 datetime.datetime 对象列表求和: from datetime import datetime, timedelta d= [datetime.datetim
我正在尝试这个 (datetime.datetime.today()-datetime.datetime.today()).days 给出 -1 并期待值 0 而不是我得到 -1。在这种情况下,我将结
如果我列一个时间增量的列表,平均值比我对这些增量的微秒值求平均时要大。为什么会这样呢?。赠送。这是Linux上的Python3.8.10。
考虑以下片段: import datetime print(datetime.datetime.now() - datetime.datetime.now()) 在 x86_64 Linux 下的 P
如何在 SQLAlchemy 查询中比较 DateTime 字段和 datetime.datetime 对象? 例如,如果我这样做 candidates = session.query(User).f
我收到以下错误: type object 'datetime.datetime' has no attribute 'datetime' 在下面一行: date = datetime.datetime
尝试找出如何将当前日期锁定为变量,以从输入的 self.birthday 中减去。我已经查看了各种示例和链接,但无济于事......建议? from datetime import datetime
您好,我有一些 datetime.datetime 格式的日期,我用它们来过滤带有 Pandas 时间戳的 Pandas 数据框。我刚刚尝试了以下方法并获得了 2 小时的偏移量: from datet
如果您调用 datetime.datetime.now(datetime.timezone.utc) 您会得到类似 datetime.datetime(2021, 9, 8, 1, 33, 19, 6
我正在使用 pywin32 读取/写入 Excel 文件。我在 Excel 中有一些日期,以 yyyy-mm-dd hh:mm:ss 格式存储。我想将它们作为 datetime.datetime 对象
据我所知,自 Unix 纪元(1970-01-01 00:00:00 UTC)以来的秒数在全局各地应该是相同的,因为它固定为 UTC。 现在,如果您所在的时区有几个小时 +/- UTC,为什么这样做会
我正在尝试添加 datetime.datetime 和 datetime.time 以获得一列。我正在尝试结合: import datetime as dt dt.datetime.combine(m
我有一个脚本需要在脚本的不同行执行以下操作: today_date = datetime.date.today() date_time = datetime.strp(date_time_string
我在 AppEngine 上收到 type object 'datetime.datetime' has no attribute 'datetime' 错误,提示日期时间类型,但我的导入是 impo
所以我一直在使用 python 语言制作东西。我遇到了一些不太容易理解的错误: TypeError: 'datetime.datetime' object is not subscriptable (
当我运行时 from datetime import date, time, timedelta date(2012, 11, 1) + timedelta(0, 3600) 结果是 datetime
我的目标是转换 utc进入loc : use chrono::{Local, UTC, TimeZone}; let utc = chrono::UTC::now(); let loc = chron
假设您有一个 datetime.date 对象,例如 datetime.date.today() 返回的对象。 稍后您还会得到一个表示时间的字符串,它补充了日期对象。 在 datetime.datet
我试过了 In [16]: import datetime In [17]: now = datetime.datetime.utcnow() In [18]: isinstance(now, dat
我有以下代码并且收到上述错误。由于我是 python 新手,因此无法理解此处的语法以及如何修复错误: if not start or date < start: start = date 最佳答案 有
我是一名优秀的程序员,十分优秀!