本文整理匯總了TypeScript中d3-scale.scaleBand函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript scaleBand函數的具體用法?TypeScript scaleBand怎麽用?TypeScript scaleBand使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了scaleBand函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Set
const applyGrouping = (seriesList: SeriesList, seriesToStackMap: StackMap): SeriesList => {
const groups = new Set();
seriesList.forEach((seriesItem, i) => {
if (seriesItem.getPointTransformer.isBroad) {
groups.add(getGroupName(seriesItem, i, seriesToStackMap));
}
});
// There cannot be single group.
if (groups.size < 2) {
return seriesList;
}
const scale = scaleBand().domain(Array.from(groups)).range([0, groups.size]);
return seriesList.map((seriesItem, i) => {
if (!seriesItem.getPointTransformer.isBroad) {
return seriesItem;
}
const getPointTransformer = getGroupedPointTransformer(
seriesItem.getPointTransformer,
groups.size,
scale(getGroupName(seriesItem, i, seriesToStackMap))!,
);
return {
...seriesItem,
getPointTransformer,
};
});
};
示例2: gridLayout
export function gridLayout(dims, data, minWidth, designatedTotal) {
const xScale: any = scaleBand<number>();
const yScale: any = scaleBand<number>();
const width = dims.width;
const height = dims.height;
const [columns, rows] = gridSize(dims, data.length, minWidth);
const xDomain = [];
const yDomain = [];
for (let i = 0; i < rows; i++) {
yDomain.push(i);
}
for (let i = 0; i < columns; i++) {
xDomain.push(i);
}
xScale.domain(xDomain);
yScale.domain(yDomain);
xScale.rangeRound([0, width], 0.1);
yScale.rangeRound([0, height], 0.1);
const res = [];
const total = designatedTotal ? designatedTotal : getTotal(data);
const cardWidth = xScale.bandwidth();
const cardHeight = yScale.bandwidth();
for (let i = 0; i < data.length; i++) {
res[i] = {};
res[i].data = {
name: data[i] ? data[i].name : '',
value: data[i] ? data[i].value : undefined,
extra: data[i] ? data[i].extra : undefined,
};
res[i].x = xScale(i % columns);
res[i].y = yScale(Math.floor(i / columns));
res[i].width = cardWidth;
res[i].height = cardHeight;
res[i].data.percent = (total > 0) ? res[i].data.value / total : 0;
res[i].data.total = total;
}
return res;
}
示例3: initBarGraph
initBarGraph() {
const svg = selection.select('.line-graph-container')
.append('svg')
.attr('width', 700)
.attr('height', 300);
const margin = {top: 20, right: 20, bottom: 30, left: 40};
const width = +svg.attr('width') - margin.left - margin.right;
const height = +svg.attr('height') - margin.top - margin.bottom;
const x = scaleBand().rangeRound([0, width]).padding(0.1);
const y = scaleLinear().rangeRound([height, 0]);
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// uses sample data from https://bl.ocks.org/mbostock/3885304
(d3 as any).tsv('/assets/data.tsv').then(function(d) {
d.frequency = +d.frequency;
return d;
}, function(error, data: [{ letter: string, frequency: number}]) {
if (error) {
throw error;
}
x.domain(data.map((d) => d.letter));
y.domain(d3.extent(data, (d) => d.frequency));
g.append('g')
.attr('class', 'axis axis--x')
.attr('transform', `translate(0, ${height})`)
.call(d3.axisBottom(x));
g.append('g')
.attr('class', 'axis axis--y')
.call(d3.axisLeft(y).ticks(10, '%').tickPadding(23))
.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 6)
.attr('dy', '0.71em')
.attr('text-anchor', 'end')
.text('Frequency');
g.selectAll('.bar')
.data(data)
.enter().append('rect')
.attr('class', 'bar')
.attr('x', (d) => x(d.letter))
.attr('y', (d) => y(d.frequency))
.attr('width', x.bandwidth())
.attr('height', (d) => height - y(d.frequency));
});
}
示例4:
export const scaleBand: FactoryFn = () => (
d3ScaleBand().paddingInner(0.3).paddingOuter(0.15) as any
);
示例5: scaleLinear
// --------------------------------------------------------------------------
let num: number;
let axisScaleNumber: d3Axis.AxisScale<number>;
let axisScaleDate: d3Axis.AxisScale<Date>;
let axisScaleString: d3Axis.AxisScale<string>;
// --------------------------------------------------------------------------
// Test AxisScale Helper Interface
// --------------------------------------------------------------------------
axisScaleNumber = scaleLinear();
axisScaleDate = scaleTime();
axisScaleString = scaleOrdinal<number>();
axisScaleNumber = scaleBand<number>();
axisScaleNumber = scalePoint<number>();
axisScaleString = scaleBand();
axisScaleString = scalePoint();
// --------------------------------------------------------------------------
// Test AxisContainerElement
// --------------------------------------------------------------------------
let containerElement: d3Axis.AxisContainerElement;
const svg: SVGSVGElement = select<SVGSVGElement, any>('svg').node() !; // mock
const g: SVGGElement = select<SVGGElement, any>('g').node() !; // mock
const canvas: HTMLCanvasElement = select<HTMLCanvasElement, any>('canvas').node() !; // mock
containerElement = svg;
containerElement = g;
// containerElement = canvas; // fails, incompatible type
示例6: ordinalScaleStringNumber
outputNumber = ordinalScaleStringNumber('negative');
// copy(...) -----------------------------------------------------------------
const copiedOrdinalScale: d3Scale.ScaleOrdinal<string, number> = ordinalScaleStringNumber.copy();
// -------------------------------------------------------------------------------
// Band Scale Factory
// -------------------------------------------------------------------------------
// scaleBand() -----------------------------------------------------------------
let bandScaleString: d3Scale.ScaleBand<string>;
let bandScaleCoercible: d3Scale.ScaleBand<StringCoercible>;
bandScaleString = d3Scale.scaleBand();
bandScaleCoercible = d3Scale.scaleBand<StringCoercible>();
// ScaleBand Interface ========================================================
// domain(...) -----------------------------------------------------------------
bandScaleString = bandScaleString.domain(['negative', 'neutral', 'positive']);
domainStrings = bandScaleString.domain();
bandScaleCoercible = bandScaleCoercible.domain([new StringCoercible('negative'), new StringCoercible('neutral'), new StringCoercible('positive')]);
// range(...) -----------------------------------------------------------------
bandScaleString = bandScaleString.range([0, 300]);
let rangeExtent: [number, number] = bandScaleString.range();
示例7: getLinearGradientStops
getLinearGradientStops(value, start) {
if (start === undefined) {
start = this.domain[0];
}
const valueScale = scaleLinear()
.domain(this.domain)
.range([0, 1]);
const colorValueScale = scaleBand()
.domain(this.colorDomain)
.range([0, 1]);
const endColor = this.getColor(value);
// generate the stops
const startVal = valueScale(start);
const startColor = this.getColor(start);
const endVal = valueScale(value);
let i = 1;
let currentVal = startVal;
const stops = [];
stops.push({
color: startColor,
offset: startVal,
originalOffset: startVal,
opacity: 1
});
while (currentVal < endVal && i < this.colorDomain.length) {
const color = this.colorDomain[i];
const offset = colorValueScale(color);
if (offset <= startVal) {
i++;
continue;
}
if (offset.toFixed(4) >= (endVal - colorValueScale.bandwidth()).toFixed(4)) {
break;
}
stops.push({
color,
offset,
opacity: 1
});
currentVal = offset;
i++;
}
if (stops[stops.length - 1].offset < 100) {
stops.push({
color: endColor,
offset: endVal,
opacity: 1
});
}
if (endVal === startVal) {
stops[0].offset = 0;
stops[1].offset = 100;
} else {
// normalize the offsets into percentages
if (stops[stops.length - 1].offset !== 100) {
for (const s of stops) {
s.offset = ((s.offset - startVal) / (endVal - startVal)) * 100;
}
}
}
return stops;
}