forked from perspective-dev/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxy-scatter.js
More file actions
184 lines (167 loc) · 7.36 KB
/
xy-scatter.js
File metadata and controls
184 lines (167 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import { select } from "d3";
import { symbolTypeFromColumn } from "../series/pointSeriesCanvas";
import { pointData } from "../data/pointData";
import {
seriesColorsFromColumn,
seriesColorsFromDistinct,
colorScale,
} from "../series/seriesColors";
import { seriesColorRange } from "../series/seriesRange";
import { symbolLegend, colorLegend } from "../legend/legend";
import { colorRangeLegend } from "../legend/colorRangeLegend";
import { filterDataByGroup } from "../legend/filter";
import { symbolsObj } from "../series/seriesSymbols";
import { gridLayoutMultiChart } from "../layout/gridLayoutMultiChart";
import xyScatterSeries from "../series/xy-scatter/xyScatterSeries";
/**
* Overrides specific symbols based on plugin settings. This modifies in-place _and_ returns the value.
* @param {any} settings
* @param {d3.ScaleOrdinal} symbols
*/
function overrideSymbols(settings, symbols) {
if (!symbols) {
return;
}
const symbolCol = settings.realValues[4];
const columnType = settings.mainValues.find(
(val) => val.name === symbolCol
)?.type;
let domain = symbols.domain();
let range = symbols.range();
let len = range.length;
for (let i in domain) {
range[i] = range[i % len];
}
let maybeSymbols = settings.columns?.[symbolCol]?.symbols;
Object.entries(maybeSymbols ?? {}).forEach(([key, value]) => {
// TODO: Define custom symbol types based on the values passed in here.
// https://d3js.org/d3-shape/symbol#custom-symbols
let symbolType = symbolsObj[value] ?? d3.symbolCircle;
let i = domain.findIndex((val) => {
switch (columnType) {
case "date":
case "datetime":
return Date(val) === Date(key);
default:
return String(val) === String(key);
}
});
range[i] = symbolType;
});
symbols.range(range);
return symbols;
}
function xyScatter(container, settings) {
const colorBy = settings.realValues[2];
let hasColorBy = !!colorBy;
let isColoredByString =
settings.mainValues.find((x) => x.name === colorBy)?.type === "string";
let color = null;
let legend = null;
const symbolCol = settings.realValues[4];
const symbols = overrideSymbols(
settings,
symbolTypeFromColumn(settings, symbolCol)
);
const data = pointData(settings, filterDataByGroup(settings));
if (hasColorBy && isColoredByString) {
if (!!symbolCol) {
// TODO: Legend should have cartesian product labels (ColorBy|SplitBy)
// For now, just use monocolor legends.
if (settings.splitValues.length > 0) {
color = seriesColorsFromDistinct(settings, data);
} else {
color = seriesColorsFromDistinct(settings, data[0]);
}
legend = symbolLegend().settings(settings).scale(symbols);
} else {
color = seriesColorsFromColumn(settings, colorBy);
legend = colorLegend().settings(settings).scale(color);
}
} else if (hasColorBy) {
color = seriesColorRange(settings, data, "colorValue");
legend = colorRangeLegend().scale(color);
} else {
// always use default color
color = colorScale().settings(settings).domain([""])();
legend = symbolLegend().settings(settings).scale(symbols);
}
if (settings.splitValues.length !== 0) {
let xyGrid = gridLayoutMultiChart()
.svg(false)
.elementsPrefix("xy-scatter");
xyGrid = xyGrid.padding("2.5em");
const xLabel = container.append("div").attr("class", "multi-xlabel");
xLabel.append("p").text(settings.mainValues[0].name);
const yLabel = container.append("div").attr("class", "multi-ylabel");
yLabel
.append("p")
.text(settings.mainValues[1].name)
.style("transform", "rotate(-90deg)")
.style("text-wrap", "nowrap");
container = container.datum(data);
container.call(xyGrid);
const xyContainer = xyGrid.chartContainer();
const xyEnter = xyGrid.chartEnter();
const xyDiv = xyGrid.chartDiv();
const xyTitle = xyGrid.chartTitle();
const containerSize = xyGrid.containerSize();
xyTitle.each((d, i, nodes) => select(nodes[i]).text(d.key));
xyEnter
.merge(xyDiv)
.attr(
"transform",
`translate(${containerSize.width / 2}, ${
containerSize.height / 2
})`
)
.each(function (data) {
const xyElement = select(this);
xyScatterSeries()
.settings(settings)
.data([data])
.color(color)
.symbols(symbols)(xyElement);
});
} else {
xyScatterSeries()
.settings(settings)
.data(data)
.color(color)
.symbols(symbols)(container);
}
if (legend) container.call(legend);
}
xyScatter.plugin = {
name: "X/Y Scatter",
category: "X/Y Chart",
max_cells: 50000,
max_columns: 50,
render_warning: true,
initial: {
type: "number",
count: 2,
names: [
"X Axis",
"Y Axis",
"Color",
"Size",
"Symbol",
"Label",
"Tooltip",
],
},
selectMode: "toggle",
};
export default xyScatter;