本文介绍了如何在iPhone上使用核心图在x轴上绘制一年中的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在x轴上使用核心图和y轴来绘制一年中的月份,我需要使用从excel表单获得的这些数据的200倍b $ b的倍数。任何人都可以帮助我解决这个问题。
提前致谢....

解决方案

使用CPTCalendarFormatter而不是CPTTimeFormatter,如其他答案中所述。 CPTTimeFormatter非常适用于需要以秒为单位设置时间间隔偏移(例如小时= 60 * 60,日= 24 * 60 * 60)的情况,但月数可变天数/秒(30 * 24 * 60 * 60)或31 * 24 * 60 * 60,28,闰年等)。 CPTCalendarFormatter允许您为标签上的日期计算确定日历单位。通过这种方式,修复了参考日期和参考日历单位,在数据源方法中,您将不得不返回从参考日期开始的那些单位数。



这里是完整的代码,享受。



在您的viewDidLoad或其他地方初始化图形:

  [...] 

//图形主机视图
CPTGraphHostingView * hostView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
[self.view addSubview:hostView];

//你的图
CPTGraph * graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
hostView.hostedGraph = graph;
CPTXYAxisSet * axisSet =(CPTXYAxisSet *)[graph axisSet];

// xAxis配置
CPTXYAxis * xAxis = [axisSet xAxis];
[xAxis setLabelingPolicy:CPTAxisLabelingPolicyFixedInterval];
//为当前语言环境准备dateFormat,我们希望2014年1月,2014年2月等等等等
NSString * dateComponents = @MMMYY;
NSString * localDateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale currentLocale]];
NSDateFormatter * labelDateFormatter = [[NSDateFormatter alloc] init];
labelDateFormatter.dateFormat = localDateFormat;
//设置xAxis日期格式化程序
CPTCalendarFormatter * xDateYearFormatter = [[CPTCalendarFormatter alloc] initWithDateFormatter:labelDateFormatter];
//记住这个引用日期,它将用于计算dataSource方法中的NSNumber
xDateYearFormatter.referenceDate = [NSDate dateWithTimeIntervalSince1970:0];
xDateYearFormatter.referenceCalendarUnit = NSMonthCalendarUnit;
[xAxis setLabelFormatter:xDateYearFormatter];

// y轴配置
CPTXYAxis * yAxis = [axisSet yAxis];
[yAxis setMajorIntervalLength:CPTDecimalFromFloat(_YOURYINTERVAL_)];
[yAxis setLabelingPolicy:CPTAxisLabelingPolicyFixedInterval];
[yAxis setLabelFormatter:_YOURYFORMATTER_];
[yAxis setAxisConstraints:[CPTConstraints constraintWithLowerOffset:0.0]]

//从图中获取(默认)图表空间,以便设置其x / y范围
CPTXYPlotSpace * plotSpace =(CPTXYPlotSpace *)graph.defaultPlotSpace;
NSDate * startDate = _YOURSTARTINGDATE_
//自引用日期以来的月数
NSInteger xStart = [[[NSCalendar currentCalendar]组件:NSCalendarUnitMonth
fromDate:xDateYearFormatter.referenceDate
toDate:_YOURSTARTINGDATE_
options:0] month];
[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(xStart)length:CPTDecimalFromInteger(_YOURDATESARRAY_.count-1)]];
[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)length:CPTDecimalFromFloat(_YOURMAXYVALUE_)]];

[...]

数据源方法返回轴上的值: (NSNumber *)numberForPlot :( CPTPlot *)绘图字段:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index $ b($)
switch(fieldEnum){

case CPTScatterPlotFieldX:{

NSInteger monthsOffset = [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:[NSDate dateWithTimeIntervalSince1970: 0] toDate:[_YOURDATESARRAY_ objectAtIndex:index] options:0] month];
NSNumber * val = [NSNumber numberWithInteger:monthsOffset];
返回val;
休息;
}
case CPTScatterPlotFieldY:{
NSNumber * val = _YOURVALUEFORYAXIS_;
返回val;
休息;
}
默认值:
break;
}

