d3-array#sum JavaScript Examples
The following examples show how to use
d3-array#sum.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: indexRollupNext.js From t-viSNE with MIT License | 4 votes |
function color() {
var scale = scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 2,
cells = [5],
cellFilter = void 0,
labels = [],
classPrefix = "",
useClass = false,
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap = void 0,
orient = "vertical",
ascending = false,
path = void 0,
titleWidth = void 0,
legendDispatcher = dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, locale.format(specifier), labelDelimiter),
legendG = svg.selectAll("g").data([scale]);
legendG.enter().append("g").attr("class", classPrefix + "legendCells");
if (cellFilter) {
helper.d3_filterCells(type, cellFilter);
}
var cell = svg.select("." + classPrefix + "legendCells").selectAll("." + classPrefix + "cell").data(type.data);
var cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell");
cellEnter.append(shape).attr("class", classPrefix + "swatch");
var shapes = svg.selectAll("g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch").data(type.data);
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
cell.exit().transition().style("opacity", 0).remove();
shapes.exit().transition().style("opacity", 0).remove();
shapes = shapes.merge(shapes);
helper.d3_drawShapes(shape, shapes, shapeHeight, shapeWidth, shapeRadius, path);
var text = helper.d3_addText(svg, cellEnter, type.labels, classPrefix, labelWrap);
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell);
// sets placement
var textSize = text.nodes().map(function (d) {
return d.getBBox();
}),
shapeSize = shapes.nodes().map(function (d) {
return d.getBBox();
});
//sets scale
//everything is fill except for line which is stroke,
if (!useClass) {
if (shape == "line") {
shapes.style("stroke", type.feature);
} else {
shapes.style("fill", type.feature);
}
} else {
shapes.attr("class", function (d) {
return classPrefix + "swatch " + type.feature(d);
});
}
var cellTrans = void 0,
textTrans = void 0,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
(function () {
var cellSize = textSize.map(function (d, i) {
return Math.max(d.height, shapeSize[i].height);
});
cellTrans = function cellTrans(d, i) {
var height = sum(cellSize.slice(0, i));
return "translate(0, " + (height + i * shapePadding) + ")";
};
textTrans = function textTrans(d, i) {
return "translate( " + (shapeSize[i].width + shapeSize[i].x + labelOffset) + ", " + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
})();
} else if (orient === "horizontal") {
cellTrans = function cellTrans(d, i) {
return "translate(" + i * (shapeSize[i].width + shapePadding) + ",0)";
};
textTrans = function textTrans(d, i) {
return "translate(" + (shapeSize[i].width * textAlign + shapeSize[i].x) + ",\n " + (shapeSize[i].height + shapeSize[i].y + labelOffset + 8) + ")";
};
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix, titleWidth);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.cellFilter = function (_) {
if (!arguments.length) return cellFilter;
cellFilter = _;
return legend;
};
legend.shape = function (_, d) {
if (!arguments.length) return shape;
if (_ == "rect" || _ == "circle" || _ == "line" || _ == "path" && typeof d === "string") {
shape = _;
path = d;
}
return legend;
};
legend.shapeWidth = function (_) {
if (!arguments.length) return shapeWidth;
shapeWidth = +_;
return legend;
};
legend.shapeHeight = function (_) {
if (!arguments.length) return shapeHeight;
shapeHeight = +_;
return legend;
};
legend.shapeRadius = function (_) {
if (!arguments.length) return shapeRadius;
shapeRadius = +_;
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.locale = function (_) {
if (!arguments.length) return locale;
locale = formatLocale(_);
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return legend.locale().format(specifier);
specifier = formatSpecifier(_);
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.labelWrap = function (_) {
if (!arguments.length) return labelWrap;
labelWrap = _;
return legend;
};
legend.useClass = function (_) {
if (!arguments.length) return useClass;
if (_ === true || _ === false) {
useClass = _;
}
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.titleWidth = function (_) {
if (!arguments.length) return titleWidth;
titleWidth = _;
return legend;
};
legend.textWrap = function (_) {
if (!arguments.length) return textWrap;
textWrap = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
}
Example #2
Source File: indexRollupNext.js From t-viSNE with MIT License | 4 votes |
function size() {
var scale = scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapePadding = 2,
cells = [5],
cellFilter = void 0,
labels = [],
classPrefix = "",
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap = void 0,
orient = "vertical",
ascending = false,
path = void 0,
titleWidth = void 0,
legendDispatcher = dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, locale.format(specifier), labelDelimiter),
legendG = svg.selectAll("g").data([scale]);
if (cellFilter) {
helper.d3_filterCells(type, cellFilter);
}
legendG.enter().append("g").attr("class", classPrefix + "legendCells");
var cell = svg.select("." + classPrefix + "legendCells").selectAll("." + classPrefix + "cell").data(type.data);
var cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell");
cellEnter.append(shape).attr("class", classPrefix + "swatch");
var shapes = svg.selectAll("g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch");
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
cell.exit().transition().style("opacity", 0).remove();
shapes.exit().transition().style("opacity", 0).remove();
shapes = shapes.merge(shapes);
//creates shape
if (shape === "line") {
helper.d3_drawShapes(shape, shapes, 0, shapeWidth);
shapes.attr("stroke-width", type.feature);
} else {
helper.d3_drawShapes(shape, shapes, type.feature, type.feature, type.feature, path);
}
var text = helper.d3_addText(svg, cellEnter, type.labels, classPrefix, labelWrap);
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell);
//sets placement
var textSize = text.nodes().map(function (d) {
return d.getBBox();
}),
shapeSize = shapes.nodes().map(function (d, i) {
var bbox = d.getBBox();
var stroke = scale(type.data[i]);
if (shape === "line" && orient === "horizontal") {
bbox.height = bbox.height + stroke;
} else if (shape === "line" && orient === "vertical") {
bbox.width = bbox.width;
}
return bbox;
});
var maxH = max(shapeSize, function (d) {
return d.height + d.y;
}),
maxW = max(shapeSize, function (d) {
return d.width + d.x;
});
var cellTrans = void 0,
textTrans = void 0,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
(function () {
var cellSize = textSize.map(function (d, i) {
return Math.max(d.height, shapeSize[i].height);
});
var y = shape == "circle" || shape == "line" ? shapeSize[0].height / 2 : 0;
cellTrans = function cellTrans(d, i) {
var height = sum(cellSize.slice(0, i));
return "translate(0, " + (y + height + i * shapePadding) + ")";
};
textTrans = function textTrans(d, i) {
return "translate( " + (maxW + labelOffset) + ",\n " + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
})();
} else if (orient === "horizontal") {
(function () {
cellTrans = function cellTrans(d, i) {
var width = sum(shapeSize.slice(0, i), function (d) {
return d.width;
});
var y = shape == "circle" || shape == "line" ? maxH / 2 : 0;
return "translate(" + (width + i * shapePadding) + ", " + y + ")";
};
var offset = shape == "line" ? maxH / 2 : maxH;
textTrans = function textTrans(d, i) {
return "translate( " + (shapeSize[i].width * textAlign + shapeSize[i].x) + ",\n " + (offset + labelOffset) + ")";
};
})();
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix, titleWidth);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.cellFilter = function (_) {
if (!arguments.length) return cellFilter;
cellFilter = _;
return legend;
};
legend.shape = function (_, d) {
if (!arguments.length) return shape;
if (_ == "rect" || _ == "circle" || _ == "line") {
shape = _;
path = d;
}
return legend;
};
legend.shapeWidth = function (_) {
if (!arguments.length) return shapeWidth;
shapeWidth = +_;
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.locale = function (_) {
if (!arguments.length) return locale;
locale = formatLocale(_);
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return legend.locale().format(specifier);
specifier = formatSpecifier(_);
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.labelWrap = function (_) {
if (!arguments.length) return labelWrap;
labelWrap = _;
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.titleWidth = function (_) {
if (!arguments.length) return titleWidth;
titleWidth = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
}
Example #3
Source File: indexRollupNext.js From t-viSNE with MIT License | 4 votes |
function symbol() {
var scale = scaleLinear(),
shape = "path",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 5,
cells = [5],
cellFilter = void 0,
labels = [],
classPrefix = "",
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelAlign = "middle",
labelOffset = 10,
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap = void 0,
orient = "vertical",
ascending = false,
titleWidth = void 0,
legendDispatcher = dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, locale.format(specifier), labelDelimiter),
legendG = svg.selectAll("g").data([scale]);
if (cellFilter) {
helper.d3_filterCells(type, cellFilter);
}
legendG.enter().append("g").attr("class", classPrefix + "legendCells");
var cell = svg.select("." + classPrefix + "legendCells").selectAll("." + classPrefix + "cell").data(type.data);
var cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell");
cellEnter.append(shape).attr("class", classPrefix + "swatch");
var shapes = svg.selectAll("g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch");
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
//remove old shapes
cell.exit().transition().style("opacity", 0).remove();
shapes.exit().transition().style("opacity", 0).remove();
shapes = shapes.merge(shapes);
helper.d3_drawShapes(shape, shapes, shapeHeight, shapeWidth, shapeRadius, type.feature);
var text = helper.d3_addText(svg, cellEnter, type.labels, classPrefix, labelWrap);
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell);
// sets placement
var textSize = text.nodes().map(function (d) {
return d.getBBox();
}),
shapeSize = shapes.nodes().map(function (d) {
return d.getBBox();
});
var maxH = max(shapeSize, function (d) {
return d.height;
}),
maxW = max(shapeSize, function (d) {
return d.width;
});
var cellTrans = void 0,
textTrans = void 0,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
(function () {
var cellSize = textSize.map(function (d, i) {
return Math.max(maxH, d.height);
});
cellTrans = function cellTrans(d, i) {
var height = sum(cellSize.slice(0, i));
return "translate(0, " + (height + i * shapePadding) + " )";
};
textTrans = function textTrans(d, i) {
return "translate( " + (maxW + labelOffset) + ",\n " + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
})();
} else if (orient === "horizontal") {
cellTrans = function cellTrans(d, i) {
return "translate( " + i * (maxW + shapePadding) + ",0)";
};
textTrans = function textTrans(d, i) {
return "translate( " + (shapeSize[i].width * textAlign + shapeSize[i].x) + ",\n " + (maxH + labelOffset) + ")";
};
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix, titleWidth);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.cellFilter = function (_) {
if (!arguments.length) return cellFilter;
cellFilter = _;
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.locale = function (_) {
if (!arguments.length) return locale;
locale = formatLocale(_);
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return legend.locale().format(specifier);
specifier = formatSpecifier(_);
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.labelWrap = function (_) {
if (!arguments.length) return labelWrap;
labelWrap = _;
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.titleWidth = function (_) {
if (!arguments.length) return titleWidth;
titleWidth = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
}
Example #4
Source File: color.js From t-viSNE with MIT License | 4 votes |
export default function color() {
let scale = scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 2,
cells = [5],
cellFilter,
labels = [],
classPrefix = "",
useClass = false,
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap,
orient = "vertical",
ascending = false,
path,
titleWidth,
legendDispatcher = dispatch("cellover", "cellout", "cellclick")
function legend(svg) {
const type = helper.d3_calcType(
scale,
ascending,
cells,
labels,
locale.format(specifier),
labelDelimiter
),
legendG = svg.selectAll("g").data([scale])
legendG
.enter()
.append("g")
.attr("class", classPrefix + "legendCells")
if (cellFilter) {
helper.d3_filterCells(type, cellFilter)
}
let cell = svg
.select("." + classPrefix + "legendCells")
.selectAll("." + classPrefix + "cell")
.data(type.data)
const cellEnter = cell
.enter()
.append("g")
.attr("class", classPrefix + "cell")
cellEnter.append(shape).attr("class", classPrefix + "swatch")
let shapes = svg
.selectAll(
"g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch"
)
.data(type.data)
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher)
cell
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes = shapes.merge(shapes)
helper.d3_drawShapes(
shape,
shapes,
shapeHeight,
shapeWidth,
shapeRadius,
path
)
const text = helper.d3_addText(
svg,
cellEnter,
type.labels,
classPrefix,
labelWrap
)
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell)
// sets placement
const textSize = text.nodes().map(d => d.getBBox()),
shapeSize = shapes.nodes().map(d => d.getBBox())
//sets scale
//everything is fill except for line which is stroke,
if (!useClass) {
if (shape == "line") {
shapes.style("stroke", type.feature)
} else {
shapes.style("fill", type.feature)
}
} else {
shapes.attr("class", d => `${classPrefix}swatch ${type.feature(d)}`)
}
let cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1
//positions cells and text
if (orient === "vertical") {
const cellSize = textSize.map((d, i) =>
Math.max(d.height, shapeSize[i].height)
)
cellTrans = (d, i) => {
const height = sum(cellSize.slice(0, i))
return `translate(0, ${height + i * shapePadding})`
}
textTrans = (d, i) =>
`translate( ${shapeSize[i].width +
shapeSize[i].x +
labelOffset}, ${shapeSize[i].y + shapeSize[i].height / 2 + 5})`
} else if (orient === "horizontal") {
cellTrans = (d, i) =>
`translate(${i * (shapeSize[i].width + shapePadding)},0)`
textTrans = (d, i) => `translate(${shapeSize[i].width * textAlign +
shapeSize[i].x},
${shapeSize[i].height + shapeSize[i].y + labelOffset + 8})`
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign)
helper.d3_title(svg, title, classPrefix, titleWidth)
cell.transition().style("opacity", 1)
}
legend.scale = function(_) {
if (!arguments.length) return scale
scale = _
return legend
}
legend.cells = function(_) {
if (!arguments.length) return cells
if (_.length > 1 || _ >= 2) {
cells = _
}
return legend
}
legend.cellFilter = function(_) {
if (!arguments.length) return cellFilter
cellFilter = _
return legend
}
legend.shape = function(_, d) {
if (!arguments.length) return shape
if (
_ == "rect" ||
_ == "circle" ||
_ == "line" ||
(_ == "path" && typeof d === "string")
) {
shape = _
path = d
}
return legend
}
legend.shapeWidth = function(_) {
if (!arguments.length) return shapeWidth
shapeWidth = +_
return legend
}
legend.shapeHeight = function(_) {
if (!arguments.length) return shapeHeight
shapeHeight = +_
return legend
}
legend.shapeRadius = function(_) {
if (!arguments.length) return shapeRadius
shapeRadius = +_
return legend
}
legend.shapePadding = function(_) {
if (!arguments.length) return shapePadding
shapePadding = +_
return legend
}
legend.labels = function(_) {
if (!arguments.length) return labels
labels = _
return legend
}
legend.labelAlign = function(_) {
if (!arguments.length) return labelAlign
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _
}
return legend
}
legend.locale = function(_) {
if (!arguments.length) return locale
locale = formatLocale(_)
return legend
}
legend.labelFormat = function(_) {
if (!arguments.length) return legend.locale().format(specifier)
specifier = formatSpecifier(_)
return legend
}
legend.labelOffset = function(_) {
if (!arguments.length) return labelOffset
labelOffset = +_
return legend
}
legend.labelDelimiter = function(_) {
if (!arguments.length) return labelDelimiter
labelDelimiter = _
return legend
}
legend.labelWrap = function(_) {
if (!arguments.length) return labelWrap
labelWrap = _
return legend
}
legend.useClass = function(_) {
if (!arguments.length) return useClass
if (_ === true || _ === false) {
useClass = _
}
return legend
}
legend.orient = function(_) {
if (!arguments.length) return orient
_ = _.toLowerCase()
if (_ == "horizontal" || _ == "vertical") {
orient = _
}
return legend
}
legend.ascending = function(_) {
if (!arguments.length) return ascending
ascending = !!_
return legend
}
legend.classPrefix = function(_) {
if (!arguments.length) return classPrefix
classPrefix = _
return legend
}
legend.title = function(_) {
if (!arguments.length) return title
title = _
return legend
}
legend.titleWidth = function(_) {
if (!arguments.length) return titleWidth
titleWidth = _
return legend
}
legend.textWrap = function(_) {
if (!arguments.length) return textWrap
textWrap = _
return legend
}
legend.on = function() {
const value = legendDispatcher.on.apply(legendDispatcher, arguments)
return value === legendDispatcher ? legend : value
}
return legend
}
Example #5
Source File: size.js From t-viSNE with MIT License | 4 votes |
export default function size() {
let scale = scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapePadding = 2,
cells = [5],
cellFilter,
labels = [],
classPrefix = "",
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap,
orient = "vertical",
ascending = false,
path,
titleWidth,
legendDispatcher = dispatch("cellover", "cellout", "cellclick")
function legend(svg) {
const type = helper.d3_calcType(
scale,
ascending,
cells,
labels,
locale.format(specifier),
labelDelimiter
),
legendG = svg.selectAll("g").data([scale])
if (cellFilter) {
helper.d3_filterCells(type, cellFilter)
}
legendG
.enter()
.append("g")
.attr("class", classPrefix + "legendCells")
let cell = svg
.select("." + classPrefix + "legendCells")
.selectAll("." + classPrefix + "cell")
.data(type.data)
const cellEnter = cell
.enter()
.append("g")
.attr("class", classPrefix + "cell")
cellEnter.append(shape).attr("class", classPrefix + "swatch")
let shapes = svg.selectAll(
"g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch"
)
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher)
cell
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes = shapes.merge(shapes)
//creates shape
if (shape === "line") {
helper.d3_drawShapes(shape, shapes, 0, shapeWidth)
shapes.attr("stroke-width", type.feature)
} else {
helper.d3_drawShapes(
shape,
shapes,
type.feature,
type.feature,
type.feature,
path
)
}
const text = helper.d3_addText(
svg,
cellEnter,
type.labels,
classPrefix,
labelWrap
)
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell)
//sets placement
const textSize = text.nodes().map(d => d.getBBox()),
shapeSize = shapes.nodes().map((d, i) => {
const bbox = d.getBBox()
const stroke = scale(type.data[i])
if (shape === "line" && orient === "horizontal") {
bbox.height = bbox.height + stroke
} else if (shape === "line" && orient === "vertical") {
bbox.width = bbox.width
}
return bbox
})
const maxH = max(shapeSize, d => d.height + d.y),
maxW = max(shapeSize, d => d.width + d.x)
let cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1
//positions cells and text
if (orient === "vertical") {
const cellSize = textSize.map((d, i) =>
Math.max(d.height, shapeSize[i].height)
)
const y =
shape == "circle" || shape == "line" ? shapeSize[0].height / 2 : 0
cellTrans = (d, i) => {
const height = sum(cellSize.slice(0, i))
return `translate(0, ${y + height + i * shapePadding})`
}
textTrans = (d, i) => `translate( ${maxW + labelOffset},
${shapeSize[i].y + shapeSize[i].height / 2 + 5})`
} else if (orient === "horizontal") {
cellTrans = (d, i) => {
const width = sum(shapeSize.slice(0, i), d => d.width)
const y = shape == "circle" || shape == "line" ? maxH / 2 : 0
return `translate(${width + i * shapePadding}, ${y})`
}
const offset = shape == "line" ? maxH / 2 : maxH
textTrans = (d, i) => {
return `translate( ${shapeSize[i].width * textAlign + shapeSize[i].x},
${offset + labelOffset})`
}
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign)
helper.d3_title(svg, title, classPrefix, titleWidth)
cell.transition().style("opacity", 1)
}
legend.scale = function(_) {
if (!arguments.length) return scale
scale = _
return legend
}
legend.cells = function(_) {
if (!arguments.length) return cells
if (_.length > 1 || _ >= 2) {
cells = _
}
return legend
}
legend.cellFilter = function(_) {
if (!arguments.length) return cellFilter
cellFilter = _
return legend
}
legend.shape = function(_, d) {
if (!arguments.length) return shape
if (_ == "rect" || _ == "circle" || _ == "line") {
shape = _
path = d
}
return legend
}
legend.shapeWidth = function(_) {
if (!arguments.length) return shapeWidth
shapeWidth = +_
return legend
}
legend.shapePadding = function(_) {
if (!arguments.length) return shapePadding
shapePadding = +_
return legend
}
legend.labels = function(_) {
if (!arguments.length) return labels
labels = _
return legend
}
legend.labelAlign = function(_) {
if (!arguments.length) return labelAlign
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _
}
return legend
}
legend.locale = function(_) {
if (!arguments.length) return locale
locale = formatLocale(_)
return legend
}
legend.labelFormat = function(_) {
if (!arguments.length) return legend.locale().format(specifier)
specifier = formatSpecifier(_)
return legend
}
legend.labelOffset = function(_) {
if (!arguments.length) return labelOffset
labelOffset = +_
return legend
}
legend.labelDelimiter = function(_) {
if (!arguments.length) return labelDelimiter
labelDelimiter = _
return legend
}
legend.labelWrap = function(_) {
if (!arguments.length) return labelWrap
labelWrap = _
return legend
}
legend.orient = function(_) {
if (!arguments.length) return orient
_ = _.toLowerCase()
if (_ == "horizontal" || _ == "vertical") {
orient = _
}
return legend
}
legend.ascending = function(_) {
if (!arguments.length) return ascending
ascending = !!_
return legend
}
legend.classPrefix = function(_) {
if (!arguments.length) return classPrefix
classPrefix = _
return legend
}
legend.title = function(_) {
if (!arguments.length) return title
title = _
return legend
}
legend.titleWidth = function(_) {
if (!arguments.length) return titleWidth
titleWidth = _
return legend
}
legend.on = function() {
const value = legendDispatcher.on.apply(legendDispatcher, arguments)
return value === legendDispatcher ? legend : value
}
return legend
}
Example #6
Source File: symbol.js From t-viSNE with MIT License | 4 votes |
export default function symbol() {
let scale = scaleLinear(),
shape = "path",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 5,
cells = [5],
cellFilter,
labels = [],
classPrefix = "",
title = "",
locale = helper.d3_defaultLocale,
specifier = helper.d3_defaultFormatSpecifier,
labelAlign = "middle",
labelOffset = 10,
labelDelimiter = helper.d3_defaultDelimiter,
labelWrap,
orient = "vertical",
ascending = false,
titleWidth,
legendDispatcher = dispatch("cellover", "cellout", "cellclick")
function legend(svg) {
const type = helper.d3_calcType(
scale,
ascending,
cells,
labels,
locale.format(specifier),
labelDelimiter
),
legendG = svg.selectAll("g").data([scale])
if (cellFilter) {
helper.d3_filterCells(type, cellFilter)
}
legendG
.enter()
.append("g")
.attr("class", classPrefix + "legendCells")
let cell = svg
.select("." + classPrefix + "legendCells")
.selectAll("." + classPrefix + "cell")
.data(type.data)
const cellEnter = cell
.enter()
.append("g")
.attr("class", classPrefix + "cell")
cellEnter.append(shape).attr("class", classPrefix + "swatch")
let shapes = svg.selectAll("g." + classPrefix + "cell " + shape + "." + classPrefix + "swatch")
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher)
//remove old shapes
cell
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes
.exit()
.transition()
.style("opacity", 0)
.remove()
shapes = shapes.merge(shapes)
helper.d3_drawShapes(
shape,
shapes,
shapeHeight,
shapeWidth,
shapeRadius,
type.feature
)
const text = helper.d3_addText(
svg,
cellEnter,
type.labels,
classPrefix,
labelWrap
)
// we need to merge the selection, otherwise changes in the legend (e.g. change of orientation) are applied only to the new cells and not the existing ones.
cell = cellEnter.merge(cell)
// sets placement
const textSize = text.nodes().map(d => d.getBBox()),
shapeSize = shapes.nodes().map(d => d.getBBox())
const maxH = max(shapeSize, d => d.height),
maxW = max(shapeSize, d => d.width)
let cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1
//positions cells and text
if (orient === "vertical") {
const cellSize = textSize.map((d, i) => Math.max(maxH, d.height))
cellTrans = (d, i) => {
const height = sum(cellSize.slice(0, i))
return `translate(0, ${height + i * shapePadding} )`
}
textTrans = (d, i) => `translate( ${maxW + labelOffset},
${shapeSize[i].y + shapeSize[i].height / 2 + 5})`
} else if (orient === "horizontal") {
cellTrans = (d, i) => `translate( ${i * (maxW + shapePadding)},0)`
textTrans = (d, i) => `translate( ${shapeSize[i].width * textAlign +
shapeSize[i].x},
${maxH + labelOffset})`
}
helper.d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign)
helper.d3_title(svg, title, classPrefix, titleWidth)
cell.transition().style("opacity", 1)
}
legend.scale = function(_) {
if (!arguments.length) return scale
scale = _
return legend
}
legend.cells = function(_) {
if (!arguments.length) return cells
if (_.length > 1 || _ >= 2) {
cells = _
}
return legend
}
legend.cellFilter = function(_) {
if (!arguments.length) return cellFilter
cellFilter = _
return legend
}
legend.shapePadding = function(_) {
if (!arguments.length) return shapePadding
shapePadding = +_
return legend
}
legend.labels = function(_) {
if (!arguments.length) return labels
labels = _
return legend
}
legend.labelAlign = function(_) {
if (!arguments.length) return labelAlign
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _
}
return legend
}
legend.locale = function(_) {
if (!arguments.length) return locale
locale = formatLocale(_)
return legend
}
legend.labelFormat = function(_) {
if (!arguments.length) return legend.locale().format(specifier)
specifier = formatSpecifier(_)
return legend
}
legend.labelOffset = function(_) {
if (!arguments.length) return labelOffset
labelOffset = +_
return legend
}
legend.labelDelimiter = function(_) {
if (!arguments.length) return labelDelimiter
labelDelimiter = _
return legend
}
legend.labelWrap = function(_) {
if (!arguments.length) return labelWrap
labelWrap = _
return legend
}
legend.orient = function(_) {
if (!arguments.length) return orient
_ = _.toLowerCase()
if (_ == "horizontal" || _ == "vertical") {
orient = _
}
return legend
}
legend.ascending = function(_) {
if (!arguments.length) return ascending
ascending = !!_
return legend
}
legend.classPrefix = function(_) {
if (!arguments.length) return classPrefix
classPrefix = _
return legend
}
legend.title = function(_) {
if (!arguments.length) return title
title = _
return legend
}
legend.titleWidth = function(_) {
if (!arguments.length) return titleWidth
titleWidth = _
return legend
}
legend.on = function() {
const value = legendDispatcher.on.apply(legendDispatcher, arguments)
return value === legendDispatcher ? legend : value
}
return legend
}
Example #7
Source File: sankey.js From the-eye-knows-the-garbage with MIT License | 4 votes |
export default function Sankey() {
var x0 = 0, y0 = 0, x1 = 1, y1 = 1, // extent
dx = 24, // nodeWidth
py = 8, // nodePadding
id = defaultId,
align = justify,
sort,
nodes = defaultNodes,
links = defaultLinks,
iterations = 6;
function sankey() {
var graph = {nodes: nodes.apply(null, arguments), links: links.apply(null, arguments)};
computeNodeLinks(graph);
computeNodeValues(graph);
computeNodeDepths(graph);
computeNodeBreadths(graph);
computeLinkBreadths(graph);
return graph;
}
sankey.update = function(graph) {
computeLinkBreadths(graph);
return graph;
};
sankey.nodeId = function(_) {
return arguments.length ? (id = typeof _ === "function" ? _ : constant(_), sankey) : id;
};
sankey.nodeAlign = function(_) {
return arguments.length ? (align = typeof _ === "function" ? _ : constant(_), sankey) : align;
};
sankey.nodeSort = function(_) {
return arguments.length ? (sort = _, sankey) : sort;
};
sankey.nodeWidth = function(_) {
return arguments.length ? (dx = +_, sankey) : dx;
};
sankey.nodePadding = function(_) {
return arguments.length ? (py = +_, sankey) : py;
};
sankey.nodes = function(_) {
return arguments.length ? (nodes = typeof _ === "function" ? _ : constant(_), sankey) : nodes;
};
sankey.links = function(_) {
return arguments.length ? (links = typeof _ === "function" ? _ : constant(_), sankey) : links;
};
sankey.size = function(_) {
return arguments.length ? (x0 = y0 = 0, x1 = +_[0], y1 = +_[1], sankey) : [x1 - x0, y1 - y0];
};
sankey.extent = function(_) {
return arguments.length ? (x0 = +_[0][0], x1 = +_[1][0], y0 = +_[0][1], y1 = +_[1][1], sankey) : [[x0, y0], [x1, y1]];
};
sankey.iterations = function(_) {
return arguments.length ? (iterations = +_, sankey) : iterations;
};
// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks(graph) {
graph.nodes.forEach(function(node, i) {
node.index = i;
node.sourceLinks = [];
node.targetLinks = [];
});
var nodeById = map(graph.nodes, id);
graph.links.forEach(function(link, i) {
link.index = i;
var source = link.source, target = link.target;
if (typeof source !== "object") source = link.source = find(nodeById, source);
if (typeof target !== "object") target = link.target = find(nodeById, target);
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}
// Compute the value (size) of each node by summing the associated links.
function computeNodeValues(graph) {
graph.nodes.forEach(function(node) {
node.value = Math.max(
sum(node.sourceLinks, value),
sum(node.targetLinks, value)
);
});
}
// Iteratively assign the depth (x-position) for each node.
// Nodes are assigned the maximum depth of incoming neighbors plus one;
// nodes with no incoming links are assigned depth zero, while
// nodes with no outgoing links are assigned the maximum depth.
function computeNodeDepths(graph) {
var nodes, next, x, n = graph.nodes.length;
for (nodes = graph.nodes, next = [], x = 0; nodes.length; ++x, nodes = next, next = []) {
if (x > n) throw new Error("circular link");
nodes.forEach(function(node) {
node.depth = x;
node.sourceLinks.forEach(function(link) {
if (next.indexOf(link.target) < 0) {
next.push(link.target);
}
});
});
}
for (nodes = graph.nodes, next = [], x = 0; nodes.length; ++x, nodes = next, next = []) {
if (x > n) throw new Error("circular link");
nodes.forEach(function(node) {
node.height = x;
node.targetLinks.forEach(function(link) {
if (next.indexOf(link.source) < 0) {
next.push(link.source);
}
});
});
}
var kx = (x1 - x0 - dx) / (x - 1);
graph.nodes.forEach(function(node) {
node.x1 = (node.x0 = x0 + Math.max(0, Math.min(x - 1, Math.floor(align.call(null, node, x)))) * kx) + dx;
});
}
function computeNodeBreadths(graph) {
var columns = nest()
.key(function(d) { return d.x0; })
.sortKeys(ascending)
.entries(graph.nodes)
.map(function(d) { return d.values; });
//
initializeNodeBreadth();
resolveCollisions();
for (var alpha = 0.9, n = iterations; n > 0; --n, alpha *= 0.9) {
relaxRightToLeft(alpha);
resolveCollisions();
relaxLeftToRight(alpha);
resolveCollisions();
}
function initializeNodeBreadth() {
var ky = min(columns, function(nodes) {
return (y1 - y0 - (nodes.length - 1) * py) / sum(nodes, value);
});
columns.forEach(function(nodes) {
if (sort != null) nodes.sort(sort);
nodes.forEach(function(node, i) {
node.y1 = (node.y0 = i) + node.value * ky;
});
});
graph.links.forEach(function(link) {
link.width = link.value * ky;
});
}
function relaxLeftToRight(alpha) {
columns.forEach(function(nodes) {
nodes.forEach(function(node) {
let y = node.y0;
for (const {target, width, value} of node.sourceLinks.sort(ascendingTargetBreadth)) {
if (value > 0) {
let dy = 0;
for (const {source, width} of target.targetLinks) {
if (source === node) break;
dy += width + py / 2;
}
dy = (y - dy - target.y0) * alpha * (value / Math.min(node.value, target.value));
target.y0 += dy;
target.y1 += dy;
}
y += width + py / 2;
}
});
});
}
function relaxRightToLeft(alpha) {
columns.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
let y = node.y0;
for (const {source, width, value} of node.targetLinks.sort(ascendingSourceBreadth)) {
if (value > 0) {
let dy = 0;
for (const {target, width} of source.sourceLinks) {
if (target === node) break;
dy += width + py / 2;
}
dy = (y - dy - source.y0) * alpha * (value / Math.min(node.value, source.value));
source.y0 += dy;
source.y1 += dy;
}
y += width + py / 2;
}
});
});
}
function resolveCollisions() {
columns.forEach(function(nodes) {
var node,
dy,
y = y0,
n = nodes.length,
i;
// Push any overlapping nodes down.
if (sort === undefined) nodes.sort(ascendingBreadth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y - node.y0;
if (dy > 0) node.y0 += dy, node.y1 += dy;
y = node.y1 + py;
}
// If the bottommost node goes outside the bounds, push it back up.
dy = y - py - y1;
if (dy > 0) {
y = (node.y0 -= dy), node.y1 -= dy;
// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.y1 + py - y;
if (dy > 0) node.y0 -= dy, node.y1 -= dy;
y = node.y0;
}
}
});
}
}
function computeLinkBreadths(graph) {
graph.nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetBreadth);
node.targetLinks.sort(ascendingSourceBreadth);
});
graph.nodes.forEach(function(node) {
var y0 = node.y0, y1 = y0;
node.sourceLinks.forEach(function(link) {
link.y0 = y0 + link.width / 2, y0 += link.width;
});
node.targetLinks.forEach(function(link) {
link.y1 = y1 + link.width / 2, y1 += link.width;
});
});
}
return sankey;
}