@emerson-eps/color-tables 0.2.3 → 0.2.6

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