@emerson-eps/color-tables 0.1.22 → 0.2.2

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 (69) hide show
  1. package/dist/component/ColorSelector/ColorSelectorComponent.d.ts +7 -0
  2. package/dist/component/ColorSelector/ColorSelectorComponent.js +62 -0
  3. package/dist/component/ColorSelector/ColorSelectorComponent.js.map +1 -0
  4. package/dist/component/ColorSelector/ColorTableSelectorWrapper.d.ts +2 -0
  5. package/dist/component/ColorSelector/ColorTableSelectorWrapper.js +25 -0
  6. package/dist/component/ColorSelector/ColorTableSelectorWrapper.js.map +1 -0
  7. package/dist/component/ColorSelector/legendComponent.d.ts +14 -0
  8. package/dist/component/ColorSelector/legendComponent.js +228 -0
  9. package/dist/component/ColorSelector/legendComponent.js.map +1 -0
  10. package/dist/component/Legend/ContinuousLegend.d.ts +1 -1
  11. package/dist/component/Legend/ContinuousLegend.js +84 -65
  12. package/dist/component/Legend/ContinuousLegend.js.map +1 -1
  13. package/dist/component/Legend/DiscreteLegend.d.ts +1 -1
  14. package/dist/component/Legend/DiscreteLegend.js +71 -52
  15. package/dist/component/Legend/DiscreteLegend.js.map +1 -1
  16. package/dist/component/Utils/d3ColorScale.d.ts +10 -0
  17. package/dist/component/Utils/d3ColorScale.js +84 -0
  18. package/dist/component/Utils/d3ColorScale.js.map +1 -0
  19. package/dist/component/Utils/discreteLegend.d.ts +2 -2
  20. package/dist/component/Utils/discreteLegend.js +26 -15
  21. package/dist/component/Utils/discreteLegend.js.map +1 -1
  22. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.d.ts +7 -0
  23. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js +11 -0
  24. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js.map +1 -0
  25. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/MD.stories.d.ts +0 -0
  26. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/MD.stories.js +0 -2
  27. package/dist/stories/ColorLegends/ContinousLegends/MD.stories.js.map +1 -0
  28. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/NTG.stories.d.ts +0 -0
  29. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/NTG.stories.js +0 -2
  30. package/dist/stories/ColorLegends/ContinousLegends/NTG.stories.js.map +1 -0
  31. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PermTot.stories.d.ts +0 -0
  32. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PermTot.stories.js +0 -2
  33. package/dist/stories/ColorLegends/ContinousLegends/PermTot.stories.js.map +1 -0
  34. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Permeability.stories.d.ts +0 -0
  35. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Permeability.stories.js +0 -2
  36. package/dist/stories/ColorLegends/ContinousLegends/Permeability.stories.js.map +1 -0
  37. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PoroTot.stories.d.ts +0 -0
  38. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PoroTot.stories.js +0 -2
  39. package/dist/stories/ColorLegends/ContinousLegends/PoroTot.stories.js.map +1 -0
  40. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Porosity.stories.d.ts +0 -0
  41. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Porosity.stories.js +0 -2
  42. package/dist/stories/ColorLegends/ContinousLegends/Porosity.stories.js.map +1 -0
  43. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/SW.stories.d.ts +0 -0
  44. package/dist/stories/ColorLegends/{ContinousLgends → ContinousLegends}/SW.stories.js +2 -4
  45. package/dist/stories/ColorLegends/ContinousLegends/SW.stories.js.map +1 -0
  46. package/package.json +3 -1
  47. package/src/component/ColorSelector/ColorSelectorComponent.tsx +109 -0
  48. package/src/component/ColorSelector/ColorTableSelectorWrapper.tsx +49 -0
  49. package/src/component/ColorSelector/legendComponent.tsx +289 -0
  50. package/src/component/Legend/ContinuousLegend.tsx +12 -3
  51. package/src/component/Legend/DiscreteLegend.tsx +14 -6
  52. package/src/component/Utils/d3ColorScale.ts +84 -0
  53. package/src/component/Utils/discreteLegend.ts +34 -23
  54. package/src/stories/ColorLegends/ColorSelector/Selector.stories.jsx +12 -0
  55. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/MD.stories.jsx +0 -2
  56. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/NTG.stories.jsx +0 -2
  57. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PermTot.stories.jsx +0 -2
  58. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Permeability.stories.jsx +0 -2
  59. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/PoroTot.stories.jsx +0 -2
  60. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/Porosity.stories.jsx +0 -2
  61. package/src/stories/ColorLegends/{ContinousLgends → ContinousLegends}/SW.stories.jsx +2 -4
  62. package/tsconfig.json +1 -1
  63. package/dist/stories/ColorLegends/ContinousLgends/MD.stories.js.map +0 -1
  64. package/dist/stories/ColorLegends/ContinousLgends/NTG.stories.js.map +0 -1
  65. package/dist/stories/ColorLegends/ContinousLgends/PermTot.stories.js.map +0 -1
  66. package/dist/stories/ColorLegends/ContinousLgends/Permeability.stories.js.map +0 -1
  67. package/dist/stories/ColorLegends/ContinousLgends/PoroTot.stories.js.map +0 -1
  68. package/dist/stories/ColorLegends/ContinousLgends/Porosity.stories.js.map +0 -1
  69. package/dist/stories/ColorLegends/ContinousLgends/SW.stories.js.map +0 -1
