MCPcopy
hub / github.com/palantir/plottable / stack

Function stack

src/utils/stackingUtils.ts:65–110  ·  view source on GitHub ↗
(
  datasets: Dataset[],
  keyAccessor: IAccessor<any>,
  valueAccessor: IAccessor<number>,
  stackingOrder: IStackingOrder = "bottomup",
)

Source from the content-addressed store, hash-verified

63 * @return {StackingResult} value and offset for each datapoint in each Dataset
64 */
65export function stack(
66 datasets: Dataset[],
67 keyAccessor: IAccessor<any>,
68 valueAccessor: IAccessor<number>,
69 stackingOrder: IStackingOrder = "bottomup",
70): StackingResult {
71 const positiveOffsets = d3.map<number>();
72 const negativeOffsets = d3.map<number>();
73 const datasetToKeyToStackedDatum = new Utils.Map<Dataset, Utils.Map<string, StackedDatum>>();
74
75 if (stackingOrder === "topdown") {
76 datasets = datasets.slice();
77 datasets.reverse();
78 }
79
80 for (const dataset of datasets) {
81 const keyToStackedDatum = new Utils.Map<string, StackedDatum>();
82 const data = dataset.data();
83 const dataLen = data.length;
84 for (let index = 0; index < dataLen; index++) {
85 const datum = data[index];
86 const accessedKey = keyAccessor(datum, index, dataset);
87 const key = normalizeKey(accessedKey);
88 const value = +valueAccessor(datum, index, dataset);
89 let offset: number;
90 const offsetMap = (value >= 0) ? positiveOffsets : negativeOffsets;
91 if (offsetMap.has(key)) {
92 offset = offsetMap.get(key);
93 offsetMap.set(key, offset + value);
94 } else {
95 offset = 0;
96 offsetMap.set(key, value);
97 }
98 keyToStackedDatum.set(key, {
99 offset: offset,
100 value: value,
101 axisValue: accessedKey,
102 originalDatum: datum,
103 originalDataset: dataset,
104 originalIndex: index,
105 });
106 }
107 datasetToKeyToStackedDatum.set(dataset, keyToStackedDatum);
108 }
109 return datasetToKeyToStackedDatum;
110}
111
112/**
113 * Computes the maximum and minimum extents of each stack individually.

Callers

nothing calls this directly

Calls 6

setMethod · 0.95
keyAccessorFunction · 0.85
dataMethod · 0.80
valueAccessorFunction · 0.50
hasMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected