本文整理汇总了TypeScript中d3.bisector函数的典型用法代码示例。如果您正苦于以下问题:TypeScript bisector函数的具体用法?TypeScript bisector怎么用?TypeScript bisector使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bisector函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: init
init() {
const {target, width, height, margin} = this;
// const {xTicks, yTicks, yRange} = this;
const {xTicks, yRange} = this;
const [w, h] = this.dimensions();
this.chart = d3.select(target)
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
this.x = d3.time.scale()
.range([0, w]);
this.y = d3.scale.linear()
.range(yRange);
this.xAxis = d3.svg.axis()
.orient('bottom')
.scale(this.x)
.ticks(xTicks)
.tickPadding(8)
.tickSize(-h);
this.chart.append('g')
.attr('class', 'x axis')
.attr('transform', `translate(0, ${h})`)
.call(this.xAxis);
his.details = this.chart.append('g')
.attr('class', 'details')
.style('display', 'none')
this.details.append('line')
.attr('class', 'x')
.attr('y1', 0)
.attr('y1', h)
this.details.append('text')
.attr('class', 'time')
this.details.append('g')
.attr('class', 'data')
this.overlay = this.chart.append('rect')
.attr('class', 'overlay')
.attr('width', w)
.attr('height', h)
.style('fill-opacity', 0)
.on('mousemove', _ => this.onMouseOver())
.on('mouseleave', _ => this.onMouseLeave());
this.xBisect = d3.bisector(d => d.time).left;
}
示例2: getClosestItemIndexes2
export function getClosestItemIndexes2(array: Array<number>, value: number, accessor: any): { left: number, right: number } {
let left: number = d3.bisector(accessor).left(array, value);
left = Math.max(left - 1, 0);
let right: number = Math.min(left + 1, array.length - 1);
let item: number = accessor(array[left]);
if (item >= value && item <= value) {
right = left;
}
return { left, right };
}
示例3: financeEODScale
export default function financeEODScale(indexAccessor = d => d.idx, dateAccessor = d => d.date, data = [0, 1], backingLinearScale = d3.scale.linear()) {
var timeScaleSteps = [
{ step: 864e5, f: function(d) { return isDefined(dateAccessor(d)) && true; } }, // 1-day
{ step: 1728e5, f: function(d, i) { return isDefined(dateAccessor(d)) && (i % 2 === 0); } }, // 2-day
{ step: 8380e5, f: function(d, i, arr) {
if (d.startOfMonth) return true;
var list = [];
if ((i - 2) >= 0) list.push(arr[i - 2]);
if ((i - 1) >= 0) list.push(arr[i - 1]);
list.push(arr[i]);
if ((i + 1) <= arr.length - 1) list.push(arr[i + 1]);
if ((i + 2) <= arr.length - 1) list.push(arr[i + 2]);
var sm = list
.map(function(each) { return each.startOfMonth; })
.reduce(function(prev, curr) {
return prev || curr;
});
return sm ? false : d.startOfWeek;
} }, // 1-week
{ step: 3525e6, f: function(d) {return d.startOfMonth; } }, // 1-month
{ step: 7776e6, f: function(d) {return d.startOfQuarter; } }, // 3-month
{ step: 31536e6, f: function(d) {return d.startOfYear; } }, // 1-year
{ step: 91536e15, f: function(d) {return isDefined(dateAccessor(d)) && (d.startOfYear && dateAccessor(d).getFullYear() % 2 === 0); } } // 2-year
];
var timeScaleStepsBisector = d3.bisector(function(d) { return d.step; }).left;
var bisectByIndex = d3.bisector(function(d) { return indexAccessor(d); }).left;
var tickFormat = [
[d3.time.format("%Y"), function(d) { return d.startOfYear; }],
[d3.time.format("%b %Y"), function(d) { return d.startOfQuarter; }],
[d3.time.format("%b"), function(d) { return d.startOfMonth; }],
[d3.time.format("%d %b"), function(d) { return d.startOfWeek; }],
[d3.time.format("%a %d "), function(/* d */) { return true; }]
];
function formater(d) {
var i = 0, format = tickFormat[i];
while (!format[1](d)) format = tickFormat[++i];
var tickDisplay = format[0](dateAccessor(d));
// console.log(t;ickDisplay);
return tickDisplay;
}
function scale(x) {
return backingLinearScale(x);
}
scale.isPolyLinear = function() {
return true;
};
scale.invert = function(x) {
return backingLinearScale.invert(x);
};
scale.data = function(x) {
if (!arguments.length) {
return data;
} else {
data = x;
// this.domain([data.first().index, data.last().index]);
this.domain([indexAccessor(data[0]), indexAccessor(data[data.length - 1])]);
return scale;
}
};
scale.indexAccessor = function(x) {
if (!arguments.length) return indexAccessor;
indexAccessor = x;
return scale;
};
scale.dateAccessor = function(x) {
if (!arguments.length) return dateAccessor;
dateAccessor = x;
return scale;
};
scale.domain = function(x) {
if (!arguments.length) return backingLinearScale.domain();
// console.log("before = %s, after = %s", JSON.stringify(backingLinearScale.domain()), JSON.stringify(x))
var d = [x[0], x[1]];
// console.log(d);
backingLinearScale.domain(d);
return scale;
};
scale.range = function(x) {
if (!arguments.length) return backingLinearScale.range();
backingLinearScale.range(x);
return scale;
};
scale.rangeRound = function(x) {
return backingLinearScale.range(x);
};
scale.clamp = function(x) {
if (!arguments.length) return backingLinearScale.clamp();
backingLinearScale.clamp(x);
return scale;
};
scale.interpolate = function(x) {
if (!arguments.length) return backingLinearScale.interpolate();
backingLinearScale.interpolate(x);
return scale;
};
scale.ticks = function(m) {
var start, end, count = 0;
//.........这里部分代码省略.........
示例4: Error
{ target: 55e4, level: 4 },
{ target: 11e5, level: 5 },
{ target: 21e5, level: 6 },
{ target: 43e5, level: 7 },
{ target: 49e5, level: 8 }, // not tested
{ target: 50e5, level: 9 }, // not tested
{ target: 58e6, level: 10 }, // not tested
{ target: 58e6, level: 11 },
{ target: 11e7, level: 12 },
{ target: 78e7, level: 13 },
{ target: 16e8, level: 14 },
{ target: 62e8, level: 15 },
{ target: 10e20, level: 16 },
];
var tickLevelBisector = d3.bisector(function(d) { return d.target; }).left;
export default function financeDiscontinuousScale(index,
interval,
backingLinearScale = d3.scale.linear()) {
if (isNotDefined(index) || isNotDefined(interval))
throw new Error("Use the discontinuousTimeScaleProvider to create financeDiscontinuousScale");
function scale(x) {
return backingLinearScale(x);
}
scale.invert = function(x) {
var inverted = backingLinearScale.invert(x);
return Math.round(inverted * 10000) / 10000;
};
示例5: financeIntradayScale
export default function financeIntradayScale(indexAccessor = d => d.idx, dateAccessor = d => d.date, data = [0, 1], backingLinearScale = d3.scale.linear()) {
var timeScaleSteps = [
{ step: 40000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfMinute); } }, // 1 min
{ step: 192000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOf5Minutes); } }, // 5 min
{ step: 400000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfQuarterHour); } },
{ step: 6684000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHalfHour); } },
{ step: 108e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHour); } },
{ step: 216e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfEighthDay); } },
{ step: 432e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfQuarterDay); } }, // 12 hours
{ step: 864e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHalfDay); } }, // 1-day
{ step: 2592e5, f: function(d) { return isDefined(dateAccessor(d)) && d.startOfDay; } }, // 3-day, doesnt work with 2h scale
{ step: 6048e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay && dateAccessor(d).getDate() % 3 === 0); } }, // 7-day
{ step: 12096e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay && dateAccessor(d).getDate() % 3 === 0); } } // 14-day
];
var timeScaleStepsBisector = d3.bisector(function(d) { return d.step; }).left;
var bisectByIndex = d3.bisector(function(d) { return indexAccessor(d); }).left;
var tickFormat = [
[d3.time.format.utc("%a %d"), function(d) { return d.startOfDay; }],
[d3.time.format.utc("%_I %p"), function(d) { return d.startOfHour && !d.startOfDay; }],
[d3.time.format.utc("%I:%M %p"), d3.functor(true)] // accumulator fallback for first entry
];
function formater(d) {
var i = 0, format = tickFormat[i];
while (!format[1](d)) format = tickFormat[++i];
var tickDisplay = format[0](dateAccessor(d));
// console.log(t;ickDisplay);
return tickDisplay;
}
function scale(x) {
return backingLinearScale(x);
}
scale.isPolyLinear = function() {
return true;
};
scale.invert = function(x) {
return backingLinearScale.invert(x);
};
scale.data = function(x) {
if (!arguments.length) {
return data;
} else {
data = x;
// this.domain([data.first().index, data.last().index]);
this.domain([indexAccessor(data[0]), indexAccessor(data[data.length - 1])]);
return scale;
}
};
scale.indexAccessor = function(x) {
if (!arguments.length) return indexAccessor;
indexAccessor = x;
return scale;
};
scale.dateAccessor = function(x) {
if (!arguments.length) return dateAccessor;
dateAccessor = x;
return scale;
};
scale.domain = function(x) {
if (!arguments.length) return backingLinearScale.domain();
// console.log("before = %s, after = %s", JSON.stringify(backingLinearScale.domain()), JSON.stringify(x))
var d = [x[0], x[1]];
// console.log(d);
backingLinearScale.domain(d);
return scale;
};
scale.range = function(x) {
if (!arguments.length) return backingLinearScale.range();
backingLinearScale.range(x);
return scale;
};
scale.rangeRound = function(x) {
return backingLinearScale.range(x);
};
scale.clamp = function(x) {
if (!arguments.length) return backingLinearScale.clamp();
backingLinearScale.clamp(x);
return scale;
};
scale.interpolate = function(x) {
if (!arguments.length) return backingLinearScale.interpolate();
backingLinearScale.interpolate(x);
return scale;
};
scale.ticks = function(m) {
/* var start, end, count = 0;
data.forEach(function(d) {
if (isDefined(dateAccessor(d))) {
if (isNotDefined(start)) start = d;
end = d;
count++;
}
});
console.log(count / data.length);
m = (count / data.length) * m;
*/
var start = head(data);
//.........这里部分代码省略.........