@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.
@@ -7,49 +7,68 @@ type LegendItem = {
7
7
  special: boolean
8
8
  }
9
9
 
10
- /**
11
- * applyColorToLegend
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 mapColorPalette = customColors ?? colorPalettes[color] ?? colorPalettes['bluegreen']
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' && mapColorPalette.length < 10 && mapColorPalette.length > 8) {
27
- const newColor = chroma(mapColorPalette[palette.isReversed ? 0 : 8])
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 ? mapColorPalette.unshift(newColor) : mapColorPalette.push(newColor)
24
+ palette.isReversed ? colorPalette.unshift(darkenedColor) : colorPalette.push(darkenedColor)
31
25
  }
32
26
 
33
- const colorIdx = legendIdx - specialClasses.length
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[legendIdx]?.special) {
33
+ if (result[legendItemIndex]?.special) {
37
34
  const specialClassColors = chroma.scale(['#D4D4D4', '#939393']).colors(specialClasses.length)
38
- return specialClassColors[legendIdx]
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')) return mapColorPalette[colorIdx]
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 amt =
46
- Math.max(result.length - specialClasses.length, 1) < 10
47
- ? Math.max(result.length - specialClasses.length, 1)
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 specificColor =
52
- distributionArray[legendIdx - specialClasses.length] ?? mapColorPalette[colorIdx] ?? mapColorPalette.at(-1)
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
- return mapColorPalette[specificColor]
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?.[disabledIdx]?.disabled) {
40
+ if (runtimeLegend.items?.[idx]?.disabled) {
42
41
  return generateColorsArray()
43
42
  }
44
43
 
45
- const legendBinColor = runtimeLegend.items.find(o => o.bin === idx)?.color
46
- return generateColorsArray(legendBinColor, runtimeLegend.items[idx]?.special)
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