@emerson-eps/color-tables 0.2.5 → 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.
- package/dist/component/ColorSelector/ColorSelectorAccordion.js +2 -2
- package/dist/component/ColorSelector/ColorSelectorAccordion.js.map +1 -1
- package/dist/component/ColorSelector/ColorSelectorComponent.d.ts +2 -2
- package/dist/component/ColorSelector/ColorSelectorComponent.js +8 -3
- package/dist/component/ColorSelector/ColorSelectorComponent.js.map +1 -1
- package/dist/component/ColorSelector/ColorSelectorWrapper.d.ts +2 -2
- package/dist/component/ColorSelector/ColorSelectorWrapper.js.map +1 -1
- package/dist/component/Legend/ColorLegend.d.ts +6 -3
- package/dist/component/Legend/ColorLegend.js +10 -2
- package/dist/component/Legend/ColorLegend.js.map +1 -1
- package/dist/component/Legend/ContinuousLegend.js +12 -15
- package/dist/component/Legend/ContinuousLegend.js.map +1 -1
- package/dist/component/Legend/DiscreteLegend.js +58 -53
- package/dist/component/Legend/DiscreteLegend.js.map +1 -1
- package/dist/component/Utils/discreteLegend.d.ts +1 -1
- package/dist/component/Utils/discreteLegend.js +120 -64
- package/dist/component/Utils/discreteLegend.js.map +1 -1
- package/package.json +1 -1
- package/src/component/ColorSelector/ColorSelectorAccordion.tsx +2 -2
- package/src/component/ColorSelector/ColorSelectorComponent.tsx +19 -14
- package/src/component/ColorSelector/ColorSelectorWrapper.tsx +5 -5
- package/src/component/Legend/ColorLegend.tsx +14 -4
- package/src/component/Legend/ContinuousLegend.tsx +50 -55
- package/src/component/Legend/DiscreteLegend.tsx +117 -79
- package/src/component/Utils/discreteLegend.ts +125 -75
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { useRef } from "react";
|
|
3
3
|
import discreteLegendUtil from "../Utils/discreteLegend";
|
|
4
|
-
import { select } from "d3";
|
|
4
|
+
import { select, scaleOrdinal } from "d3";
|
|
5
5
|
import { colorTablesArray, colorTablesObj } from "../ColorTableTypes";
|
|
6
6
|
|
|
7
7
|
declare type ItemColor = {
|
|
@@ -30,11 +30,13 @@ export const DiscreteColorLegend: React.FC<colorLegendProps> = ({
|
|
|
30
30
|
const divRef = useRef<HTMLDivElement>(null);
|
|
31
31
|
let itemName: string[] = [];
|
|
32
32
|
let itemColor: ItemColor[] = [];
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const testColor: ItemColor[] = [];
|
|
34
|
+
const testName: string[] = [];
|
|
35
35
|
|
|
36
36
|
React.useEffect(() => {
|
|
37
37
|
if (divRef.current) {
|
|
38
|
+
select(divRef.current).select("div").remove();
|
|
39
|
+
select(divRef.current).select("svg").remove();
|
|
38
40
|
discreteLegend();
|
|
39
41
|
}
|
|
40
42
|
return function cleanup() {
|
|
@@ -45,114 +47,149 @@ export const DiscreteColorLegend: React.FC<colorLegendProps> = ({
|
|
|
45
47
|
|
|
46
48
|
async function discreteLegend() {
|
|
47
49
|
let dataSet;
|
|
48
|
-
let
|
|
50
|
+
let useSelectorLegend = false;
|
|
49
51
|
|
|
50
52
|
try {
|
|
51
53
|
// fix for dash wrapper
|
|
52
54
|
if (typeof colorTables === "string") {
|
|
53
|
-
|
|
54
|
-
dataSet = await res.json()
|
|
55
|
+
const res = await fetch(colorTables);
|
|
56
|
+
dataSet = await res.json();
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const colorsArray =
|
|
60
|
+
typeof colorTables === "string"
|
|
61
|
+
? colorTableData(colorName, dataSet)
|
|
62
|
+
: colorTableData(colorName, colorTables);
|
|
63
|
+
|
|
64
|
+
console.log('updateLegend', updateLegend)
|
|
61
65
|
|
|
62
66
|
// Main discrete legend
|
|
63
|
-
if(!updateLegend) {
|
|
67
|
+
if (!updateLegend || updateLegend.length == 0) {
|
|
64
68
|
Object.keys(discreteData).forEach((key) => {
|
|
65
69
|
//eslint-disable-next-line
|
|
66
70
|
let code = (discreteData as { [key: string]: any })[key][1]
|
|
67
71
|
//compare the first value in colorarray(colortable) and code from discreteData
|
|
68
|
-
const matchedColorsArrays = colorsArray.find(
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
const matchedColorsArrays = colorsArray.find(
|
|
73
|
+
(value: number[]) => {
|
|
74
|
+
return value[0] == code;
|
|
75
|
+
}
|
|
76
|
+
);
|
|
71
77
|
if (matchedColorsArrays)
|
|
72
|
-
itemColor.push({
|
|
73
|
-
|
|
78
|
+
itemColor.push({
|
|
79
|
+
color: RGBToHex(matchedColorsArrays),
|
|
80
|
+
});
|
|
81
|
+
itemName.push(key);
|
|
74
82
|
});
|
|
75
|
-
|
|
76
|
-
}
|
|
83
|
+
useSelectorLegend = false;
|
|
84
|
+
}
|
|
77
85
|
// Discrete legend using Colortable colors
|
|
78
|
-
else if (updateLegend
|
|
79
|
-
// Object.keys(discreteData).forEach((key) => {
|
|
80
|
-
// //eslint-disable-next-line
|
|
81
|
-
// let code = (discreteData as { [key: string]: any })[key][1]
|
|
82
|
-
// //compare the first value in colorarray(colortable) and code from discreteData
|
|
83
|
-
// const matchedColorsArrays = updateLegend.color.find((value: number[]) => {
|
|
84
|
-
// return value[0] == code;
|
|
85
|
-
// });
|
|
86
|
-
// if (matchedColorsArrays) {
|
|
87
|
-
// testColor.push({color: RGBToHex(matchedColorsArrays)});
|
|
88
|
-
// testName.push(key);
|
|
89
|
-
// }
|
|
90
|
-
|
|
91
|
-
// });
|
|
92
|
-
|
|
93
|
-
//let color = updateLegend;
|
|
86
|
+
else if (updateLegend?.color) {
|
|
94
87
|
updateLegend.color.forEach((key: any) => {
|
|
95
|
-
testColor.push({color: RGBToHex(key)});
|
|
88
|
+
testColor.push({ color: RGBToHex(key) });
|
|
96
89
|
});
|
|
97
90
|
|
|
98
|
-
itemColor = testColor
|
|
99
|
-
itemName = testName
|
|
100
|
-
|
|
91
|
+
itemColor = testColor;
|
|
92
|
+
itemName = testName;
|
|
93
|
+
useSelectorLegend = true;
|
|
101
94
|
}
|
|
102
95
|
// Discrete legend using d3 colors
|
|
103
|
-
else
|
|
96
|
+
else if (updateLegend?.colorsObject) {
|
|
104
97
|
updateLegend.colorsObject.forEach((key: any) => {
|
|
105
|
-
itemColor.push({color: key});
|
|
98
|
+
itemColor.push({ color: key });
|
|
106
99
|
});
|
|
107
100
|
|
|
108
|
-
itemColor = itemColor
|
|
109
|
-
itemName = updateLegend.legendColorName
|
|
110
|
-
|
|
111
|
-
|
|
101
|
+
//itemColor = itemColor;
|
|
102
|
+
itemName = updateLegend.legendColorName;
|
|
103
|
+
useSelectorLegend = true;
|
|
112
104
|
}
|
|
113
105
|
|
|
114
|
-
//const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
106
|
+
// const toolTipName = itemName.map((word, idx) => {
|
|
107
|
+
// return <li key={idx}>{word}</li>;
|
|
108
|
+
// });
|
|
109
|
+
|
|
110
|
+
// let nameTool;
|
|
111
|
+
|
|
112
|
+
// toolTipName.forEach((data) => {
|
|
113
|
+
// //nameTool.push(data.props.children)
|
|
114
|
+
// nameTool = data.props.children;
|
|
115
|
+
// });
|
|
116
|
+
|
|
117
|
+
// // create a tooltip
|
|
118
|
+
// const tooltip = select(divRef.current)
|
|
119
|
+
// .append("div")
|
|
120
|
+
// .style("position", "absolute")
|
|
121
|
+
// .style("visibility", "hidden")
|
|
122
|
+
// .style("background-color", "black")
|
|
123
|
+
// .style("border", "solid")
|
|
124
|
+
// .style("border-width", "1px")
|
|
125
|
+
// .style("border-radius", "5px")
|
|
126
|
+
// .style("padding", "10px")
|
|
127
|
+
// .text(nameTool)
|
|
128
|
+
// .style("color", "grey");
|
|
129
|
+
|
|
130
|
+
const ordinalValues = scaleOrdinal().domain(itemName);
|
|
131
|
+
const colorLegend = discreteLegendUtil(
|
|
132
|
+
itemColor,
|
|
133
|
+
useSelectorLegend,
|
|
134
|
+
horizontal
|
|
135
|
+
).inputScale(ordinalValues);
|
|
136
|
+
const currentDiv = select(divRef.current);
|
|
137
|
+
let totalRect;
|
|
138
|
+
|
|
139
|
+
// append the title
|
|
140
|
+
currentDiv
|
|
120
141
|
.append("div")
|
|
121
142
|
.text(dataObjectName)
|
|
143
|
+
.style("color", "grey")
|
|
122
144
|
.attr("y", 10)
|
|
123
|
-
.style("margin-bottom", "5px")
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
.style("margin
|
|
145
|
+
.style("margin-bottom", "5px");
|
|
146
|
+
|
|
147
|
+
// Append svg to the div
|
|
148
|
+
const svgLegend = currentDiv
|
|
149
|
+
.style("margin", "5px")
|
|
128
150
|
.append("svg")
|
|
151
|
+
// .on("mouseover", function () {
|
|
152
|
+
// return tooltip.style("visibility", "visible");
|
|
153
|
+
// })
|
|
154
|
+
// .on("mouseout", function () {
|
|
155
|
+
// return tooltip.style("visibility", "hidden");
|
|
156
|
+
// })
|
|
129
157
|
.call(colorLegend);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
} else if (!discreteFlag && horizontal) {
|
|
135
|
-
svgLegend
|
|
136
|
-
.style("transform", "rotate(90deg)")
|
|
137
|
-
.attr("height", calcLegendHeight + "px")
|
|
138
|
-
.attr("width", calcLegendHeight + "px");
|
|
158
|
+
|
|
159
|
+
// Style for main horizontal legend
|
|
160
|
+
if (!useSelectorLegend) {
|
|
161
|
+
totalRect = itemColor.length;
|
|
139
162
|
}
|
|
163
|
+
// Style for color selector legend
|
|
140
164
|
else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
165
|
+
// calculate width for legend using colortable colors
|
|
166
|
+
if (updateLegend?.color) {
|
|
167
|
+
totalRect = updateLegend.color.length;
|
|
168
|
+
}
|
|
169
|
+
// calculate width for legend using d3 colors
|
|
170
|
+
else {
|
|
171
|
+
totalRect = updateLegend.colorsObject.length;
|
|
147
172
|
}
|
|
148
|
-
svgLegend.attr("viewBox", `0 0 ${totalRect} 2`)
|
|
149
|
-
.attr("preserveAspectRatio", "none")
|
|
150
|
-
.style("font-size", ".5")
|
|
151
|
-
.attr("height", "50px")
|
|
152
|
-
.attr("width", "150px");
|
|
153
173
|
}
|
|
154
|
-
|
|
155
|
-
|
|
174
|
+
|
|
175
|
+
if (horizontal) {
|
|
176
|
+
svgLegend
|
|
177
|
+
.attr("viewBox", `0 0 ${totalRect} 2`)
|
|
178
|
+
.attr("preserveAspectRatio", "none")
|
|
179
|
+
.style("font-size", ".4")
|
|
180
|
+
.attr("height", "50px")
|
|
181
|
+
.attr("width", "150px");
|
|
182
|
+
}
|
|
183
|
+
// vertical legend
|
|
184
|
+
else if (!horizontal) {
|
|
185
|
+
svgLegend
|
|
186
|
+
.attr("viewBox", `0 0 4 ${totalRect}`)
|
|
187
|
+
.attr("preserveAspectRatio", "none")
|
|
188
|
+
.style("font-size", ".4")
|
|
189
|
+
.attr("height", "150px")
|
|
190
|
+
.attr("width", "100px");
|
|
191
|
+
}
|
|
192
|
+
} catch (error) {
|
|
156
193
|
console.error(error);
|
|
157
194
|
}
|
|
158
195
|
}
|
|
@@ -161,10 +198,11 @@ export const DiscreteColorLegend: React.FC<colorLegendProps> = ({
|
|
|
161
198
|
<div
|
|
162
199
|
style={{
|
|
163
200
|
position: "absolute",
|
|
164
|
-
right: position ? position[0] :
|
|
165
|
-
top: position ? position[1] :
|
|
201
|
+
right: position ? position[0] : " ",
|
|
202
|
+
top: position ? position[1] : " ",
|
|
166
203
|
backgroundColor: "#ffffffcc",
|
|
167
204
|
borderRadius: "5px",
|
|
205
|
+
zIndex: "999 !important",
|
|
168
206
|
}}
|
|
169
207
|
>
|
|
170
208
|
<div id="legend" ref={divRef}></div>
|
|
@@ -1,105 +1,155 @@
|
|
|
1
|
-
declare type ItemColor = {
|
|
2
|
-
color: any;
|
|
3
|
-
}
|
|
1
|
+
declare type ItemColor = {
|
|
2
|
+
color: any;
|
|
3
|
+
};
|
|
4
4
|
|
|
5
5
|
// eslint-disable-next-line
|
|
6
|
-
export default function discreteLegendUtil(itemColor: ItemColor[], isSelectorLegend?: boolean): any {
|
|
7
|
-
var cellWidth = 22;
|
|
8
|
-
var cellHeight = 22;
|
|
9
|
-
const cellPadding = 2;
|
|
6
|
+
export default function discreteLegendUtil(itemColor: ItemColor[], isSelectorLegend?: boolean, horizontal?: any): any {
|
|
7
|
+
// var cellWidth = 22;
|
|
8
|
+
// var cellHeight = 22;
|
|
10
9
|
|
|
11
|
-
if (isSelectorLegend) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
10
|
+
// if (isSelectorLegend) {
|
|
11
|
+
// cellWidth = 25;
|
|
12
|
+
// cellHeight = 22;
|
|
13
|
+
// }
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
const legendValues: ItemColor[] = [];
|
|
17
16
|
|
|
18
17
|
// eslint-disable-next-line
|
|
19
|
-
function legend(g: any) {
|
|
18
|
+
function legend(g: any) {
|
|
20
19
|
function drawLegend() {
|
|
21
20
|
// Code to fill the color
|
|
22
21
|
// Styling for color selector legend
|
|
23
|
-
if (isSelectorLegend) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
//if (isSelectorLegend) {
|
|
23
|
+
g.selectAll("g.legendCells")
|
|
24
|
+
.data(itemColor)
|
|
25
|
+
.enter()
|
|
26
|
+
.append("g")
|
|
27
|
+
.append("rect")
|
|
28
|
+
.attr("class", "rectLabel")
|
|
29
|
+
.style("cursor", "pointer")
|
|
30
|
+
.style("pointer-events", "all");
|
|
31
|
+
g.selectAll("rect")
|
|
32
|
+
.attr("height", 1)
|
|
33
|
+
.attr("width", 1)
|
|
34
|
+
.style("fill", function (d: Record<string, unknown>) {
|
|
35
|
+
return d["color"];
|
|
36
|
+
});
|
|
37
|
+
if (horizontal && !isSelectorLegend) {
|
|
38
|
+
g.selectAll("rect")
|
|
39
|
+
.attr("x", function (d: number, i: number) {
|
|
40
|
+
console.log(d)
|
|
41
|
+
return i;
|
|
42
|
+
})
|
|
43
|
+
.attr("y", 0);
|
|
44
|
+
} else if (!horizontal && !isSelectorLegend) {
|
|
29
45
|
g.selectAll("rect")
|
|
30
|
-
.attr("
|
|
46
|
+
.attr("y", function (d: number, i: number) {
|
|
31
47
|
console.log(d)
|
|
32
48
|
return i;
|
|
33
49
|
})
|
|
34
|
-
.attr("
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
.
|
|
38
|
-
|
|
50
|
+
.attr("x", 0);
|
|
51
|
+
} else if (horizontal == true && isSelectorLegend) {
|
|
52
|
+
g.selectAll("rect")
|
|
53
|
+
.attr("x", function (d: number, i: number) {
|
|
54
|
+
console.log(d)
|
|
55
|
+
return i;
|
|
39
56
|
})
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
.
|
|
44
|
-
.attr("class", "breakLabels")
|
|
45
|
-
.style("font-size", "0.4")
|
|
46
|
-
//.attr("x", cellWidth + cellPadding)
|
|
47
|
-
//.attr("y", 5 + cellHeight / 2)
|
|
48
|
-
.attr("x", function (d: any, i: any) {
|
|
57
|
+
.attr("y", 0);
|
|
58
|
+
} else if (horizontal == false && isSelectorLegend) {
|
|
59
|
+
g.selectAll("rect")
|
|
60
|
+
.attr("y", function (d: number, i: number) {
|
|
49
61
|
console.log(d)
|
|
50
|
-
return i
|
|
62
|
+
return i;
|
|
51
63
|
})
|
|
52
|
-
.attr("
|
|
53
|
-
|
|
64
|
+
.attr("x", 0);
|
|
65
|
+
} else {
|
|
66
|
+
g.selectAll("rect")
|
|
67
|
+
.attr("x", function (d: number, i: number) {
|
|
54
68
|
console.log(d)
|
|
55
69
|
return i;
|
|
56
70
|
})
|
|
71
|
+
.attr("y", 0);
|
|
57
72
|
}
|
|
73
|
+
|
|
74
|
+
// Display the labels for legend
|
|
75
|
+
// g.selectAll("g")
|
|
76
|
+
// .style("background", "grey")
|
|
77
|
+
// .append("text")
|
|
78
|
+
// .attr("class", "breakLabels")
|
|
79
|
+
// .style("font-size", "0.4")
|
|
80
|
+
// //.attr("x", cellWidth + cellPadding)
|
|
81
|
+
// //.attr("y", 5 + cellHeight / 2)
|
|
82
|
+
// .attr("x", function (d: any, i: any) {
|
|
83
|
+
// //console.log(d)
|
|
84
|
+
// return i + "." + 25
|
|
85
|
+
// })
|
|
86
|
+
// .attr("y", 1.5)
|
|
87
|
+
// .text(function (d: any, i: any) {
|
|
88
|
+
// //console.log(d)
|
|
89
|
+
// return i;
|
|
90
|
+
// })
|
|
91
|
+
//}
|
|
58
92
|
// styling for main legend
|
|
59
|
-
else {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
93
|
+
// else {
|
|
94
|
+
// itemColor.forEach((item, index) => {
|
|
95
|
+
// legendValues[index].color = item.color;
|
|
96
|
+
// });
|
|
97
|
+
// g.selectAll("g.legendCells")
|
|
98
|
+
// .append("text")
|
|
99
|
+
// .attr("class", "breakLabels")
|
|
100
|
+
// .style("fill", "#6F6F6F")
|
|
101
|
+
// .attr("x", cellWidth + cellPadding)
|
|
102
|
+
// .attr("y", 5 + cellHeight / 2)
|
|
103
|
+
// .text(function (d: Record<string, unknown>, i:any) {
|
|
104
|
+
// return d['label'];
|
|
105
|
+
// });
|
|
106
|
+
// g.selectAll("g.legendCells")
|
|
107
|
+
// .style("cursor", "pointer")
|
|
108
|
+
// .append("rect")
|
|
109
|
+
// .attr("height", cellHeight)
|
|
110
|
+
// .attr("width", cellWidth)
|
|
111
|
+
// .style("fill", function (d: Record<string, unknown>) {
|
|
112
|
+
// //console.log('d', d)
|
|
113
|
+
// return d["color"];
|
|
114
|
+
// })
|
|
115
|
+
|
|
116
|
+
// // Alighment of cell in straight line
|
|
117
|
+
// g.selectAll("g.legendCells").attr(
|
|
118
|
+
// "transform",
|
|
119
|
+
// function (_d: Record<string, unknown>, i: number) {
|
|
120
|
+
// return (
|
|
121
|
+
// "translate(0," + i * (cellHeight + cellPadding) + ")"
|
|
122
|
+
// //"translate(0," + i + ")"
|
|
123
|
+
// );
|
|
124
|
+
// }
|
|
125
|
+
// );
|
|
126
|
+
// }
|
|
78
127
|
}
|
|
79
128
|
|
|
80
129
|
// display the discrete legend
|
|
81
|
-
if (!isSelectorLegend) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
130
|
+
// if (!isSelectorLegend) {
|
|
131
|
+
// // console.log('itemcolor', itemColor)
|
|
132
|
+
// g.selectAll("g.legendCells")
|
|
133
|
+
// .data(legendValues)
|
|
134
|
+
// .enter()
|
|
135
|
+
// .append("g")
|
|
136
|
+
// .attr("class", "legendCells")
|
|
137
|
+
// }
|
|
138
|
+
|
|
89
139
|
drawLegend();
|
|
90
140
|
}
|
|
91
141
|
// eslint-disable-next-line
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
142
|
+
legend.inputScale = function (newScale: any) {
|
|
143
|
+
// eslint-disable-next-line
|
|
144
|
+
let scale: any = {};
|
|
145
|
+
scale = newScale;
|
|
146
|
+
|
|
147
|
+
scale.domain().forEach(function (el: any) {
|
|
148
|
+
const cellObject = { color: scale(el), label: el };
|
|
149
|
+
legendValues.push(cellObject);
|
|
150
|
+
});
|
|
151
|
+
return this;
|
|
152
|
+
};
|
|
103
153
|
|
|
104
154
|
return legend;
|
|
105
155
|
}
|