@cdc/map 4.25.6 → 4.25.7
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/cdcmap.js +43118 -22395
- package/examples/private/m.json +427 -0
- package/index.html +9 -0
- package/package.json +3 -3
- package/src/CdcMap.tsx +5 -0
- package/src/CdcMapComponent.tsx +31 -4
- package/src/_stories/CdcMap.stories.tsx +11 -1
- package/src/_stories/_mock/floating-point.json +427 -0
- package/src/components/EditorPanel/components/EditorPanel.tsx +52 -51
- package/src/components/Legend/components/Legend.tsx +33 -3
- package/src/helpers/applyColorToLegend.ts +43 -24
- package/src/helpers/applyLegendToRow.ts +7 -5
- package/src/helpers/generateRuntimeLegend.ts +334 -150
- package/src/scss/main.scss +1 -1
- package/src/types/runtimeLegend.ts +15 -1
- package/examples/m2.json +0 -32904
|
@@ -7,49 +7,68 @@ type LegendItem = {
|
|
|
7
7
|
special: boolean
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @param legendIdx legend item index
|
|
13
|
-
* @param config chart config
|
|
14
|
-
* @param result hash of legend items
|
|
15
|
-
* @returns string - the corresponding color for the legend item
|
|
16
|
-
*/
|
|
17
|
-
export const applyColorToLegend = (legendIdx: number, config: MapConfig, result: LegendItem[] = []): string => {
|
|
10
|
+
// Applies color to a legend item based on its index and special classes.
|
|
11
|
+
export const applyColorToLegend = (legendItemIndex: number, config: MapConfig, result: LegendItem[] = []): string => {
|
|
18
12
|
if (!config) throw new Error('Config is required')
|
|
19
13
|
|
|
20
14
|
const { legend, customColors, general, color } = config
|
|
21
15
|
const { geoType, palette } = general
|
|
22
16
|
const specialClasses = legend?.specialClasses ?? []
|
|
23
|
-
const
|
|
17
|
+
const colorPalette = customColors ?? colorPalettes[color] ?? colorPalettes['bluegreen']
|
|
24
18
|
|
|
25
19
|
// Handle Region Maps need for a 10th color
|
|
26
|
-
if (geoType === 'us-region' &&
|
|
27
|
-
const
|
|
20
|
+
if (geoType === 'us-region' && colorPalette.length < 10 && colorPalette.length > 8) {
|
|
21
|
+
const darkenedColor = chroma(colorPalette[palette.isReversed ? 0 : 8])
|
|
28
22
|
.darken(0.75)
|
|
29
23
|
.hex()
|
|
30
|
-
palette.isReversed ?
|
|
24
|
+
palette.isReversed ? colorPalette.unshift(darkenedColor) : colorPalette.push(darkenedColor)
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
|
|
27
|
+
// Check if there are actually any special classes in the current result
|
|
28
|
+
const actualSpecialClassesCount = result.filter(item => item.special).length
|
|
29
|
+
|
|
30
|
+
const regularItemColorIndex = legendItemIndex - actualSpecialClassesCount
|
|
34
31
|
|
|
35
32
|
// Handle special classes coloring
|
|
36
|
-
if (result[
|
|
33
|
+
if (result[legendItemIndex]?.special) {
|
|
37
34
|
const specialClassColors = chroma.scale(['#D4D4D4', '#939393']).colors(specialClasses.length)
|
|
38
|
-
return specialClassColors[
|
|
35
|
+
return specialClassColors[legendItemIndex]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// For categorical maps with custom colors, use color distribution logic
|
|
39
|
+
if (config.legend?.type === 'category' && customColors) {
|
|
40
|
+
const amt = config.legend.additionalCategories?.length ?? 10
|
|
41
|
+
const distributionArray = colorDistributions[amt] ?? []
|
|
42
|
+
|
|
43
|
+
const specificColor = distributionArray[legendItemIndex - specialClasses.length] ?? colorPalette.at(-1)
|
|
44
|
+
|
|
45
|
+
// If specificColor is a number, use it as an index; otherwise return the color directly
|
|
46
|
+
return colorPalette[specificColor] ?? '#fff'
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
// Use qualitative color palettes directly
|
|
42
|
-
if (color.includes('qualitative'))
|
|
50
|
+
if (color.includes('qualitative')) {
|
|
51
|
+
const safeIndex = Math.max(0, Math.min(regularItemColorIndex, colorPalette.length - 1))
|
|
52
|
+
return colorPalette[safeIndex]
|
|
53
|
+
}
|
|
43
54
|
|
|
44
|
-
// Determine color distribution
|
|
45
|
-
const
|
|
46
|
-
Math.max(result.length -
|
|
47
|
-
? Math.max(result.length -
|
|
55
|
+
// Determine color distribution - use actual special classes count for consistent coloring
|
|
56
|
+
const legendItemCount =
|
|
57
|
+
Math.max(result.length - actualSpecialClassesCount, 1) < 10
|
|
58
|
+
? Math.max(result.length - actualSpecialClassesCount, 1)
|
|
48
59
|
: Object.keys(colorDistributions).length
|
|
49
|
-
const distributionArray = colorDistributions[amt] ?? []
|
|
50
60
|
|
|
51
|
-
const
|
|
52
|
-
|
|
61
|
+
const colorDistributionArray = colorDistributions[legendItemCount] ?? []
|
|
62
|
+
|
|
63
|
+
const rowDistributionIndex = colorDistributionArray[legendItemIndex - actualSpecialClassesCount]
|
|
64
|
+
|
|
65
|
+
const colorValue = rowDistributionIndex ?? colorPalette[regularItemColorIndex] ?? colorPalette.at(-1)
|
|
66
|
+
|
|
67
|
+
// Check if specificColor is a string(e.g., a valid color code)
|
|
68
|
+
if (typeof colorValue === 'string' && config.legend?.type === 'category' && customColors) {
|
|
69
|
+
return colorValue
|
|
70
|
+
}
|
|
53
71
|
|
|
54
|
-
|
|
72
|
+
// Otherwise, use specificColor as an index for mapColorPalette
|
|
73
|
+
return colorPalette[colorValue]
|
|
55
74
|
}
|
|
@@ -29,21 +29,23 @@ export const applyLegendToRow = (
|
|
|
29
29
|
return generateColorsArray(mapColorPalette[3])
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
const hash = hashObj(rowObj)
|
|
32
|
+
const hash = String(hashObj(rowObj))
|
|
33
33
|
|
|
34
34
|
if (!legendMemo.current.has(hash)) {
|
|
35
35
|
return generateColorsArray()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const idx = legendMemo.current.get(hash)!
|
|
39
|
-
const disabledIdx = showSpecialClassesLast ? legendSpecialClassLastMemo.current.get(hash) ?? idx : idx
|
|
40
39
|
|
|
41
|
-
if (runtimeLegend.items?.[
|
|
40
|
+
if (runtimeLegend.items?.[idx]?.disabled) {
|
|
42
41
|
return generateColorsArray()
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
// Use the index from legendMemo which should already be updated for reordered items
|
|
45
|
+
const legendItem = runtimeLegend.items?.[idx]
|
|
46
|
+
const legendBinColor = legendItem?.color
|
|
47
|
+
|
|
48
|
+
return generateColorsArray(legendBinColor, legendItem?.special)
|
|
47
49
|
} catch (e) {
|
|
48
50
|
console.error('COVE: ', e)
|
|
49
51
|
return null
|