小编jon*_*jon的帖子

d3.bisector 在降序数组上

我正在创建一个简单的 D3 折线图,但无法使用该d3.bisector()函数创建工具提示。我正在寻找它为每个值转到 Y 和 X 轴。平分线函数可以很好地为我提供与鼠标坐标相对应的 xData 值,但是对于 yData,我一直得到一些非常奇怪的结果。我怀疑问题在于 yData 数组的降序性质。有没有办法调整bisector()函数来处理降序数组?我正在寻找一种适用于任何数据集的解决方案。

//define the domain and range for the chart
var xScale =  d3.scaleLinear().domain([0,10]).range([0, width]);
var yScale =  d3.scaleLinear().domain([0,10]).range([height,0]);

//data for the line
var xData = [0,1,2,3,4,5,6,7,8,9,10];
var yData = [10,9,8,7,6,5,4,3,2,1,0];

//set up the  bisector function
var bisectData= d3.bisector(function(d){return d}).left;

 // get the x and y position of the cursor and put it into the Xscale/yScale function to get the correct X and Y values from …
Run Code Online (Sandbox Code Playgroud)

javascript d3.js

3
推荐指数
1
解决办法
312
查看次数

使用两个NSDates计算一个人的年龄

我代表一个人的班级有一种计算该人年龄的方法:

@interface Student : NSObject

@property (strong) NSDate *dob;

// This method will work out the person's age in calendar years
- (NSDateComponents*)ageCalculation:(NSDate*)dob;
Run Code Online (Sandbox Code Playgroud)

这是类实现文件

@implementation Student

- (NSDateComponents*)ageCalculation:(NSDate*)dob {
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents *components = [calendar components:units 
                                               fromDate:dob 
                                                 toDate:now   
                                                options:0];
    return components;
}

@end
Run Code Online (Sandbox Code Playgroud)

我不确定我是否正确行事,但是:

Student *student1 = [[Student alloc] init];
[student1 setDob:aDateOfBirth];
NSDateComponents *ageComponents = [student1 ageCalculation:aDateOfBirth];
Run Code Online (Sandbox Code Playgroud)

我应该怎么做这个方法的结果?我怎样才能ageCalculation:使用我已设定的日期?

time cocoa objective-c nsdate

1
推荐指数
1
解决办法
2684
查看次数

标签 统计

cocoa ×1

d3.js ×1

javascript ×1

nsdate ×1

objective-c ×1

time ×1