@emerson-eps/color-tables 0.2.13 → 0.2.14

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 (110) hide show
  1. package/dist/component/ColorSelector/ColorSelectorAccordion.js +2 -2
  2. package/dist/component/ColorSelector/ColorSelectorAccordion.js.map +1 -1
  3. package/dist/component/ColorSelector/ColorSelectorComponent.d.ts +1 -2
  4. package/dist/component/ColorSelector/ColorSelectorComponent.js +3 -3
  5. package/dist/component/ColorSelector/ColorSelectorComponent.js.map +1 -1
  6. package/dist/component/ColorSelector/ColorSelectorWrapper.d.ts +6 -1
  7. package/dist/component/ColorSelector/ColorSelectorWrapper.js +7 -8
  8. package/dist/component/ColorSelector/ColorSelectorWrapper.js.map +1 -1
  9. package/dist/component/Legend/ColorLegend.js +2 -2
  10. package/dist/component/Legend/ColorLegend.js.map +1 -1
  11. package/dist/component/Legend/ContinuousLegend.d.ts +1 -1
  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 +1 -1
  15. package/dist/component/Legend/DiscreteLegend.js.map +1 -1
  16. package/dist/component/Utils/continousLegend.d.ts +1 -1
  17. package/dist/component/{Utils/ColorTableTypes.d.ts → colorTableTypes.d.ts} +0 -0
  18. package/dist/component/colorTableTypes.js +2 -0
  19. package/dist/component/colorTableTypes.js.map +1 -0
  20. package/dist/index.d.ts +2 -4
  21. package/dist/index.js +1 -3
  22. package/dist/index.js.map +1 -1
  23. package/package.json +6 -3
  24. package/.storybook/main.js +0 -11
  25. package/.storybook/preview.js +0 -9
  26. package/.vscode/settings.json +0 -3
  27. package/dist/component/Utils/ColorTableTypes.js +0 -2
  28. package/dist/component/Utils/ColorTableTypes.js.map +0 -1
  29. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.d.ts +0 -7
  30. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js +0 -11
  31. package/dist/stories/ColorLegends/ColorSelector/Selector.stories.js.map +0 -1
  32. package/dist/stories/ColorLegends/ContinousLegends/MD.stories.d.ts +0 -7
  33. package/dist/stories/ColorLegends/ContinousLegends/MD.stories.js +0 -38
  34. package/dist/stories/ColorLegends/ContinousLegends/MD.stories.js.map +0 -1
  35. package/dist/stories/ColorLegends/ContinousLegends/NTG.stories.d.ts +0 -7
  36. package/dist/stories/ColorLegends/ContinousLegends/NTG.stories.js +0 -38
  37. package/dist/stories/ColorLegends/ContinousLegends/NTG.stories.js.map +0 -1
  38. package/dist/stories/ColorLegends/ContinousLegends/PermTot.stories.d.ts +0 -7
  39. package/dist/stories/ColorLegends/ContinousLegends/PermTot.stories.js +0 -38
  40. package/dist/stories/ColorLegends/ContinousLegends/PermTot.stories.js.map +0 -1
  41. package/dist/stories/ColorLegends/ContinousLegends/Permeability.stories.d.ts +0 -7
  42. package/dist/stories/ColorLegends/ContinousLegends/Permeability.stories.js +0 -38
  43. package/dist/stories/ColorLegends/ContinousLegends/Permeability.stories.js.map +0 -1
  44. package/dist/stories/ColorLegends/ContinousLegends/PoroTot.stories.d.ts +0 -7
  45. package/dist/stories/ColorLegends/ContinousLegends/PoroTot.stories.js +0 -38
  46. package/dist/stories/ColorLegends/ContinousLegends/PoroTot.stories.js.map +0 -1
  47. package/dist/stories/ColorLegends/ContinousLegends/Porosity.stories.d.ts +0 -7
  48. package/dist/stories/ColorLegends/ContinousLegends/Porosity.stories.js +0 -38
  49. package/dist/stories/ColorLegends/ContinousLegends/Porosity.stories.js.map +0 -1
  50. package/dist/stories/ColorLegends/ContinousLegends/SW.stories.d.ts +0 -7
  51. package/dist/stories/ColorLegends/ContinousLegends/SW.stories.js +0 -38
  52. package/dist/stories/ColorLegends/ContinousLegends/SW.stories.js.map +0 -1
  53. package/dist/stories/ColorLegends/DiscreteLegends/Facies.stories.d.ts +0 -7
  54. package/dist/stories/ColorLegends/DiscreteLegends/Facies.stories.js +0 -47
  55. package/dist/stories/ColorLegends/DiscreteLegends/Facies.stories.js.map +0 -1
  56. package/dist/stories/ColorLegends/DiscreteLegends/FaciesNoCalcite.stories.d.ts +0 -7
  57. package/dist/stories/ColorLegends/DiscreteLegends/FaciesNoCalcite.stories.js +0 -47
  58. package/dist/stories/ColorLegends/DiscreteLegends/FaciesNoCalcite.stories.js.map +0 -1
  59. package/dist/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.d.ts +0 -7
  60. package/dist/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.js +0 -39
  61. package/dist/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.js.map +0 -1
  62. package/dist/stories/ColorLegends/DiscreteLegends/FaultProximityFlag.stories.d.ts +0 -7
  63. package/dist/stories/ColorLegends/DiscreteLegends/FaultProximityFlag.stories.js +0 -39
  64. package/dist/stories/ColorLegends/DiscreteLegends/FaultProximityFlag.stories.js.map +0 -1
  65. package/dist/stories/ColorLegends/DiscreteLegends/RDE.stories.d.ts +0 -7
  66. package/dist/stories/ColorLegends/DiscreteLegends/RDE.stories.js +0 -42
  67. package/dist/stories/ColorLegends/DiscreteLegends/RDE.stories.js.map +0 -1
  68. package/dist/stories/ColorLegends/DiscreteLegends/RDE_Orig.stories.d.ts +0 -7
  69. package/dist/stories/ColorLegends/DiscreteLegends/RDE_Orig.stories.js +0 -53
  70. package/dist/stories/ColorLegends/DiscreteLegends/RDE_Orig.stories.js.map +0 -1
  71. package/dist/stories/ColorLegends/DiscreteLegends/ZoneMain.stories.d.ts +0 -7
  72. package/dist/stories/ColorLegends/DiscreteLegends/ZoneMain.stories.js +0 -42
  73. package/dist/stories/ColorLegends/DiscreteLegends/ZoneMain.stories.js.map +0 -1
  74. package/dist/stories/ColorLegends/DiscreteLegends/Zonelog.stories.d.ts +0 -7
  75. package/dist/stories/ColorLegends/DiscreteLegends/Zonelog.stories.js +0 -52
  76. package/dist/stories/ColorLegends/DiscreteLegends/Zonelog.stories.js.map +0 -1
  77. package/dist/stories/ColorLegends/LegendWithSelector/LegendSelector.stories.d.ts +0 -7
  78. package/dist/stories/ColorLegends/LegendWithSelector/LegendSelector.stories.js +0 -58
  79. package/dist/stories/ColorLegends/LegendWithSelector/LegendSelector.stories.js.map +0 -1
  80. package/src/component/ColorSelector/ColorSelectorAccordion.tsx +0 -54
  81. package/src/component/ColorSelector/ColorSelectorComponent.tsx +0 -312
  82. package/src/component/ColorSelector/ColorSelectorWrapper.tsx +0 -119
  83. package/src/component/Legend/ColorLegend.tsx +0 -106
  84. package/src/component/Legend/ContinuousLegend.tsx +0 -233
  85. package/src/component/Legend/DiscreteLegend.tsx +0 -206
  86. package/src/component/Utils/ColorTableTypes.ts +0 -6
  87. package/src/component/Utils/continousLegend.ts +0 -73
  88. package/src/component/Utils/d3ColorScale.ts +0 -84
  89. package/src/component/Utils/discreteLegend.ts +0 -81
  90. package/src/component/color-tables.json +0 -203
  91. package/src/custom.d.ts +0 -1
  92. package/src/index.ts +0 -26
  93. package/src/stories/ColorLegends/ColorSelector/Selector.stories.jsx +0 -12
  94. package/src/stories/ColorLegends/ContinousLegends/MD.stories.jsx +0 -29
  95. package/src/stories/ColorLegends/ContinousLegends/NTG.stories.jsx +0 -29
  96. package/src/stories/ColorLegends/ContinousLegends/PermTot.stories.jsx +0 -29
  97. package/src/stories/ColorLegends/ContinousLegends/Permeability.stories.jsx +0 -29
  98. package/src/stories/ColorLegends/ContinousLegends/PoroTot.stories.jsx +0 -29
  99. package/src/stories/ColorLegends/ContinousLegends/Porosity.stories.jsx +0 -29
  100. package/src/stories/ColorLegends/ContinousLegends/SW.stories.jsx +0 -29
  101. package/src/stories/ColorLegends/DiscreteLegends/Facies.stories.jsx +0 -39
  102. package/src/stories/ColorLegends/DiscreteLegends/FaciesNoCalcite.stories.jsx +0 -39
  103. package/src/stories/ColorLegends/DiscreteLegends/FaultDistanceHum.stories.jsx +0 -31
  104. package/src/stories/ColorLegends/DiscreteLegends/FaultProximityFlag.stories.jsx +0 -31
  105. package/src/stories/ColorLegends/DiscreteLegends/RDE.stories.jsx +0 -34
  106. package/src/stories/ColorLegends/DiscreteLegends/RDE_Orig.stories.jsx +0 -45
  107. package/src/stories/ColorLegends/DiscreteLegends/ZoneMain.stories.jsx +0 -34
  108. package/src/stories/ColorLegends/DiscreteLegends/Zonelog.stories.jsx +0 -46
  109. package/src/stories/ColorLegends/LegendWithSelector/LegendSelector.stories.jsx +0 -51
  110. package/tsconfig.json +0 -23
