@dra2020/dra-types 1.8.108 → 1.8.110
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/datasets.d.ts +16 -6
- package/dist/dra-types.js +87 -65
- package/dist/dra-types.js.map +1 -1
- package/lib/colormgr.ts +51 -27
- package/lib/datasets.ts +18 -6
- package/package.json +2 -2
package/lib/colormgr.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// App libraries
|
|
2
2
|
import * as PF from "./packedfields";
|
|
3
|
-
import {isColorBy, parseColorBy} from './datasets';
|
|
4
|
-
import {Util, Colors} from '@dra2020/baseclient'
|
|
3
|
+
import {isColorBy, parseColorBy, DatasetColor, DatasetFormat} from './datasets';
|
|
4
|
+
import {Util, Colors, Detail} from '@dra2020/baseclient'
|
|
5
5
|
|
|
6
6
|
// All Groups Mosaic has 16 colors
|
|
7
7
|
// Index values into the 16 colors
|
|
@@ -531,39 +531,63 @@ export interface DistrictColorParams
|
|
|
531
531
|
colorDistrictsBy: string,
|
|
532
532
|
}
|
|
533
533
|
|
|
534
|
+
function safeNumber(n: any): number { n = Number(n); return typeof n !== 'number' || isNaN(n) ? 0 : n }
|
|
535
|
+
function safeStops(s: string): number[]
|
|
536
|
+
{
|
|
537
|
+
if (!s) return EthnicFewStops;
|
|
538
|
+
return s.split(',').map(safeNumber);
|
|
539
|
+
}
|
|
540
|
+
function safeColors(s: string): string[]
|
|
541
|
+
{
|
|
542
|
+
if (!s) return ['#fafafa', '#aaaaaa', '#666666', '#111111'];
|
|
543
|
+
return s.split(',').map(s => s.trim());
|
|
544
|
+
}
|
|
545
|
+
|
|
534
546
|
export function ToExtendedColor(agg: PF.PackedFields, dc: PF.DatasetContext, colorBy: string): string
|
|
535
547
|
{
|
|
536
548
|
// compute pct
|
|
537
549
|
const {datasetid, field} = parseColorBy(colorBy);
|
|
538
|
-
if (!datasetid || !field || !dc.dsMeta || !dc.dsMeta[datasetid]
|
|
550
|
+
if (!datasetid || !field || !dc.dsMeta || !dc.dsMeta[datasetid])
|
|
551
|
+
return '#ffffff';
|
|
539
552
|
const meta = dc.dsMeta[datasetid];
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
if (dfield.colorBySum)
|
|
550
|
-
for (let i = 0; i < fields.length; i++)
|
|
553
|
+
const dsfield = meta.fields ? meta.fields[field] : null;
|
|
554
|
+
const dscolor = meta.colors ? meta.colors.find((c: DatasetColor) => c.shortCaption === field) : null;
|
|
555
|
+
if (!dsfield && !dscolor)
|
|
556
|
+
return '#ffffff';
|
|
557
|
+
if (dscolor)
|
|
558
|
+
{
|
|
559
|
+
let stops = safeStops(dscolor.stops);
|
|
560
|
+
let colors = safeColors(dscolor.colors);
|
|
561
|
+
if (stops.length != colors.length)
|
|
551
562
|
{
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
break;
|
|
563
|
+
stops = EthnicFewStops;
|
|
564
|
+
colors = safeColors('');
|
|
555
565
|
}
|
|
566
|
+
let o: any = {};
|
|
567
|
+
let getter = PF.ToGetter(agg, dc, datasetid, datasetid);
|
|
568
|
+
Object.keys(meta.fields).forEach(f => o[f] = getter(f));
|
|
569
|
+
let formatter = new Detail.FormatDetail(dscolor.expr);
|
|
570
|
+
let result = formatter.format(Detail.FormatDetail.prepare(o));
|
|
571
|
+
let intensity = Math.min(Math.max(result.n || 0, 0), 1);
|
|
572
|
+
return Util.execGradient(makeStops(stops, colors), intensity);
|
|
573
|
+
}
|
|
556
574
|
else
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
575
|
+
{
|
|
576
|
+
let getter = PF.ToGetter(agg, dc, datasetid, datasetid);
|
|
577
|
+
let fields = PF.sortedFieldList(meta);
|
|
578
|
+
let den = 0;
|
|
579
|
+
if (meta.fields['Tot'])
|
|
580
|
+
den = getter('Tot');
|
|
581
|
+
else
|
|
582
|
+
fields.forEach(f => { den += getter(f) });
|
|
583
|
+
let num = getter(field);
|
|
584
|
+
|
|
585
|
+
// Careful...
|
|
586
|
+
if (den == 0 || den === undefined || isNaN(den) || num === undefined || isNaN(num))
|
|
587
|
+
return '#ffffff';
|
|
588
|
+
|
|
589
|
+
return Util.execGradient(makeStops(EthnicFewStops, Colors.EthnicFewClassicColors), num / den);
|
|
590
|
+
}
|
|
567
591
|
}
|
|
568
592
|
|
|
569
593
|
export function computeDistrictColors(params: DistrictColorParams): DistrictCache[]
|
package/lib/datasets.ts
CHANGED
|
@@ -8,9 +8,6 @@ export interface DatasetField
|
|
|
8
8
|
order?: number, // For ordering fields in UI
|
|
9
9
|
isCombo?: boolean, // Built-in, for census combos
|
|
10
10
|
colorBy?: boolean, // For extended datasets, display in UI
|
|
11
|
-
invert?: boolean, // For extended datasets, colorby intensity inverted
|
|
12
|
-
colorBySum?: boolean, // For extended datasets, colorby sums up all fields up to this one (e.g. to show "Poverty Level")
|
|
13
|
-
colorLabel?: string, // For extended datasets, provides label for coloring dropdown
|
|
14
11
|
sumOf?: string[], // Only for import processing
|
|
15
12
|
}
|
|
16
13
|
export function sortFields(f1: DatasetField, f2: DatasetField): number
|
|
@@ -45,6 +42,22 @@ export function isColorBy(colorby: string): boolean
|
|
|
45
42
|
// 'Asn', 'Pac', 'OthAl', 'Mix', 'BlC', 'NatC', 'AsnC', 'PacC',
|
|
46
43
|
export type DatasetFields = { [key: string]: DatasetField };
|
|
47
44
|
|
|
45
|
+
export interface DatasetColor {
|
|
46
|
+
shortCaption: string,
|
|
47
|
+
longCaption: string,
|
|
48
|
+
expr: string,
|
|
49
|
+
colors: string,
|
|
50
|
+
stops: string,
|
|
51
|
+
};
|
|
52
|
+
export type DatasetColors = DatasetColor[];
|
|
53
|
+
|
|
54
|
+
export interface DatasetFormat {
|
|
55
|
+
shortCaption: string,
|
|
56
|
+
longCaption: string,
|
|
57
|
+
expr: string,
|
|
58
|
+
};
|
|
59
|
+
export type DatasetFormats = DatasetFormat[];
|
|
60
|
+
|
|
48
61
|
export interface DatasetMeta
|
|
49
62
|
{
|
|
50
63
|
type: string, // demographic | election | pvi | other
|
|
@@ -58,9 +71,8 @@ export interface DatasetMeta
|
|
|
58
71
|
privateKey?: string, // Old-style semi-private datasets
|
|
59
72
|
members?: { [key: number]: string }, // For composites, specifies
|
|
60
73
|
fields?: DatasetFields,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
detailColor?: string, // Optional colorby expressions
|
|
74
|
+
colors?: DatasetColors, // Optional colorby expressions
|
|
75
|
+
formats?: DatasetFormats, // Optional format expressions
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
export type DatasetsMeta = { [dataset: string]: DatasetMeta };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dra2020/dra-types",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.110",
|
|
4
4
|
"description": "Shared types used between client, server and tools.",
|
|
5
5
|
"main": "dist/dra-types.js",
|
|
6
6
|
"types": "./dist/all.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"webpack-cli": "^5.1.4"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@dra2020/baseclient": "^1.0.
|
|
37
|
+
"@dra2020/baseclient": "^1.0.132",
|
|
38
38
|
"geojson": "^0.5.0",
|
|
39
39
|
"object-hash": "^3.0.0"
|
|
40
40
|
}
|