return nil;
}


how to plot months of the year on x-axis using core plot and on y-axis i need multiples of 200morever this data i used to get from an excel sheet.can anyone help me out from this.Thanks in advance....

解决方案

Use CPTCalendarFormatter instead of CPTTimeFormatter as stated in the other answers. CPTTimeFormatter is good when you have to set time interval offset in seconds (ex. hour= 60*60, day=24*60*60), but months have a variable number of days/seconds (30*24*60*60 or 31*24*60*60, 28, leap years ecc.). CPTCalendarFormatter let you decide the calendar unit for date calculations on labels. In this way, fixed a reference date and a reference calendar unit, in the datasource method you will have to return the number of those units starting from the reference date.

Here's full code, enjoy.

In your viewDidLoad or elsewhere you initialize the graph:

[...]

// Graph host view
CPTGraphHostingView* hostView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
[self.view addSubview: hostView];

// Your graph
CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
hostView.hostedGraph = graph;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)[graph axisSet];    

// xAxis configuration
CPTXYAxis *xAxis = [axisSet xAxis];
[xAxis setLabelingPolicy:CPTAxisLabelingPolicyFixedInterval];
// Prepare dateFormat for current Locale, we want "JAN 2014" "FEB 2014" and so on
NSString *dateComponents = @"MMMYY";
NSString *localDateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *labelDateFormatter=[[NSDateFormatter alloc] init];
labelDateFormatter.dateFormat=localDateFormat;    
// Set xAxis date Formatter
CPTCalendarFormatter *xDateYearFormatter = [[CPTCalendarFormatter alloc] initWithDateFormatter:labelDateFormatter];
//Keep in mind this reference date, it will be used to calculate NSNumber in dataSource method
xDateYearFormatter.referenceDate = [NSDate dateWithTimeIntervalSince1970:0];
xDateYearFormatter.referenceCalendarUnit=NSMonthCalendarUnit;    
[xAxis setLabelFormatter:xDateYearFormatter];

// yAxis configuration
CPTXYAxis *yAxis = [axisSet yAxis];
[yAxis setMajorIntervalLength:CPTDecimalFromFloat(_YOURYINTERVAL_)];
[yAxis setLabelingPolicy:CPTAxisLabelingPolicyFixedInterval];
[yAxis setLabelFormatter:_YOURYFORMATTER_];
[yAxis setAxisConstraints:[CPTConstraints constraintWithLowerOffset:0.0]]

// Get the (default) plotspace from the graph so we can set its x/y ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
NSDate *startDate=_YOURSTARTINGDATE_
//Number of months since the reference date
NSInteger xStart=[[[NSCalendar currentCalendar] components: NSCalendarUnitMonth
                                 fromDate: xDateYearFormatter.referenceDate
                                   toDate: _YOURSTARTINGDATE_
                                  options: 0] month];    
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(xStart)     length:CPTDecimalFromInteger(_YOURDATESARRAY_.count-1)]];
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(_YOURMAXYVALUE_)]];

[...]

Datasource method to return values on axis:

 -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    switch (fieldEnum) {

        case CPTScatterPlotFieldX: {

            NSInteger monthsOffset=[[[NSCalendar currentCalendar] components: NSCalendarUnitMonth  fromDate: [NSDate dateWithTimeIntervalSince1970:0] toDate: [_YOURDATESARRAY_ objectAtIndex:index] options: 0] month];                
            NSNumber *val = [NSNumber numberWithInteger:monthsOffset];    
            return val;
            break;
        }
        case CPTScatterPlotFieldY: {
            NSNumber *val=_YOURVALUEFORYAXIS_;
            return val;
            break;
        }
        default:
            break;
    }

    return nil;
}

这篇关于如何在iPhone上使用核心图在x轴上绘制一年中的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:44