@@ -1,106 +0,0 @@
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
- import {d3ColorScales} from "../Utils/d3ColorScale";
7
-
8
- declare type ColorLegendProps = {
9
- colorTables: any;
10
- min: number;
11
- max: number;
12
- dataObjectName: string;
13
- position?: number[] | null;
14
- colorName: string;
15
- horizontal?: boolean | null;
16
- discreteData: { objects: Record<string, [number[], number]> };
17
- getColorMapname?: any;
18
- }
19
-
20
- // Todo: Adapt it for other layers too
21
- export const ColorLegend: React.FC<ColorLegendProps> = ({
22
- position,
23
- horizontal,
24
- colorTables,
25
- min,
26
- max,
27
- dataObjectName,
28
- colorName,
29
- discreteData,
30
- getColorMapname
31
- }: ColorLegendProps) => {
32
-
33
- const [isToggled, setIsToggled] = React.useState(false);
34
- const handleClick = useCallback(() => {
35
- setIsToggled(true);
36
- }, []);
37
-
38
- const [updateLegend, setUpdateLegendColor] = React.useState([] as any);
39
-
40
- const isColortableColors = colorTables.find(
41
- (value: any) => {
42
- return value?.name == colorName
43
- }
44
- );
45
-
46
- const isD3Colors = d3ColorScales.find(
47
- (value: any) => {
48
- return value?.name == colorName
49
- }
50
- );
51
-
52
- const [isCont, setIsCont] = React.useState(
53
- (isColortableColors || isD3Colors) &&
54
- (isColortableColors?.discrete == false || isD3Colors?.discrete == false) ? true : false
55
- );
56
-
57
- // Get new colorscale from colorselector and update legend
58
- const colorScaleObject = React.useCallback((data: any, value: any) => {
59
- // update color map layer
60
- if (data.name && getColorMapname) {
61
- getColorMapname(data.name);
62
- }
63
- // d3 color name
64
- else if (getColorMapname) {
65
- getColorMapname(data.legendColorName);
66
- }
67
- setUpdateLegendColor(data);
68
- setIsCont(value);
69
- }, []);
70
-
71
- return (
72
- <div>
73
- <div onClick={handleClick}>
74
- {isCont == true && (
75
- <ContinuousLegend
76
- min={min}
77
- max={max}
78
- dataObjectName={dataObjectName}
79
- position={position}
80
- colorName={colorName}
81
- colorTables={colorTables}
82
- horizontal={horizontal}
83
- updateLegend={updateLegend}
84
- />
85
- )}
86
- {isCont == false && (
87
- <DiscreteColorLegend
88
- discreteData={discreteData}
89
- dataObjectName={dataObjectName}
90
- position={position}
91
- colorName={colorName}
92
- colorTables={colorTables}
93
- horizontal={horizontal}
94
- updateLegend={updateLegend}
95
- />
96
- )}
97
- </div>
98
- <div>
99
- {isToggled && (
100
- <ColorSelectorAccordion colorScaleObject={colorScaleObject} isHorizontal={horizontal} />
101
- )}
102
- </div>
103
- </div>
104
-
105
- );
106
- };
@@ -1,233 +0,0 @@
1
- import * as React from "react";
2
- import { useRef } from "react";
3
- import { RGBToHex, colorsArray } from "../Utils/continousLegend";
4
- import { select, scaleLinear, scaleSequential, axisBottom, axisRight } from "d3";
5
- import { colorTablesArray } from "../Utils/ColorTableTypes";
6
- import {d3ColorScales} from "../Utils/d3ColorScale";
7
- import { color } from "d3-color";
8
- import { range } from "d3";
9
-
10
- declare type legendProps = {
11
- min: number;
12
- max: number;
13
- dataObjectName: string;
14
- position?: number[] | null;
15
- colorName: string;
16
- colorTables: colorTablesArray | string;
17
- horizontal?: boolean | null;
18
- updateLegend?: any;
19
- }
20
-
21
- declare type ItemColor = {
22
- color: string;
23
- offset: number;
24
- }
25
-
26
- export const ContinuousLegend: React.FC<legendProps> = ({
27
- min,
28
- max,
29
- dataObjectName,
30
- position,
31
- colorName,
32
- colorTables,
33
- horizontal,
34
- updateLegend,
35
- }: legendProps) => {
36
- const divRef = useRef<HTMLDivElement>(null);
37
-
38
- React.useEffect(() => {
39
- if (divRef.current) {
40
- continuousLegend();
41
- };
42
- return function cleanup() {
43
- select(divRef.current).select("div").remove();
44
- select(divRef.current).select("svg").remove();
45
- };
46
- }, [min, max, colorName, colorTables, horizontal, updateLegend]);
47
-
48
- async function continuousLegend() {
49
- const itemColor: ItemColor[] = [];
50
- let dataSet;
51
-
52
- try {
53
- // fix for dash wrapper
54
- if (typeof colorTables === "string") {
55
- const res = await fetch(colorTables);
56
- dataSet = await res.json()
57
- }
58
- //Return the matched colors array from color.tables.json file
59
- let legendColors = typeof colorTables === "string" ?
60
- colorsArray(colorName, dataSet)
61
- :
62
- colorsArray(colorName, colorTables);
63
-
64
- // Update color of legend based on color selector scales
65
- // data is passed on click upon color scales
66
- if (updateLegend && Object.keys(updateLegend).length > 0) {
67
- // legend using color table data
68
- if (updateLegend.color) {
69
- legendColors = updateLegend.color;
70
- }
71
- // legend using d3 data
72
- else if (updateLegend.arrayData) {
73
- legendColors = updateLegend.arrayData;
74
- }
75
- }
76
- // main continuous legend for colortable colors
77
- if (legendColors.length > 0) {
78
- legendColors = legendColors
79
- }
80
- // main continuous legend for d3 colors
81
- else {
82
- const arrayData: any = []
83
- const d3ColorArrays = colorsArray(colorName, d3ColorScales)
84
- const data = range(10).map((d) => ({color: d3ColorArrays(d / 10)}));
85
- data.forEach((colorsObject: any, index: number) => {
86
- arrayData.push([0 + "." + index, color(colorsObject.color)?.rgb().r, color(colorsObject.color)?.rgb().g, color(colorsObject.color)?.rgb().b])
87
- });
88
- legendColors = arrayData
89
- }
90
-
91
- legendColors.forEach((value: [number, number, number, number]) => {
92
- // return the color and offset needed to draw the legend
93
- itemColor.push({
94
- offset: RGBToHex(value).offset,
95
- color: RGBToHex(value).color,
96
- });
97
- });
98
-
99
- const colorScale = scaleSequential().domain([min, max]);
100
- // append a defs (for definition) element to your SVG
101
- const svgLegend = select(divRef.current)
102
- .append("svg")
103
- .style("background-color", "#ffffffcc")
104
- .style("border-radius", "5px");
105
-
106
- const defs = svgLegend.append("defs");
107
- let linearGradient;
108
- // vertical legend
109
- if (!horizontal) {
110
- svgLegend
111
- // .style("transform", "rotate(270deg)")
112
- // .style("margin-top", "80px");
113
- .attr("width", "100")
114
- .attr("height", "190");
115
- linearGradient = defs
116
- .append("linearGradient")
117
- .attr("id", "linear-gradient")
118
- .attr("x1", "0%")
119
- .attr("x2", "0%")
120
- .attr("y1", "0%")
121
- .attr("y2", "100%"); //since it's a vertical linear gradient
122
- }
123
- // horizontal legend
124
- else {
125
- svgLegend.attr("width", "200").attr("height", "90");
126
-
127
- // append a linearGradient element to the defs and give it a unique id
128
- linearGradient = defs
129
- .append("linearGradient")
130
- .attr("id", "linear-gradient")
131
- .attr("x1", "0%")
132
- .attr("x2", "100%") //since it's a horizontal linear gradient
133
- .attr("y1", "0%")
134
- .attr("y2", "0%");
135
- }
136
-
137
- // append multiple color stops by using D3's data/enter step
138
- linearGradient
139
- .selectAll("stop")
140
- .data(itemColor)
141
- .enter()
142
- .append("stop")
143
- .attr("offset", function (data) {
144
- return data.offset + "%";
145
- })
146
- .attr("stop-color", function (data) {
147
- return data.color;
148
- });
149
-
150
- // append title
151
- svgLegend
152
- .append("text")
153
- .attr("class", "legendTitle")
154
- .attr("x", 25)
155
- .attr("y", 20)
156
- .style("text-anchor", "left")
157
- .text(dataObjectName);
158
-
159
- // vertical legend
160
- if (!horizontal) {
161
- // draw the rectangle and fill with gradient
162
- svgLegend
163
- .append("rect")
164
- .attr("x", 25)
165
- .attr("y", 30)
166
- .attr("width", 25)
167
- .attr("height", "150")
168
- .style("fill", "url(#linear-gradient)");
169
- }
170
- // horizontal legend
171
- else {
172
- // draw the rectangle and fill with gradient
173
- svgLegend
174
- .append("rect")
175
- .attr("x", 25)
176
- .attr("y", 30)
177
- .attr("width", "150")
178
- .attr("height", 25)
179
- .style("fill", "url(#linear-gradient)");
180
- }
181
-
182
- // create tick marks
183
- // range varies the size of the axis
184
- const xLeg = scaleLinear().domain([min, max]).range([10, 158]);
185
- const yLeg = scaleLinear().domain([min, max]).range([10, 158]);
186
-
187
- const horizontalAxisLeg = axisBottom(xLeg).tickValues(
188
- colorScale.domain()
189
- );
190
- const VerticalAxisLeg = axisRight(yLeg)
191
- .tickSize(24)
192
- .tickValues(colorScale.domain());
193
-
194
- if (horizontal) {
195
- svgLegend
196
- .attr("class", "axis")
197
- .append("g")
198
- .attr("transform", "translate(15, 55)")
199
- .style("font-size", "10px")
200
- .style("font-weight", "700")
201
- .call(horizontalAxisLeg)
202
- .style("height", 25);
203
- } else {
204
- svgLegend
205
- .attr("class", "axis")
206
- .append("g")
207
- .attr("transform", "translate(25, 20)")
208
- .style("font-size", "10px")
209
- .style("font-weight", "700")
210
- .call(VerticalAxisLeg);
211
- }
212
- } catch (error) {
213
- console.error(error);
214
- }
215
- }
216
-
217
- return (
218
- <div
219
- style={{
220
- position: "absolute",
221
- right: position ? position[0] : " ",
222
- top: position ? position[1] : " ",
223
- zIndex: 999,
224
- }}
225
- >
226
- <div id="legend" ref={divRef}></div>
227
- </div>
228
- );
229
- };
230
-
231
- ContinuousLegend.defaultProps = {
232
- position: [5, 10],
233
- };
@@ -1,206 +0,0 @@
1
- import * as React from "react";
2
- import { useRef } from "react";
3
- import discreteLegendUtil from "../Utils/discreteLegend";
4
- import { select, scaleOrdinal } from "d3";
5
- import { colorTablesArray } from "../Utils/ColorTableTypes";
6
- import { colorsArray } from "../Utils/continousLegend"
7
- import {d3ColorScales} from "../Utils/d3ColorScale";
8
-
9
- declare type ItemColor = {
10
- color: string;
11
- name?: string
12
- };
13
-
14
- declare type colorLegendProps = {
15
- discreteData: { objects: Record<string, [number[], number]> };
16
- dataObjectName: string;
17
- position?: number[] | null;
18
- colorName: string;
19
- colorTables: colorTablesArray | string;
20
- horizontal?: boolean | null;
21
- updateLegend?: any
22
- };
23
-
24
- export const DiscreteColorLegend: React.FC<colorLegendProps> = ({
25
- discreteData,
26
- dataObjectName,
27
- position,
28
- colorName,
29
- colorTables,
30
- horizontal,
31
- updateLegend,
32
- }: colorLegendProps) => {
33
- const divRef = useRef<HTMLDivElement>(null);
34
- let itemName: string[] = [];
35
- let itemColor: ItemColor[] = [];
36
-
37
- React.useEffect(() => {
38
- if (divRef.current) {
39
- select(divRef.current).select("div").remove();
40
- select(divRef.current).select("svg").remove();
41
- discreteLegend();
42
- }
43
- return function cleanup() {
44
- select(divRef.current).select("div").remove();
45
- select(divRef.current).select("svg").remove();
46
- };
47
- }, [discreteData, colorName, colorTables, horizontal, updateLegend]);
48
-
49
- async function discreteLegend() {
50
- let dataSet;
51
- let useSelectorLegend = false;
52
-
53
- try {
54
- // fix for dash wrapper
55
- if (typeof colorTables === "string") {
56
- const res = await fetch(colorTables);
57
- dataSet = await res.json();
58
- }
59
-
60
- const arrayOfColors =
61
- typeof colorTables === "string"
62
- ? colorsArray(colorName, dataSet)
63
- : colorsArray(colorName, colorTables);
64
-
65
- const d3ColorArrays = colorsArray(colorName, d3ColorScales)
66
-
67
- // Main discrete legend
68
- if (!updateLegend || updateLegend.length == 0) {
69
- Object.keys(discreteData).forEach((key) => {
70
- //eslint-disable-next-line
71
- let code = (discreteData as { [key: string]: any })[key][1]
72
- // for colortable colors
73
- if (arrayOfColors.length > 0) {
74
- //compare the first value in colorarray(colortable) and code from discreteData
75
- const matchedColorsArrays = arrayOfColors.find(
76
- (value: number[]) => {
77
- return value[0] == code;
78
- }
79
- );
80
- if (matchedColorsArrays)
81
- itemColor.push({
82
- color: RGBToHex(matchedColorsArrays),
83
- name: key
84
- });
85
- itemName.push(key);
86
- }
87
- // for d3 colors
88
- else {
89
- var matchedColorsArrays = d3ColorArrays.find((value: number,index: number) => {
90
- return index == code;
91
- });
92
- if(matchedColorsArrays) itemColor.push({color: matchedColorsArrays});
93
- }
94
-
95
- });
96
- useSelectorLegend = false;
97
- }
98
- // Discrete legend using Colortable colors
99
- else if (updateLegend?.color) {
100
- updateLegend.color.forEach((key: any) => {
101
- itemColor.push({ color: RGBToHex(key) });
102
- });
103
-
104
- useSelectorLegend = true;
105
- }
106
- // Discrete legend using d3 colors
107
- else if (updateLegend?.colorsObject) {
108
- updateLegend.colorsObject.forEach((key: any) => {
109
- itemColor.push({ color: key });
110
- });
111
-
112
- itemName = updateLegend.legendColorName;
113
- useSelectorLegend = true;
114
- }
115
-
116
- const ordinalValues = scaleOrdinal().domain(itemName);
117
- const colorLegend = discreteLegendUtil(
118
- itemColor,
119
- useSelectorLegend,
120
- horizontal
121
- )
122
- .inputScale(ordinalValues);
123
- const currentDiv = select(divRef.current);
124
- let totalRect;
125
-
126
- // append the title
127
- currentDiv
128
- .append("div")
129
- .text(dataObjectName)
130
- .style("color", "grey")
131
- .attr("y", 10)
132
- .style("margin-bottom", "5px");
133
-
134
- // Append svg to the div
135
- const svgLegend = currentDiv
136
- .style("margin", "5px")
137
- .append("svg")
138
- .call(colorLegend);
139
-
140
- // Style for main horizontal legend
141
- if (!useSelectorLegend) {
142
- totalRect = itemColor.length;
143
- }
144
- // Style for color selector legend
145
- else {
146
- // calculate width for legend using colortable colors
147
- if (updateLegend?.color) {
148
- totalRect = updateLegend.color.length;
149
- }
150
- // calculate width for legend using d3 colors
151
- else {
152
- totalRect = updateLegend.colorsObject.length;
153
- }
154
- }
155
-
156
- if (horizontal) {
157
- svgLegend
158
- .attr("viewBox", `0 0 ${totalRect} 2`)
159
- .attr("preserveAspectRatio", "none")
160
- .style("font-size", ".4")
161
- .attr("height", "50px")
162
- .attr("width", "150px");
163
- }
164
- // vertical legend
165
- else if (!horizontal) {
166
- svgLegend
167
- .attr("viewBox", `0 0 4 ${totalRect}`)
168
- .attr("preserveAspectRatio", "none")
169
- .style("font-size", ".4")
170
- .attr("height", "150px")
171
- .attr("width", "100px");
172
- }
173
- } catch (error) {
174
- console.error(error);
175
- }
176
- }
177
-
178
- return (
179
- <div
180
- style={{
181
- position: "absolute",
182
- right: position ? position[0] : " ",
183
- top: position ? position[1] : " ",
184
- backgroundColor: "#ffffffcc",
185
- borderRadius: "5px",
186
- zIndex: 999,
187
- }}
188
- >
189
- <div id="legend" ref={divRef}></div>
190
- </div>
191
- );
192
- };
193
-
194
- export function RGBToHex(rgb: number[]) {
195
- let r = rgb[1].toString(16),
196
- g = rgb[2].toString(16),
197
- b = rgb[3].toString(16);
198
- if (r.length == 1) r = "0" + r;
199
- if (g.length == 1) g = "0" + g;
200
- if (b.length == 1) b = "0" + b;
201
- return "#" + r + g + b;
202
- }
203
-
204
- DiscreteColorLegend.defaultProps = {
205
- position: [5, 10],
206
- };
@@ -1,6 +0,0 @@
1
- export declare type colorTablesObj = {
2
- name: string;
3
- discrete: boolean;
4
- colors: [number, number, number, number][];
5
- }
6
- export type colorTablesArray = Array<colorTablesObj>;
@@ -1,73 +0,0 @@
1
- import { color } from "d3-color";
2
- import { interpolateRgb } from "d3-interpolate";
3
- import {
4
- colorTablesArray,
5
- colorTablesObj,
6
- } from "../Utils/ColorTableTypes";
7
-
8
- // Based on objectName return the colors array from color.tables.json file
9
- export function colorsArray(
10
- colorName: string,
11
- colorTables: any
12
- ): any {
13
- const colorTableData = colorTables.filter(
14
- (value: colorTablesObj) =>
15
- value.name.toLowerCase() == colorName.toLowerCase()
16
- );
17
- return colorTableData.length > 0 ? colorTableData[0].colors : [];
18
- }
19
-
20
- // return the colors based on the point
21
- export function rgbValues(
22
- point: number,
23
- colorName: string,
24
- colorTables: colorTablesArray
25
- ): number[] | { r: number; g: number; b: number; opacity: number } | undefined {
26
- const colorTableColors = colorsArray(colorName, colorTables);
27
- // compare the point and first value from colorTableColors
28
- const colorArrays = colorTableColors.find(
29
- (value: [number, number, number, number]) => {
30
- return point == value[0];
31
- }
32
- );
33
-
34
- // if point and value in color table matches then return particular colors
35
- if (colorArrays) {
36
- return colorArrays.slice(1);
37
- }
38
- // if no match then need to do interpolation
39
- else {
40
- // Get index of first match of colortable point greater than point
41
- const index = colorTableColors.findIndex((value: number[]) => {
42
- return value[0] > point;
43
- });
44
-
45
- const firstColorArray = colorTableColors[index - 1];
46
- const secondColorArray = colorTableColors[index];
47
-
48
- if ((firstColorArray || secondColorArray) != undefined) {
49
- const t0 = firstColorArray[0];
50
- const t1 = secondColorArray[0];
51
- const t = (point - t0) / (t1 - t0); // t = 0.0 gives first color, t = 1.0 gives second color.
52
- const interpolatedValues = interpolateRgb(
53
- RGBToHex(firstColorArray).color,
54
- RGBToHex(secondColorArray).color
55
- )(t);
56
- return color(interpolatedValues)?.rgb();
57
- }
58
- return undefined;
59
- }
60
- }
61
-
62
- // return the hex color code and offset
63
- export function RGBToHex(rgb: number[]) {
64
- let r = Math.round(rgb[1]).toString(16),
65
- g = Math.round(rgb[2]).toString(16),
66
- b = Math.round(rgb[3]).toString(16);
67
- if (r.length == 1) r = "0" + r;
68
- if (g.length == 1) g = "0" + g;
69
- if (b.length == 1) b = "0" + b;
70
- const offset = rgb[0] * 100.0;
71
-
72
- return { color: "#" + r + g + b, offset: offset };
73
- }