@@ -0,0 +1,109 @@
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
15
+ }
16
+
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,
56
+ }: 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
+
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
+ />
92
+ });
93
+
94
+ discreteLegend = d3discreteData.map((val: any) => {
95
+ return <LegendComponent
96
+ legendColorName={val.name}
97
+ position={position}
98
+ colorsObject={val.colors}
99
+ useDiscColorTable={false} />
100
+ });
101
+ }
102
+
103
+ return (
104
+ <div>
105
+ {continuousLegend}
106
+ {discreteLegend}
107
+ </div>
108
+ );
109
+ };
@@ -0,0 +1,49 @@
1
+ import * as React from "react";
2
+ import { Accordion } from "@equinor/eds-core-react";
3
+ import { ColorSelector } from "./ColorSelectorComponent";
4
+
5
+ export const ColorSelectorWrapper = () => {
6
+ return (
7
+ <div style={{width: "650px"}}>
8
+ <Accordion >
9
+ <Accordion.Item>
10
+ <Accordion.Header>
11
+ Color Scales
12
+ </Accordion.Header>
13
+ <Accordion.Panel>
14
+ <Accordion >
15
+ <Accordion.Item>
16
+ <Accordion.Header>
17
+ Geologic Color Scale
18
+ </Accordion.Header>
19
+ <Accordion.Panel>
20
+ <ColorSelector useColorTableColors={true} useD3Colors={false}/>
21
+ </Accordion.Panel>
22
+ </Accordion.Item>
23
+ </Accordion>
24
+ <Accordion >
25
+ <Accordion.Item>
26
+ <Accordion.Header>
27
+ D3 Color Scale
28
+ </Accordion.Header>
29
+ <Accordion.Panel>
30
+ <ColorSelector useColorTableColors={false} useD3Colors={true}/>
31
+ </Accordion.Panel>
32
+ </Accordion.Item>
33
+ </Accordion>
34
+ <Accordion >
35
+ <Accordion.Item>
36
+ <Accordion.Header>
37
+ Custom Color Scale
38
+ </Accordion.Header>
39
+ <Accordion.Panel>
40
+ Custom Content
41
+ </Accordion.Panel>
42
+ </Accordion.Item>
43
+ </Accordion>
44
+ </Accordion.Panel>
45
+ </Accordion.Item>
46
+ </Accordion>
47
+ </div>
48
+ );
49
+ };
@@ -0,0 +1,289 @@
1
+ import * as React from "react";
2
+ import { useRef } from "react";
3
+ import { RGBToHex } from "../Utils/continousLegend";
4
+ import {d3ColorScales} from "../Utils/d3ColorScale"
5
+ import * as d3 from "d3";
6
+ import { scaleOrdinal, select } from "d3";
7
+ import { colorTablesArray, colorTablesObj } from "../ColorTableTypes";
8
+ import discreteLegendUtil from "../Utils/discreteLegend";
9
+
10
+ interface legendProps {
11
+ position: number[];
12
+ colorsObject?: any;
13
+ legendColor?: any;
14
+ legendColorName: string;
15
+ useContColorTable?: boolean;
16
+ useDiscColorTable?: boolean;
17
+ valueIndex?: any;
18
+ }
19
+
20
+ interface ItemColor {
21
+ color: string;
22
+ offset?: number;
23
+ }
24
+
25
+ export const LegendComponent: React.FC<legendProps> = ({
26
+ position,
27
+ colorsObject,
28
+ legendColor,
29
+ legendColorName,
30
+ useContColorTable,
31
+ useDiscColorTable,
32
+ valueIndex,
33
+ }: legendProps) => {
34
+
35
+ const divRef = useRef<HTMLDivElement>(null);
36
+
37
+ React.useEffect(() => {
38
+ // continuous legend using color table colors
39
+ if (useContColorTable == true && divRef.current) {
40
+ contColortableLegend();
41
+ return function cleanup() {
42
+ d3.select(divRef.current).select("svg").remove();
43
+ };
44
+ }
45
+ // discrete legend using color table colors
46
+ if (useDiscColorTable == true && divRef.current) {
47
+ discColorTableLegend();
48
+ return function cleanup() {
49
+ select(divRef.current).select("div").remove();
50
+ select(divRef.current).select("svg").remove();
51
+ };
52
+ }
53
+ // discrete legend using d3 colors
54
+ if (useDiscColorTable == false && divRef.current) {
55
+ discD3legend();
56
+ return function cleanup() {
57
+ select(divRef.current).select("div").remove();
58
+ select(divRef.current).select("svg").remove();
59
+ }
60
+ }
61
+ // continuous legend using d3 colors
62
+ else if (useContColorTable == false && divRef.current) {
63
+ contD3Legend();
64
+ return function cleanup() {
65
+ d3.select(divRef.current).select("svg").remove();
66
+ };
67
+ }
68
+ }, [useContColorTable]);
69
+
70
+ // continuous legend using color table colors (linear gradiend code)
71
+ function contColortableLegend() {
72
+ const itemColor: ItemColor[] = [];
73
+
74
+ colorsObject.color.forEach((value: [number, number, number, number]) => {
75
+ // return the color and offset needed to draw the legend
76
+ itemColor.push({
77
+ offset: RGBToHex(value).offset,
78
+ color: RGBToHex(value).color,
79
+ });
80
+ });
81
+
82
+ // append a defs (for definition) element to your SVG
83
+ const svgLegend = d3.select(divRef.current)
84
+ .append("svg")
85
+ .style("height", "50px")
86
+ .style("display", "flex")
87
+
88
+ const defs = svgLegend.append("defs");
89
+ let currentIndex = "linear-gradient-" + valueIndex;
90
+ // append a linearGradient element to the defs and give it a unique id
91
+ const linearGradient = defs
92
+ .append("linearGradient")
93
+ .attr("id", currentIndex)
94
+ .attr("x1", "0%")
95
+ .attr("x2", "100%") //since it's a horizontal linear gradient
96
+ .attr("y1", "0%")
97
+ .attr("y2", "0%");
98
+ // append multiple color stops by using D3's data/enter step
99
+
100
+ linearGradient
101
+ .selectAll("stop")
102
+ .data(itemColor)
103
+ .enter()
104
+ .append("stop")
105
+ .attr("offset", function (data) {
106
+ return data.offset + "%";
107
+ })
108
+ .attr("stop-color", function (data) {
109
+ return data.color;
110
+ });
111
+
112
+ // append title
113
+ svgLegend
114
+ .append("text")
115
+ .attr("class", "legendTitle")
116
+ .attr("x", 0)
117
+ .attr("y", 43)
118
+ .style("text-anchor", "left")
119
+ .text(colorsObject.name);
120
+
121
+ // draw the rectangle and fill with gradient
122
+ svgLegend
123
+ .append("rect")
124
+ .attr("x", 180)
125
+ .attr("y", 30)
126
+ .attr("width", 150)
127
+ .attr("height", 25)
128
+ .style("fill", "url(#"+currentIndex+")");
129
+ }
130
+
131
+ // continuous legend using d3 colors (linear gradiend code)
132
+ function contD3Legend() {
133
+ const itemColor: any = [];
134
+
135
+ d3ColorScales.forEach((value: any) => {
136
+ // return the color and offset needed to draw the legend
137
+ itemColor.push(value.colors);
138
+ });
139
+
140
+ // create an array of steps based on the color scale
141
+ // returns an array of evenly-spaced numbers. Returns the integers from zero to the specified end minus one.
142
+ // d3.range(start, stop, step)
143
+ var data = d3.range(10).map(d=> ({color:legendColor(d/10), value:d}))
144
+ // get the array's min and max value
145
+ var extent: any = d3.extent(data, d => d.value);
146
+
147
+ // append a defs (for definition) element to your SVG
148
+ const svgLegend = d3.select(divRef.current)
149
+ .append("svg")
150
+ .style("height", "50px")
151
+ .style("display", "flex")
152
+
153
+ const defs = svgLegend.append("defs");
154
+ let currentIndex = "linear-gradient-" + valueIndex;
155
+ // append a linearGradient element to the defs and give it a unique id
156
+ const linearGradient = defs
157
+ .append("linearGradient")
158
+ .attr("id", currentIndex)
159
+ .attr("x1", "0%")
160
+ .attr("x2", "100%") //since it's a horizontal linear gradient
161
+ .attr("y1", "0%")
162
+ .attr("y2", "0%");
163
+
164
+ // append multiple color stops by using D3's data/enter step
165
+ linearGradient.selectAll("stop")
166
+ .data(data)
167
+ .enter().append("stop")
168
+ .attr("offset", d => ((d.value - extent[0]) / (extent[1] - extent[0]) * 100) + "%")
169
+ .attr("stop-color", d => d.color);
170
+
171
+ // append title
172
+ svgLegend
173
+ .append("text")
174
+ .attr("class", "legendTitle")
175
+ .attr("x", 0)
176
+ .attr("y", 43)
177
+ .style("text-anchor", "left")
178
+ .text(legendColorName);
179
+
180
+
181
+ // draw the rectangle and fill with gradient
182
+ svgLegend
183
+ .append("rect")
184
+ .attr("x", 180)
185
+ .attr("y", 30)
186
+ .attr("width", 150)
187
+ .attr("height", 25)
188
+ .style("fill", "url(#"+currentIndex+")");
189
+ }
190
+
191
+ // discrete legend using color table colors
192
+ function discColorTableLegend() {
193
+ const itemColor: ItemColor[] = [];
194
+ const itemName: any = [];
195
+
196
+ colorsObject.color.forEach((element: any, key: any) => {
197
+ itemColor.push({color: RGBToHex(element)});
198
+ itemName.push(key);
199
+ })
200
+
201
+ function RGBToHex(rgb: number[]) {
202
+ let r = rgb[1].toString(16),
203
+ g = rgb[2].toString(16),
204
+ b = rgb[3].toString(16);
205
+ if (r.length == 1) r = "0" + r;
206
+ if (g.length == 1) g = "0" + g;
207
+ if (b.length == 1) b = "0" + b;
208
+ return "#" + r + g + b;
209
+ }
210
+
211
+ const ordinalValues = scaleOrdinal().domain(itemName);
212
+ const colorLegend = discreteLegendUtil(itemColor, "ColorSelector").inputScale(ordinalValues);
213
+ select("svg").append("g").attr("transform", "translate(50,70)").attr("class", "legend").call(colorLegend);
214
+ //const calcLegendHeight = 22 * itemColor.length + 4 * itemColor.length;
215
+ const selectedLegend = select(divRef.current);
216
+ selectedLegend
217
+ .append("text")
218
+ .text(legendColorName)
219
+ .attr("y", 7)
220
+ .style("float", "left")
221
+ .style("width", "30%")
222
+ .style("margin", "10px 0px");
223
+ selectedLegend
224
+ .append("svg")
225
+ .style("margin-top", "13px")
226
+ .style("margin-right", "103px")
227
+ .style("float", "right")
228
+ // .style("width", "60%")
229
+ .style("height", "25px")
230
+ .call(colorLegend);
231
+ }
232
+
233
+ // discrete legend using d3 colors
234
+ function discD3legend() {
235
+ const itemName: any = [];
236
+ const itemColor: any = [];
237
+
238
+ colorsObject.forEach((element: any, key: any) => {
239
+ itemColor.push({color: element});
240
+ itemName.push(key);
241
+ });
242
+
243
+ const ordinalValues = scaleOrdinal(colorsObject).domain(itemName);
244
+ const discreteLegend = discreteLegendUtil(itemColor, "ColorSelector").inputScale(ordinalValues);
245
+ select("svg").append("g").attr("transform", "translate(50,70)").attr("class", "legend").call(discreteLegend);
246
+ const selectedLegend = select(divRef.current);
247
+ selectedLegend
248
+ .append("text")
249
+ .text(legendColorName)
250
+ .attr("y", 7)
251
+ .style("float", "left")
252
+ .style("width", "30%")
253
+ .style("margin", "10px 0px");
254
+ selectedLegend
255
+ .append("svg")
256
+ .style("margin-top", "13px")
257
+ .style("margin-right", "103px")
258
+ .style("float", "right")
259
+ // .style("width", "60%")
260
+ .style("height", "25px")
261
+ .call(discreteLegend);
262
+ }
263
+
264
+ return (
265
+ <div
266
+ style={{
267
+ right: position[0],
268
+ top: position[1],
269
+ borderRadius: "5px",
270
+ height: "35px",
271
+ marginTop: "15px"
272
+ }}
273
+ >
274
+ <div id="legend" ref={divRef}></div>
275
+ </div>
276
+ );
277
+ };
278
+
279
+ // Based on name return the colors array from color.tables.json file
280
+ export function colorTableData(
281
+ colorName: string,
282
+ colorTables: colorTablesArray
283
+ ): [number, number, number, number][] {
284
+ const colorTableData = colorTables.filter(
285
+ (value: colorTablesObj) =>
286
+ value.name.toLowerCase() == colorName.toLowerCase()
287
+ );
288
+ return colorTableData.length > 0 ? colorTableData[0].colors : [];
289
+ }
@@ -10,7 +10,7 @@ declare type legendProps = {
10
10
  dataObjectName: string;
11
11
  position?: number[] | null;
12
12
  colorName: string;
13
- colorTables: colorTablesArray;
13
+ colorTables: colorTablesArray | string;
14
14
  horizontal?: boolean | null;
15
15
  }
