@emerson-eps/color-tables 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/component/ColorSelector/ColorSelectorAccordion.d.ts +2 -0
- package/dist/component/ColorSelector/{ColorTableSelectorWrapper.js → ColorSelectorAccordion.js} +12 -7
- package/dist/component/ColorSelector/ColorSelectorAccordion.js.map +1 -0
- package/dist/component/ColorSelector/ColorSelectorComponent.d.ts +12 -3
- package/dist/component/ColorSelector/ColorSelectorComponent.js +254 -51
- package/dist/component/ColorSelector/ColorSelectorComponent.js.map +1 -1
- package/dist/component/ColorSelector/ColorSelectorWrapper.d.ts +8 -0
- package/dist/component/ColorSelector/ColorSelectorWrapper.js +64 -0
- package/dist/component/ColorSelector/ColorSelectorWrapper.js.map +1 -0
- package/dist/component/Legend/ColorLegend.d.ts +13 -0
- package/dist/component/Legend/ColorLegend.js +26 -0
- package/dist/component/Legend/ColorLegend.js.map +1 -0
- package/dist/component/Legend/ContinuousLegend.d.ts +1 -0
- package/dist/component/Legend/ContinuousLegend.js +147 -75
- package/dist/component/Legend/ContinuousLegend.js.map +1 -1
- package/dist/component/Legend/DiscreteLegend.d.ts +2 -0
- package/dist/component/Legend/DiscreteLegend.js +118 -62
- package/dist/component/Legend/DiscreteLegend.js.map +1 -1
- package/dist/component/Utils/discreteLegend.d.ts +1 -1
- package/dist/component/Utils/discreteLegend.js +70 -50
- package/dist/component/Utils/discreteLegend.js.map +1 -1
- package/dist/component/color-tables.json +4 -4
- package/dist/stories/ColorLegends/ColorSelector/Selector.stories.d.ts +2 -2
- package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js +3 -3
- package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js.map +1 -1
- package/dist/stories/ColorLegends/DiscreteLegends/Facies.stories.js +11 -11
- package/dist/stories/ColorLegends/DiscreteLegends/Facies.stories.js.map +1 -1
- package/dist/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.js +1 -1
- package/dist/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.js.map +1 -1
- package/dist/stories/ColorLegends/LegendWithSelector/DiscreteLegendWithSelector.stories.d.ts +7 -0
- package/dist/stories/ColorLegends/LegendWithSelector/DiscreteLegendWithSelector.stories.js +41 -0
- package/dist/stories/ColorLegends/LegendWithSelector/DiscreteLegendWithSelector.stories.js.map +1 -0
- package/dist/stories/ColorLegends/LegendWithSelector/LegendWithSelector.stories.d.ts +7 -0
- package/dist/stories/ColorLegends/LegendWithSelector/LegendWithSelector.stories.js +27 -0
- package/dist/stories/ColorLegends/LegendWithSelector/LegendWithSelector.stories.js.map +1 -0
- package/dist/stories/ColorLegends/SingleComponent/Single.stories.d.ts +7 -0
- package/dist/stories/ColorLegends/SingleComponent/Single.stories.js +47 -0
- package/dist/stories/ColorLegends/SingleComponent/Single.stories.js.map +1 -0
- package/package.json +1 -1
- package/src/component/ColorSelector/{ColorTableSelectorWrapper.tsx → ColorSelectorAccordion.tsx} +11 -6
- package/src/component/ColorSelector/ColorSelectorComponent.tsx +308 -93
- package/src/component/ColorSelector/ColorSelectorWrapper.tsx +118 -0
- package/src/component/Legend/ColorLegend.tsx +81 -0
- package/src/component/Legend/ContinuousLegend.tsx +160 -79
- package/src/component/Legend/DiscreteLegend.tsx +127 -61
- package/src/component/Utils/continousLegend.ts +1 -1
- package/src/component/Utils/discreteLegend.ts +77 -52
- package/src/component/color-tables.json +4 -4
- package/src/stories/ColorLegends/ColorSelector/Selector.stories.jsx +3 -3
- package/src/stories/ColorLegends/DiscreteLegends/Facies.stories.jsx +39 -39
- package/src/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.jsx +1 -1
- package/src/stories/ColorLegends/LegendWithSelector/DiscreteLegendWithSelector.stories.jsx +44 -0
- package/src/stories/ColorLegends/LegendWithSelector/LegendWithSelector.stories.jsx +30 -0
- package/src/stories/ColorLegends/SingleComponent/Single.stories.jsx +51 -0
- package/dist/component/ColorSelector/ColorTableSelectorWrapper.d.ts +0 -2
- package/dist/component/ColorSelector/ColorTableSelectorWrapper.js.map +0 -1
- package/dist/component/ColorSelector/legendComponent.d.ts +0 -14
- package/dist/component/ColorSelector/legendComponent.js +0 -228
- package/dist/component/ColorSelector/legendComponent.js.map +0 -1
- package/src/component/ColorSelector/legendComponent.tsx +0 -289
|
@@ -1,109 +1,324 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const discreteD3ColorData: any = []
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { RGBToHex } from "../Utils/continousLegend";
|
|
4
|
+
import {d3ColorScales} from "../Utils/d3ColorScale";
|
|
5
|
+
import { select, range} from "d3";
|
|
6
|
+
import * as d3 from "d3";
|
|
7
|
+
import { colorTablesArray, colorTablesObj } from "../ColorTableTypes";
|
|
8
|
+
import discreteLegendUtil from "../Utils/discreteLegend";
|
|
9
|
+
import { color } from "d3-color";
|
|
11
10
|
|
|
12
11
|
interface legendProps {
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
position: number[];
|
|
13
|
+
colorsObject?: any;
|
|
14
|
+
legendColor?: any;
|
|
15
|
+
legendColorName?: string | undefined;
|
|
16
|
+
useContColorTable?: boolean;
|
|
17
|
+
useDiscColorTable?: boolean;
|
|
18
|
+
valueIndex?: any;
|
|
19
|
+
colorScaleData: any;
|
|
20
|
+
getColorMapname?: any | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface ItemColor {
|
|
24
|
+
color: string;
|
|
25
|
+
offset?: number;
|
|
15
26
|
}
|
|
16
27
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
var d3continuousData = d3ColorScales.filter((element: any) => {
|
|
28
|
-
return element.discrete == false;
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
d3continuousData.forEach((element: any) => {
|
|
32
|
-
continuosD3ColorData.push({color: element.colors, name: element.name})
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// code is for color table discrete data
|
|
36
|
-
var discreteData = colorTables.filter((element: any) => {
|
|
37
|
-
return element.discrete == true;
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
discreteData.forEach((element: any) => {
|
|
41
|
-
discreteColorData.push({color: element.colors, name: element.name})
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// code is for color table d3 discrete data
|
|
45
|
-
var d3discreteData = d3ColorScales.filter((element: any) => {
|
|
46
|
-
return element.discrete == true;
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
d3discreteData.forEach((element: any) => {
|
|
50
|
-
discreteD3ColorData.push({color: element.colors, name: element.name})
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
export const ColorSelector: React.FC<legendProps> = ({
|
|
54
|
-
useColorTableColors,
|
|
55
|
-
useD3Colors,
|
|
28
|
+
export const ColorSelectorComponent: React.FC<legendProps> = ({
|
|
29
|
+
// position,
|
|
30
|
+
colorsObject,
|
|
31
|
+
legendColor,
|
|
32
|
+
legendColorName,
|
|
33
|
+
useContColorTable,
|
|
34
|
+
useDiscColorTable,
|
|
35
|
+
valueIndex,
|
|
36
|
+
colorScaleData,
|
|
37
|
+
// getColorMapname,
|
|
56
38
|
}: legendProps) => {
|
|
57
|
-
let continuousLegend;
|
|
58
|
-
let discreteLegend;
|
|
59
|
-
|
|
60
|
-
if (useColorTableColors) {
|
|
61
|
-
continuousLegend = continuosColorData.map((value: any, key: any) => {
|
|
62
|
-
return <div>
|
|
63
|
-
<LegendComponent
|
|
64
|
-
position={position + key}
|
|
65
|
-
colorsObject={value}
|
|
66
|
-
legendColor={''}
|
|
67
|
-
legendColorName={''}
|
|
68
|
-
useContColorTable={true}
|
|
69
|
-
valueIndex={key}
|
|
70
|
-
/>
|
|
71
|
-
</div>
|
|
72
|
-
});
|
|
73
39
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
40
|
+
const divRef = useRef<HTMLDivElement>(null);
|
|
41
|
+
// create an array of steps based on the color scale
|
|
42
|
+
// returns an array of evenly-spaced numbers. Returns the integers from zero to the specified end minus one.
|
|
43
|
+
// d3.range(start, stop, step)
|
|
44
|
+
if (legendColor) {
|
|
45
|
+
var data = range(10).map(d=> ({color:legendColor(d/10), value:d}))
|
|
46
|
+
// get the array's min and max value
|
|
47
|
+
var extent: any = d3.extent(data, d => d.value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const handleChange = React.useCallback(() => {
|
|
51
|
+
// continous legend with colortable colors
|
|
52
|
+
if (colorsObject && Object.keys(colorsObject).length > 0 && useContColorTable) {
|
|
53
|
+
colorScaleData(colorsObject, true);
|
|
54
|
+
}
|
|
55
|
+
// continous legend with d3 colors
|
|
56
|
+
else if (data && !useContColorTable) {
|
|
57
|
+
let arrayData: any = []
|
|
58
|
+
data.forEach((colorsObject: any) => {
|
|
59
|
+
arrayData.push([0 + "." + colorsObject.value, color(colorsObject.color)?.rgb().r, color(colorsObject.color)?.rgb().g, color(colorsObject.color)?.rgb().b])
|
|
60
|
+
});
|
|
61
|
+
colorScaleData(arrayData, true);
|
|
62
|
+
}
|
|
63
|
+
// discrete legend with colortable colors
|
|
64
|
+
else if (colorsObject && Object.keys(colorsObject).length > 0 && useDiscColorTable) {
|
|
65
|
+
colorScaleData(colorsObject, false);
|
|
66
|
+
}
|
|
67
|
+
// discrete legend with d3 colors
|
|
68
|
+
else {
|
|
69
|
+
colorScaleData({colorsObject, legendColorName}, false);
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
// continuous legend using color table colors
|
|
75
|
+
if (useContColorTable == true && divRef.current) {
|
|
76
|
+
contColortableLegend();
|
|
77
|
+
return function cleanup() {
|
|
78
|
+
select(divRef.current).select("svg").remove();
|
|
79
|
+
select(divRef.current).select("div").remove();
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// discrete legend using color table colors
|
|
83
|
+
if (useDiscColorTable == true && divRef.current) {
|
|
84
|
+
discColorTableLegend();
|
|
85
|
+
return function cleanup() {
|
|
86
|
+
select(divRef.current).select("div").remove();
|
|
87
|
+
select(divRef.current).select("svg").remove();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// discrete legend using d3 colors
|
|
91
|
+
if (useDiscColorTable == false && divRef.current) {
|
|
92
|
+
discD3legend();
|
|
93
|
+
return function cleanup() {
|
|
94
|
+
select(divRef.current).select("div").remove();
|
|
95
|
+
select(divRef.current).select("svg").remove();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// continuous legend using d3 colors
|
|
99
|
+
else if (useContColorTable == false && divRef.current) {
|
|
100
|
+
contD3Legend();
|
|
101
|
+
return function cleanup() {
|
|
102
|
+
select(divRef.current).select("svg").remove();
|
|
103
|
+
select(divRef.current).select("div").remove();
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}, [useContColorTable]);
|
|
107
|
+
|
|
108
|
+
// continuous legend using color table colors (using linear gradiend)
|
|
109
|
+
function contColortableLegend() {
|
|
110
|
+
const itemColor: ItemColor[] = [];
|
|
111
|
+
|
|
112
|
+
colorsObject.color.forEach((value: [number, number, number, number]) => {
|
|
113
|
+
// return the color and offset needed to draw the legend
|
|
114
|
+
itemColor.push({
|
|
115
|
+
offset: RGBToHex(value).offset,
|
|
116
|
+
color: RGBToHex(value).color,
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// append a defs (for definition) element to your SVG
|
|
121
|
+
const svgLegend = select(divRef.current)
|
|
122
|
+
.append("svg")
|
|
123
|
+
.style("height", "30px")
|
|
124
|
+
|
|
125
|
+
const defs = svgLegend.append("defs");
|
|
126
|
+
let currentIndex = "linear-gradient-" + valueIndex;
|
|
127
|
+
// append a linearGradient element to the defs and give it a unique id
|
|
128
|
+
const linearGradient = defs
|
|
129
|
+
.append("linearGradient")
|
|
130
|
+
.attr("id", currentIndex)
|
|
131
|
+
.attr("x1", "0%")
|
|
132
|
+
.attr("x2", "100%") //since it's a horizontal linear gradient
|
|
133
|
+
.attr("y1", "0%")
|
|
134
|
+
.attr("y2", "0%");
|
|
135
|
+
// append multiple color stops by using D3's data/enter step
|
|
136
|
+
|
|
137
|
+
linearGradient
|
|
138
|
+
.selectAll("stop")
|
|
139
|
+
.data(itemColor)
|
|
140
|
+
.enter()
|
|
141
|
+
.append("stop")
|
|
142
|
+
.attr("offset", function (data) {
|
|
143
|
+
return data.offset + "%";
|
|
144
|
+
})
|
|
145
|
+
.attr("stop-color", function (data) {
|
|
146
|
+
return data.color;
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// append title
|
|
150
|
+
svgLegend
|
|
151
|
+
.append("text")
|
|
152
|
+
.attr("id", "legendTitle")
|
|
153
|
+
.attr("x", 0)
|
|
154
|
+
.attr("y", 15)
|
|
155
|
+
.style("font-size", "smaller")
|
|
156
|
+
.style("font-weight", "900")
|
|
157
|
+
// .attr("textLength","5em")
|
|
158
|
+
.text(colorsObject.name);
|
|
159
|
+
|
|
160
|
+
// draw the rectangle and fill with gradient
|
|
161
|
+
svgLegend
|
|
162
|
+
.append("rect")
|
|
163
|
+
.attr("x", 85)
|
|
164
|
+
.attr("y", 5)
|
|
165
|
+
.attr("width", "110")
|
|
166
|
+
.attr("height", 20)
|
|
167
|
+
.style("fill", "url(#"+currentIndex+")");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// continuous legend using d3 colors (using linear gradiend)
|
|
171
|
+
function contD3Legend() {
|
|
172
|
+
const itemColor: any = [];
|
|
173
|
+
|
|
174
|
+
d3ColorScales.forEach((value: any) => {
|
|
175
|
+
// return the color and offset needed to draw the legend
|
|
176
|
+
itemColor.push(value.colors);
|
|
92
177
|
});
|
|
93
178
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
179
|
+
// append a defs (for definition) element to your SVG
|
|
180
|
+
const svgLegend = select(divRef.current)
|
|
181
|
+
.append("svg")
|
|
182
|
+
.style("height", "50px")
|
|
183
|
+
|
|
184
|
+
const defs = svgLegend.append("defs");
|
|
185
|
+
let currentIndex = "linear-gradient-" + valueIndex;
|
|
186
|
+
// append a linearGradient element to the defs and give it a unique id
|
|
187
|
+
const linearGradient = defs
|
|
188
|
+
.append("linearGradient")
|
|
189
|
+
.attr("id", currentIndex)
|
|
190
|
+
.attr("x1", "0%")
|
|
191
|
+
.attr("x2", "100%") //since it's a horizontal linear gradient
|
|
192
|
+
.attr("y1", "0%")
|
|
193
|
+
.attr("y2", "0%");
|
|
194
|
+
|
|
195
|
+
// append multiple color stops by using D3's data/enter step
|
|
196
|
+
linearGradient.selectAll("stop")
|
|
197
|
+
.data(data)
|
|
198
|
+
.enter().append("stop")
|
|
199
|
+
.attr("offset", d => ((d.value - extent[0]) / (extent[1] - extent[0]) * 100) + "%")
|
|
200
|
+
.attr("stop-color", d => d.color);
|
|
201
|
+
|
|
202
|
+
// append title
|
|
203
|
+
svgLegend
|
|
204
|
+
.append("text")
|
|
205
|
+
.attr("id", "legendTitle")
|
|
206
|
+
.attr("x", 0)
|
|
207
|
+
.attr("y", 15)
|
|
208
|
+
.style("font-size", "smaller")
|
|
209
|
+
.style("font-weight", "900")
|
|
210
|
+
.text(legendColorName ? legendColorName : '');
|
|
211
|
+
|
|
212
|
+
// draw the rectangle and fill with gradient
|
|
213
|
+
svgLegend
|
|
214
|
+
.append("rect")
|
|
215
|
+
.attr("x", 85)
|
|
216
|
+
.attr("y", 5)
|
|
217
|
+
.attr("width", "110")
|
|
218
|
+
.attr("height", 20)
|
|
219
|
+
.style("fill", "url(#"+currentIndex+")");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// discrete legend using color table colors
|
|
223
|
+
function discColorTableLegend() {
|
|
224
|
+
const itemColor: ItemColor[] = [];
|
|
225
|
+
const itemName: any = [];
|
|
226
|
+
|
|
227
|
+
colorsObject.color.forEach((element: any, key: any) => {
|
|
228
|
+
itemColor.push({color: RGBToHex(element)});
|
|
229
|
+
itemName.push(key);
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
function RGBToHex(rgb: number[]) {
|
|
233
|
+
let r = rgb[1].toString(16),
|
|
234
|
+
g = rgb[2].toString(16),
|
|
235
|
+
b = rgb[3].toString(16);
|
|
236
|
+
if (r.length == 1) r = "0" + r;
|
|
237
|
+
if (g.length == 1) g = "0" + g;
|
|
238
|
+
if (b.length == 1) b = "0" + b;
|
|
239
|
+
return "#" + r + g + b;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
//const ordinalValues = scaleOrdinal().domain(itemName);
|
|
243
|
+
let count = itemName.length
|
|
244
|
+
const colorLegend = discreteLegendUtil(itemColor, true)
|
|
245
|
+
//const calcLegendHeight = 22 * itemColor.length + 4 * itemColor.length;
|
|
246
|
+
const selectedLegend = select(divRef.current);
|
|
247
|
+
// colorname as label for the legend (ex: facies)
|
|
248
|
+
selectedLegend
|
|
249
|
+
.append("label")
|
|
250
|
+
.text(legendColorName ? legendColorName : '')
|
|
251
|
+
.attr("y", 7)
|
|
252
|
+
.style("float", "left")
|
|
253
|
+
.style("width", "15px")
|
|
254
|
+
.style("margin", "10px 0px")
|
|
255
|
+
.style("white-space", "nowrap")
|
|
256
|
+
.style("font-size", "smaller")
|
|
257
|
+
.style("font-weight", "900")
|
|
258
|
+
|
|
259
|
+
selectedLegend
|
|
260
|
+
.append("svg")
|
|
261
|
+
.style("margin-top", "13px")
|
|
262
|
+
.style("margin-left", "70px")
|
|
263
|
+
.attr("viewBox", `0 0 ${count} 1`)
|
|
264
|
+
.attr("preserveAspectRatio", "none")
|
|
265
|
+
|
|
266
|
+
.style("width", "109px")
|
|
267
|
+
.style("height", "20px")
|
|
268
|
+
.call(colorLegend);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// discrete legend using d3 colors
|
|
272
|
+
function discD3legend() {
|
|
273
|
+
const itemName: any = [];
|
|
274
|
+
const itemColor: any = [];
|
|
275
|
+
colorsObject.forEach((element: any, key: any) => {
|
|
276
|
+
itemColor.push({color: element});
|
|
277
|
+
itemName.push(key);
|
|
100
278
|
});
|
|
279
|
+
//const ordinalValues = scaleOrdinal(colorsObject).domain(itemName);
|
|
280
|
+
let count = itemName.length
|
|
281
|
+
const discreteLegend = discreteLegendUtil(itemColor, true);
|
|
282
|
+
select("svg").append("g").attr("transform", "translate(50,70)").attr("class", "legend").call(discreteLegend);
|
|
283
|
+
const selectedLegend = select(divRef.current);
|
|
284
|
+
selectedLegend
|
|
285
|
+
.append("text")
|
|
286
|
+
.text(legendColorName ? legendColorName : '')
|
|
287
|
+
.attr("y", 7)
|
|
288
|
+
.style("float", "left")
|
|
289
|
+
.style("width", "15px")
|
|
290
|
+
.style("margin", "10px 0px")
|
|
291
|
+
.style("font-size", "smaller")
|
|
292
|
+
.style("font-weight", "900");
|
|
293
|
+
selectedLegend
|
|
294
|
+
.append("svg")
|
|
295
|
+
.style("margin-top", "13px")
|
|
296
|
+
.style("margin-left", "70px")
|
|
297
|
+
.style("background", "grey")
|
|
298
|
+
.attr("viewBox", `0 0 ${count} 1`)
|
|
299
|
+
.attr("preserveAspectRatio", "none")
|
|
300
|
+
|
|
301
|
+
.style("width", "109px")
|
|
302
|
+
.style("height", "20px")
|
|
303
|
+
.call(discreteLegend);
|
|
101
304
|
}
|
|
102
305
|
|
|
103
306
|
return (
|
|
104
307
|
<div>
|
|
105
|
-
{
|
|
106
|
-
|
|
308
|
+
<div id="legend" style={{height: 30}}
|
|
309
|
+
ref={divRef} onClick={handleChange}></div>
|
|
107
310
|
</div>
|
|
108
311
|
);
|
|
109
|
-
};
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// Based on name return the colors array from color.tables.json file
|
|
315
|
+
export function colorTableData(
|
|
316
|
+
colorName: string,
|
|
317
|
+
colorTables: colorTablesArray
|
|
318
|
+
): [number, number, number, number][] {
|
|
319
|
+
const colorTableData = colorTables.filter(
|
|
320
|
+
(value: colorTablesObj) =>
|
|
321
|
+
value.name.toLowerCase() == colorName.toLowerCase()
|
|
322
|
+
);
|
|
323
|
+
return colorTableData.length > 0 ? colorTableData[0].colors : [];
|
|
324
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as colorTables from "../color-tables.json";
|
|
3
|
+
import {d3ColorScales} from "../Utils/d3ColorScale";
|
|
4
|
+
import {ColorSelectorComponent} from "./ColorSelectorComponent";
|
|
5
|
+
|
|
6
|
+
const position = [16, 206];
|
|
7
|
+
const continuosColorData: any = [];
|
|
8
|
+
const continuosD3ColorData: any = [];
|
|
9
|
+
const discreteColorData: any = [];
|
|
10
|
+
const discreteD3ColorData: any = [];
|
|
11
|
+
|
|
12
|
+
interface legendProps {
|
|
13
|
+
useColorTableColors: boolean,
|
|
14
|
+
useD3Colors: boolean,
|
|
15
|
+
colorScaleObject: any
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Continuous legend using color table data
|
|
19
|
+
var colorTableContinuousData = colorTables.filter((element: any) => {
|
|
20
|
+
return element.discrete == false;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
colorTableContinuousData.forEach((element: any) => {
|
|
24
|
+
continuosColorData.push({color: element.colors, name: element.name});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Continuous legend using d3 data
|
|
28
|
+
var d3continuousData = d3ColorScales.filter((element: any) => {
|
|
29
|
+
return element.discrete == false;
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
d3continuousData.forEach((element: any) => {
|
|
33
|
+
continuosD3ColorData.push({color: element.colors, name: element.name});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Discrete legend using color table data
|
|
37
|
+
var discreteData = colorTables.filter((element: any) => {
|
|
38
|
+
return element.discrete == true;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
discreteData.forEach((element: any) => {
|
|
42
|
+
discreteColorData.push({color: element.colors, name: element.name});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Discrete legend using d3 data
|
|
46
|
+
var d3discreteData = d3ColorScales.filter((element: any) => {
|
|
47
|
+
return element.discrete == true;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
d3discreteData.forEach((element: any) => {
|
|
51
|
+
discreteD3ColorData.push({color: element.colors, name: element.name});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export const ColorSelectorWrapper: React.FC<legendProps> = ({
|
|
55
|
+
useColorTableColors,
|
|
56
|
+
useD3Colors,
|
|
57
|
+
colorScaleObject,
|
|
58
|
+
}: legendProps) => {
|
|
59
|
+
let continuousLegend;
|
|
60
|
+
let discreteLegend;
|
|
61
|
+
|
|
62
|
+
// return continuous and discrete legend which uses colortable data
|
|
63
|
+
if (useColorTableColors) {
|
|
64
|
+
continuousLegend = continuosColorData.map((value: any, key: any) => {
|
|
65
|
+
return <div>
|
|
66
|
+
<ColorSelectorComponent
|
|
67
|
+
position={position + key}
|
|
68
|
+
colorsObject={value}
|
|
69
|
+
useContColorTable={true}
|
|
70
|
+
valueIndex={key}
|
|
71
|
+
colorScaleData={colorScaleObject.colorScaleObject}
|
|
72
|
+
/>
|
|
73
|
+
</div>
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
discreteLegend = discreteColorData.map((val: any, key: any) => {
|
|
77
|
+
return <ColorSelectorComponent
|
|
78
|
+
position={position + key}
|
|
79
|
+
colorsObject={discreteColorData[key]}
|
|
80
|
+
legendColorName={val.name}
|
|
81
|
+
useDiscColorTable={true}
|
|
82
|
+
colorScaleData={colorScaleObject.colorScaleObject}
|
|
83
|
+
/>
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// return continuous and discrete legend which uses d3 data
|
|
87
|
+
if (useD3Colors) {
|
|
88
|
+
continuousLegend = continuosD3ColorData.map((val: any, key: any) => {
|
|
89
|
+
return <ColorSelectorComponent
|
|
90
|
+
position={position + key}
|
|
91
|
+
legendColor={val.color}
|
|
92
|
+
legendColorName={val.name}
|
|
93
|
+
useContColorTable={false}
|
|
94
|
+
valueIndex={key + "0"}
|
|
95
|
+
colorScaleData={colorScaleObject.colorScaleObject}
|
|
96
|
+
/>
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
discreteLegend = d3discreteData.map((val: any, key: any) => {
|
|
100
|
+
return <ColorSelectorComponent
|
|
101
|
+
position={position + key}
|
|
102
|
+
colorsObject={val.colors}
|
|
103
|
+
legendColorName={val.name}
|
|
104
|
+
useDiscColorTable={false}
|
|
105
|
+
colorScaleData={colorScaleObject.colorScaleObject}
|
|
106
|
+
/>
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div className="legendWrapper"
|
|
112
|
+
style={{height: 200, overflow: "auto", display: "flex", flexDirection: "column", overflowX: "hidden"}}
|
|
113
|
+
>
|
|
114
|
+
{continuousLegend}
|
|
115
|
+
{discreteLegend}
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { DiscreteColorLegend } from "./DiscreteLegend";
|
|
3
|
+
import { ContinuousLegend } from "./ContinuousLegend";
|
|
4
|
+
import {useCallback} from "react";
|
|
5
|
+
import { ColorSelectorAccordion } from "../ColorSelector/ColorSelectorAccordion";
|
|
6
|
+
|
|
7
|
+
interface ColorLegendProps {
|
|
8
|
+
colorTables: any;
|
|
9
|
+
min: number;
|
|
10
|
+
max: number;
|
|
11
|
+
dataObjectName: string;
|
|
12
|
+
position?: number[] | null;
|
|
13
|
+
colorName: string;
|
|
14
|
+
horizontal?: boolean | null;
|
|
15
|
+
discreteData: any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Todo: Adapt it for other layers too
|
|
19
|
+
const ColorLegend: React.FC<ColorLegendProps> = ({
|
|
20
|
+
position,
|
|
21
|
+
horizontal,
|
|
22
|
+
colorTables,
|
|
23
|
+
min,
|
|
24
|
+
max,
|
|
25
|
+
dataObjectName,
|
|
26
|
+
colorName,
|
|
27
|
+
discreteData
|
|
28
|
+
}: ColorLegendProps) => {
|
|
29
|
+
|
|
30
|
+
const [isToggled, setIsToggled] = React.useState(false);
|
|
31
|
+
const handleClick = useCallback(() => {
|
|
32
|
+
setIsToggled(true);
|
|
33
|
+
}, []);
|
|
34
|
+
|
|
35
|
+
const [updateLegend, setUpdateLegendColor] = React.useState([] as any);
|
|
36
|
+
const [isCont, setIsCont] = React.useState(true);
|
|
37
|
+
|
|
38
|
+
// Get new colorscale from colorselector and update legend
|
|
39
|
+
const colorScaleObject = React.useCallback((data: any, value: any) => {
|
|
40
|
+
setUpdateLegendColor(data);
|
|
41
|
+
setIsCont(value);
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div>
|
|
46
|
+
<div onClick={handleClick}>
|
|
47
|
+
{isCont && (
|
|
48
|
+
<ContinuousLegend
|
|
49
|
+
min={min}
|
|
50
|
+
max={max}
|
|
51
|
+
dataObjectName={dataObjectName}
|
|
52
|
+
position={position}
|
|
53
|
+
colorName={colorName}
|
|
54
|
+
colorTables={colorTables}
|
|
55
|
+
horizontal={horizontal}
|
|
56
|
+
updateLegend={updateLegend}
|
|
57
|
+
/>
|
|
58
|
+
)}
|
|
59
|
+
{!isCont && (
|
|
60
|
+
<DiscreteColorLegend
|
|
61
|
+
discreteData={discreteData}
|
|
62
|
+
dataObjectName={dataObjectName}
|
|
63
|
+
position={position}
|
|
64
|
+
colorName={colorName}
|
|
65
|
+
colorTables={colorTables}
|
|
66
|
+
horizontal={horizontal}
|
|
67
|
+
updateLegend={updateLegend}
|
|
68
|
+
/>
|
|
69
|
+
)}
|
|
70
|
+
</div>
|
|
71
|
+
<div>
|
|
72
|
+
{isToggled && (
|
|
73
|
+
<ColorSelectorAccordion colorScaleObject={colorScaleObject} />
|
|
74
|
+
)}
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export default ColorLegend;
|