@emerson-eps/color-tables 0.2.5 → 0.2.8
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/dist/index.d.ts +8 -5
- package/dist/index.js +7 -4
- package/dist/index.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
- package/src/index.ts +22 -5
- package/tsconfig.json +2 -2
|
@@ -49,7 +49,7 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
49
49
|
try {
|
|
50
50
|
// fix for dash wrapper
|
|
51
51
|
if (typeof colorTables === "string") {
|
|
52
|
-
|
|
52
|
+
const res = await fetch(colorTables);
|
|
53
53
|
dataSet = await res.json()
|
|
54
54
|
}
|
|
55
55
|
//Return the matched colors array from color.tables.json file
|
|
@@ -58,8 +58,6 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
58
58
|
:
|
|
59
59
|
colorsArray(colorName, colorTables);
|
|
60
60
|
|
|
61
|
-
//let legendColors = colorsArray(colorName, colorTables);
|
|
62
|
-
|
|
63
61
|
// Update color of legend based on color selector scales
|
|
64
62
|
// data is passed on click upon color scales
|
|
65
63
|
if (updateLegend) {
|
|
@@ -68,8 +66,8 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
68
66
|
legendColors = updateLegend.color;
|
|
69
67
|
}
|
|
70
68
|
// legend using d3 data
|
|
71
|
-
else if (updateLegend.
|
|
72
|
-
legendColors = updateLegend
|
|
69
|
+
else if (updateLegend.arrayData) {
|
|
70
|
+
legendColors = updateLegend.arrayData;
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
73
|
// main continuous legend
|
|
@@ -84,7 +82,7 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
84
82
|
color: RGBToHex(value).color,
|
|
85
83
|
});
|
|
86
84
|
});
|
|
87
|
-
|
|
85
|
+
|
|
88
86
|
const colorScale = scaleSequential().domain([min, max]);
|
|
89
87
|
// append a defs (for definition) element to your SVG
|
|
90
88
|
const svgLegend = select(divRef.current)
|
|
@@ -100,31 +98,29 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
100
98
|
// .style("transform", "rotate(270deg)")
|
|
101
99
|
// .style("margin-top", "80px");
|
|
102
100
|
.attr("width", "100")
|
|
103
|
-
.attr("height", "
|
|
101
|
+
.attr("height", "190");
|
|
104
102
|
linearGradient = defs
|
|
105
103
|
.append("linearGradient")
|
|
106
104
|
.attr("id", "linear-gradient")
|
|
107
105
|
.attr("x1", "0%")
|
|
108
|
-
.attr("x2", "0%")
|
|
106
|
+
.attr("x2", "0%")
|
|
109
107
|
.attr("y1", "0%")
|
|
110
|
-
.attr("y2", "100%");
|
|
111
|
-
}
|
|
108
|
+
.attr("y2", "100%"); //since it's a vertical linear gradient
|
|
109
|
+
}
|
|
112
110
|
// horizontal legend
|
|
113
111
|
else {
|
|
114
|
-
svgLegend
|
|
115
|
-
|
|
116
|
-
.attr("height", "165")
|
|
117
|
-
|
|
112
|
+
svgLegend.attr("width", "200").attr("height", "90");
|
|
113
|
+
|
|
118
114
|
// append a linearGradient element to the defs and give it a unique id
|
|
119
115
|
linearGradient = defs
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
116
|
+
.append("linearGradient")
|
|
117
|
+
.attr("id", "linear-gradient")
|
|
118
|
+
.attr("x1", "0%")
|
|
119
|
+
.attr("x2", "100%") //since it's a horizontal linear gradient
|
|
120
|
+
.attr("y1", "0%")
|
|
121
|
+
.attr("y2", "0%");
|
|
126
122
|
}
|
|
127
|
-
|
|
123
|
+
|
|
128
124
|
// append multiple color stops by using D3's data/enter step
|
|
129
125
|
linearGradient
|
|
130
126
|
.selectAll("stop")
|
|
@@ -146,7 +142,7 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
146
142
|
.attr("y", 20)
|
|
147
143
|
.style("text-anchor", "left")
|
|
148
144
|
.text(dataObjectName);
|
|
149
|
-
|
|
145
|
+
|
|
150
146
|
// vertical legend
|
|
151
147
|
if (!horizontal) {
|
|
152
148
|
// draw the rectangle and fill with gradient
|
|
@@ -162,43 +158,45 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
162
158
|
else {
|
|
163
159
|
// draw the rectangle and fill with gradient
|
|
164
160
|
svgLegend
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
161
|
+
.append("rect")
|
|
162
|
+
.attr("x", 25)
|
|
163
|
+
.attr("y", 30)
|
|
164
|
+
.attr("width", "150")
|
|
165
|
+
.attr("height", 25)
|
|
166
|
+
.style("fill", "url(#linear-gradient)");
|
|
171
167
|
}
|
|
172
168
|
|
|
173
|
-
//create tick marks
|
|
169
|
+
// create tick marks
|
|
174
170
|
// range varies the size of the axis
|
|
175
171
|
const xLeg = scaleLinear().domain([min, max]).range([10, 158]);
|
|
176
|
-
const yLeg = scaleLinear().domain([min, max]).range([10,
|
|
177
|
-
|
|
178
|
-
const horizontalAxisLeg = axisBottom(xLeg).tickValues(colorScale.domain());
|
|
179
|
-
var VerticalAxisLeg = axisRight(yLeg).tickSize(24).tickValues(colorScale.domain());
|
|
172
|
+
const yLeg = scaleLinear().domain([min, max]).range([10, 158]);
|
|
180
173
|
|
|
174
|
+
const horizontalAxisLeg = axisBottom(xLeg).tickValues(
|
|
175
|
+
colorScale.domain()
|
|
176
|
+
);
|
|
177
|
+
const VerticalAxisLeg = axisRight(yLeg)
|
|
178
|
+
.tickSize(24)
|
|
179
|
+
.tickValues(colorScale.domain());
|
|
181
180
|
|
|
182
181
|
if (horizontal) {
|
|
183
182
|
svgLegend
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
183
|
+
.attr("class", "axis")
|
|
184
|
+
.append("g")
|
|
185
|
+
.attr("transform", "translate(15, 55)")
|
|
186
|
+
.style("font-size", "10px")
|
|
187
|
+
.style("font-weight", "700")
|
|
188
|
+
.call(horizontalAxisLeg)
|
|
189
|
+
.style("height", 25);
|
|
191
190
|
} else {
|
|
192
191
|
svgLegend
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
192
|
+
.attr("class", "axis")
|
|
193
|
+
.append("g")
|
|
194
|
+
.attr("transform", "translate(25, 20)")
|
|
195
|
+
.style("font-size", "10px")
|
|
196
|
+
.style("font-weight", "700")
|
|
197
|
+
.call(VerticalAxisLeg);
|
|
199
198
|
}
|
|
200
|
-
}
|
|
201
|
-
catch (error) {
|
|
199
|
+
} catch (error) {
|
|
202
200
|
console.error(error);
|
|
203
201
|
}
|
|
204
202
|
}
|
|
@@ -207,15 +205,12 @@ export const ContinuousLegend: React.FC<legendProps> = ({
|
|
|
207
205
|
<div
|
|
208
206
|
style={{
|
|
209
207
|
position: "absolute",
|
|
210
|
-
right: position ? position[0] :
|
|
211
|
-
top: position ? position[1] :
|
|
208
|
+
right: position ? position[0] : " ",
|
|
209
|
+
top: position ? position[1] : " ",
|
|
210
|
+
zIndex: 999,
|
|
212
211
|
}}
|
|
213
212
|
>
|
|
214
|
-
<div id="legend" ref={divRef}
|
|
215
|
-
style={{
|
|
216
|
-
width: horizontal ? "220px" : "100px",
|
|
217
|
-
height: horizontal ? "165px" : "150px",
|
|
218
|
-
}}></div>
|
|
213
|
+
<div id="legend" ref={divRef}></div>
|
|
219
214
|
</div>
|
|
220
215
|
);
|
|
221
216
|
};
|
|
@@ -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>
|