16
16
 
@@ -38,10 +38,19 @@ export const ContinuousLegend: React.FC<legendProps> = ({
38
38
  };
39
39
  }, [min, max, colorName, colorTables, horizontal]);
40
40
 
41
- function continuousLegend() {
41
+ async function continuousLegend() {
42
42
  const itemColor: ItemColor[] = [];
43
+ let dataSet;
44
+
45
+ if (typeof colorTables === "string") {
46
+ let res = await fetch(colorTables);
47
+ dataSet = await res.json()
48
+ }
43
49
  // Return the matched colors array from color.tables.json file
44
- const colorTableColors = colorsArray(colorName, colorTables);
50
+ let colorTableColors = typeof colorTables === "string" ?
51
+ colorsArray(colorName, dataSet)
52
+ :
53
+ colorsArray(colorName, colorTables);
45
54
  colorTableColors.forEach((value: [number, number, number, number]) => {
46
55
  // return the color and offset needed to draw the legend
47
56
  itemColor.push({
@@ -13,7 +13,7 @@ declare type colorLegendProps = {
13
13
  dataObjectName: string;
14
14
  position?: number[] | null;
15
15
  colorName: string;
16
- colorTables: colorTablesArray;
16
+ colorTables: colorTablesArray | string;
17
17
  horizontal?: boolean | null;
18
18
  }
19
19
 
@@ -35,13 +35,21 @@ export const DiscreteColorLegend: React.FC<colorLegendProps> = ({
35
35
  select(divRef.current).select("svg").remove();
36
36
  };
37
37
  }, [discreteData, colorName, colorTables, horizontal]);
38
- function discreteLegend() {
38
+ async function discreteLegend() {
39
39
  const itemName: string[] = [];
40
40
  const itemColor: ItemColor[] = [];
41
- const colorsArray: [number, number, number, number][] = colorTableData(
42
- colorName,
43
- colorTables
44
- );
41
+ let dataSet;
42
+
43
+ if (typeof colorTables === "string") {
44
+ let res = await fetch(colorTables);
45
+ dataSet = await res.json()
46
+ }
47
+
48
+ let colorsArray = typeof colorTables === "string" ?
49
+ colorTableData(colorName, dataSet)
50
+ :
51
+ colorTableData(colorName, colorTables);
52
+
45
53
  Object.keys(discreteData).forEach((key) => {
46
54
  // eslint-disable-next-line
47
55
  let code = (discreteData as { [key: string]: any })[key][1]
@@ -0,0 +1,84 @@
1
+ import * as d3 from "d3";
2
+
3
+ export const d3ColorScales = [
4
+ {
5
+ "name": "Spectral",
6
+ "discrete": false,
7
+ "colors": d3.interpolateSpectral
8
+ },
9
+ {
10
+ "name": "RdYlBu",
11
+ "discrete": false,
12
+ "colors": d3.interpolateRdYlBu
13
+ },
14
+ {
15
+ "name": "Warm",
16
+ "discrete": false,
17
+ "colors": d3.interpolateWarm
18
+ },
19
+ {
20
+ "name": "Cool",
21
+ "discrete": false,
22
+ "colors": d3.interpolateCool
23
+ },
24
+ {
25
+ "name": "Viridis",
26
+ "discrete": false,
27
+ "colors": d3.interpolateViridis
28
+ },
29
+ {
30
+ "name": "Inferno",
31
+ "discrete": false,
32
+ "colors": d3.interpolateInferno
33
+ },
34
+ {
35
+ "name": "Accent",
36
+ "discrete": true,
37
+ "colors": d3.schemeAccent
38
+ },
39
+ {
40
+ "name": "Dark2",
41
+ "discrete": true,
42
+ "colors": d3.schemeDark2
43
+ },
44
+ {
45
+ "name": "Paired",
46
+ "discrete": true,
47
+ "colors": d3.schemePaired
48
+ },
49
+ {
50
+ "name": "Pastel1",
51
+ "discrete": true,
52
+ "colors": d3.schemePastel1
53
+ },
54
+ {
55
+ "name": "Pastel2",
56
+ "discrete": true,
57
+ "colors": d3.schemePastel2
58
+ },
59
+ {
60
+ "name": "Set1",
61
+ "discrete": true,
62
+ "colors": d3.schemeSet1
63
+ },
64
+ {
65
+ "name": "Set2",
66
+ "discrete": true,
67
+ "colors": d3.schemeSet3
68
+ },
69
+ {
70
+ "name": "Set3",
71
+ "discrete": true,
72
+ "colors": d3.schemeSet3
73
+ },
74
+ {
75
+ "name": "Tableau10",
76
+ "discrete": true,
77
+ "colors": d3.schemeTableau10
78
+ },
79
+ {
80
+ "name": "Category10",
81
+ "discrete": true,
82
+ "colors": d3.schemeCategory10
83
+ },
84
+ ]
@@ -1,12 +1,12 @@
1
1
  declare type ItemColor = {
2
- color: string;
2
+ color: any;
3
3
  }
4
4
 
5
5
  // eslint-disable-next-line
6
- export default function legendUtil(itemColor: ItemColor[]): any {
6
+ export default function discreteLegendUtil(itemColor: ItemColor[], ColorSelector?: string): any {
7
7
  let legendValues: ItemColor[] = [];
8
8
  const cellWidth = 22;
9
- const cellHeight = 22;
9
+ const cellHeight: any = 22;
10
10
  const cellPadding = 4;
11
11
 
12
12
  // eslint-disable-next-line
@@ -20,30 +20,41 @@ export default function legendUtil(itemColor: ItemColor[]): any {
20
20
  .append("rect")
21
21
  .attr("height", cellHeight)
22
22
  .attr("width", cellWidth)
23
- .style("stroke", "black")
24
- .style("stroke-width", "1px")
25
23
  .style("fill", function (d: Record<string, unknown>) {
26
24
  return d["color"];
27
25
  });
26
+ if (!ColorSelector) {
27
+ g.selectAll("g.legendCells.rect").style("stroke", "black").style("stroke-width", "1px")
28
+ }
28
29
  // Display the label
29
- g.selectAll("g.legendCells")
30
- .append("text")
31
- .attr("class", "breakLabels")
32
- .style("fill", "#6F6F6F")
33
- .attr("x", cellWidth + cellPadding)
34
- .attr("y", 5 + cellHeight / 2)
35
- .text(function (d: Record<string, unknown>) {
36
- return d["label"];
37
- });
30
+ if (!ColorSelector) {
31
+ g.selectAll("g.legendCells")
32
+ .append("text")
33
+ .attr("class", "breakLabels")
34
+ .style("fill", "#6F6F6F")
35
+ .attr("x", cellWidth + cellPadding)
36
+ .attr("y", 5 + cellHeight / 2)
37
+ .text(function (d: Record<string, unknown>) {
38
+ return d["label"];
39
+ });
40
+ }
38
41
  // Alighment of cell in straight line
39
- g.selectAll("g.legendCells").attr(
40
- "transform",
41
- function (_d: Record<string, unknown>, i: number) {
42
- return (
43
- "translate(0," + i * (cellHeight + cellPadding) + ")"
44
- );
45
- }
46
- );
42
+ if (!ColorSelector) {
43
+ g.selectAll("g.legendCells").attr(
44
+ "transform",
45
+ function (_d: Record<string, unknown>, i: number) {
46
+ return (
47
+ "translate(0," + i * (cellHeight + cellPadding) + ")"
48
+ );
49
+ }
50
+ );
51
+ }
52
+ if (ColorSelector) {
53
+ g.selectAll("g.legendCells")
54
+ .attr("transform", function(d: any) {
55
+ return "translate(" + (d.label * cellWidth) + ",0)" }
56
+ );
57
+ }
47
58
  }
48
59
  // display the discrete legend
49
60
  g.selectAll("g.legendCells")
@@ -66,4 +77,4 @@ export default function legendUtil(itemColor: ItemColor[]): any {
66
77
  return this;
67
78
  };
68
79
  return legend;
69
- }
80
+ }