-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca84c1d
commit 51094c5
Showing
21 changed files
with
1,276 additions
and
952 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/client/src/dataLayers/legends/ExpressionEvaluator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expression } from "mapbox-gl/dist/style-spec/index.es.js"; | ||
import { Expression as MapboxExpression, StyleFunction } from "mapbox-gl"; | ||
import { Feature } from "geojson"; | ||
|
||
const expressionGlobals = { | ||
zoom: 14, | ||
}; | ||
|
||
/** A color as returned by a Mapbox style expression. All values are in [0, 1] */ | ||
export interface RGBA { | ||
r: number; | ||
g: number; | ||
b: number; | ||
a: number; | ||
} | ||
|
||
interface TypeMap { | ||
string: string; | ||
number: number; | ||
color: RGBA; | ||
boolean: boolean; | ||
[other: string]: any; | ||
} | ||
|
||
// Copied from https://gist.github.com/danvk/4378b6936f9cd634fc8c9f69c4f18b81 | ||
/** | ||
* Class for working with Mapbox style expressions. | ||
* | ||
* See https://docs.mapbox.com/mapbox-gl-js/style-spec/#expressions | ||
*/ | ||
export class ExpressionEvaluator<T> { | ||
/** | ||
* Parse a Mapbox style expression. | ||
* | ||
* Pass an expected type to get tigher error checking and more precise types. | ||
*/ | ||
static parse<T extends expression.StylePropertyType>( | ||
expr: | ||
| number | ||
| string | ||
| Readonly<StyleFunction> | ||
| Readonly<MapboxExpression> | ||
| undefined, | ||
expectedType?: T | ||
): ExpressionEvaluator<TypeMap[T]> { | ||
// For details on use of this private API and plans to publicize it, see | ||
// https://github.com/mapbox/mapbox-gl-js/issues/7670 | ||
let parseResult: expression.ParseResult; | ||
if (expectedType) { | ||
parseResult = expression.createExpression(expr, { type: expectedType }); | ||
if (parseResult.result === "success") { | ||
return new ExpressionEvaluator<TypeMap[T]>(parseResult.value); | ||
} | ||
} else { | ||
parseResult = expression.createExpression(expr); | ||
if (parseResult.result === "success") { | ||
return new ExpressionEvaluator<any>(parseResult.value); | ||
} | ||
} | ||
|
||
throw parseResult.value[0]; | ||
} | ||
|
||
constructor(public parsedExpression: expression.StyleExpression) {} | ||
|
||
evaluate(feature: Feature): T { | ||
return this.parsedExpression.evaluate(expressionGlobals, feature); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/client/src/dataLayers/legends/LegendDataModel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
export interface GLLegendFillSymbol { | ||
type: "fill"; | ||
color: string; | ||
extruded?: boolean; | ||
patternImageId?: string; | ||
/** 0-1 */ | ||
fillOpacity: number; | ||
strokeWidth: number; | ||
strokeOpacity?: number; | ||
strokeColor?: string; | ||
dashed?: boolean; | ||
} | ||
|
||
export interface GLLegendLineSymbol { | ||
type: "line"; | ||
color: string; | ||
strokeWidth: number; | ||
patternImageId?: string; | ||
dashed?: boolean; | ||
opacity?: number; | ||
} | ||
|
||
export interface GLLegendCircleSymbol { | ||
type: "circle"; | ||
color: string; | ||
strokeWidth: number; | ||
strokeColor?: string; | ||
/** 0-1 */ | ||
fillOpacity: number; | ||
strokeOpacity: number; | ||
radius: number; | ||
} | ||
|
||
// TODO: icon-color | ||
export interface GLLegendMarkerSymbol { | ||
type: "marker"; | ||
imageId: string; | ||
haloColor?: string; | ||
haloWidth?: number; | ||
rotation?: number; | ||
/** multiple of width & height to display */ | ||
iconSize: number; | ||
} | ||
|
||
export interface GLLegendRasterSymbol { | ||
type: "raster"; | ||
} | ||
|
||
export interface GLLegendVideoSymbol { | ||
type: "video"; | ||
} | ||
|
||
export interface GLLegendTextSymbol { | ||
type: "text"; | ||
color: string; | ||
fontFamily: string; | ||
fontWeight: "normal" | "bold"; | ||
fontStyle: "normal" | "italic"; | ||
haloColor?: string; | ||
haloWidth?: number; | ||
} | ||
|
||
export type GLLegendSymbol = | ||
| GLLegendFillSymbol | ||
| GLLegendCircleSymbol | ||
| GLLegendMarkerSymbol | ||
| GLLegendTextSymbol | ||
| GLLegendRasterSymbol | ||
| GLLegendLineSymbol | ||
| GLLegendVideoSymbol; | ||
|
||
export type GLLegendListPanel = { | ||
id: string; | ||
type: "GLLegendListPanel"; | ||
label?: string; | ||
items: { id: string; label: string; symbol: GLLegendSymbol }[]; | ||
}; | ||
|
||
/** | ||
* Display should be stacked if bubbles are big and can nest together, otherwise | ||
* display as a list. | ||
* | ||
* Note that a BubblePanel may be paired with a ListPanel for a common case of | ||
* a bubble chart with a categorical variable controlling the color of the bubbles. | ||
*/ | ||
export type GLLegendBubblePanel = { | ||
id: string; | ||
type: "GLLegendBubblePanel"; | ||
label?: string; | ||
stops: { | ||
value: number; | ||
radius: number; | ||
fill: string; | ||
stroke: string; | ||
strokeWidth: number; | ||
}[]; | ||
}; | ||
|
||
export type GLMarkerSizePanel = { | ||
id: string; | ||
type: "GLMarkerSizePanel"; | ||
label?: string; | ||
stops: { | ||
id: string; | ||
imageId: string; | ||
value: number; | ||
iconSize: number; | ||
color?: string; | ||
haloColor?: string; | ||
haloWidth?: number; | ||
rotation?: number; | ||
}[]; | ||
}; | ||
|
||
export type GLLegendStepPanel = { | ||
id: string; | ||
type: "GLLegendStepPanel"; | ||
label?: string; | ||
steps: { id: string; label: string; symbol: GLLegendSymbol }[]; | ||
}; | ||
|
||
export type GLLegendHeatmapPanel = { | ||
id: string; | ||
type: "GLLegendHeatmapPanel"; | ||
stops: { value: number; color: string }[]; | ||
}; | ||
|
||
export type GLLegendGradientPanel = { | ||
id: string; | ||
type: "GLLegendGradientPanel"; | ||
label?: string; | ||
stops: { value: number; label: string; color: string }[]; | ||
}; | ||
|
||
export type GLLegendPanel = | ||
| GLLegendListPanel | ||
| GLLegendBubblePanel | ||
| GLLegendHeatmapPanel | ||
| GLLegendGradientPanel | ||
| GLMarkerSizePanel | ||
| GLLegendStepPanel; | ||
|
||
export type SimpleLegendForGLLayers = { | ||
type: "SimpleGLLegend"; | ||
symbol: GLLegendSymbol; | ||
}; | ||
|
||
export type MultipleSymbolLegendForGLLayers = { | ||
type: "MultipleSymbolGLLegend"; | ||
panels: GLLegendPanel[]; | ||
}; | ||
|
||
export type LegendForGLLayers = | ||
| SimpleLegendForGLLayers | ||
| MultipleSymbolLegendForGLLayers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/client/src/dataLayers/legends/LegendMarkerSizePanel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.